I want to get latest time slots for 2 cases based on doorman true for false based on logged in user.
- If doorman is true, the query will be the same
- if doorman is false, then I need to add a parameter in the query for doorman that its is false so give me only records having none in them
So, it's the same query almost that will work for both cases with little modification.
def self.latest_pickup_date current_zone,doorman if doorman latest_timeslot = Timeslot.where(dropoff_slots: '-1', zone_id: current_zone).order(:slot_date).last // it is working fine else latest_timeslot = Timeslot.where(dropoff_slots: '-1', zone_id: current_zone, doorman_type: "none").order(:slot_date).last // it also does the same job but need to need check if doorman is none, so need refactoring my queries to use one query for both cases end latest_timeslot.nil? ? Date.current : latest_timeslot.slot_date end
I would like to refactor my code and the query in a way to use the approach of DRY.
I do not want to write these queries twice in both cases. I need a better solution using code practices. Or if I am doing this right way, you can advice as well. Plus, I need good professional code practices and code refactoring as well.