This is the dirty bastard of my current code block, and I'm trying to find a way to improve this code for increased readability, but also functionality.
The method checks if the store is currently open. Yet on our site we close the sites 30 minutes prior to their real closing time. Issues arise mostly because all of our stores are open past midnight.
So this function is supposed to figure out if a store is open, taking into account that we close it 30 minutes before it's actual close time, and the fact that if it closes 12:10AM the next day, we have to close it 11:40PM the prior day.
def open?(current_time: Time.now.change(sec: 0)) current_business_hour = business_hours.where('week_day = ? AND open_at <= ? AND close_at >= ?', current_time.wday, current_time, current_time).first if current_business_hour safe_closing_hour = current_business_hour.close_at - 30.minutes if current_time.strftime('%H:%M') >= '23:29' next_opening_hour = business_hours.where(week_day: DateTime.now.tomorrow.wday).order(:open_at).first if next_opening_hour if next_opening_hour.open_at.strftime('%H:%M') == '00:00' if next_opening_hour.close_at.strftime('%H:%M') >= '00:29' true elsif (next_opening_hour.close_at - 30.minutes).strftime('%H:%M') < current_time.strftime('%H:%M') false else true end else false end else false end elsif safe_closing_hour.strftime('%H:%M') < current_time.strftime('%H:%M') false else true end else false end end
It's an unappealing spiderweb of if else statements, I do hope that the code makes someone what sense when you read it, then it's just the puzzle of understanding which true and false belongs to which.
I also feel like this is awfully complicated, and I wish that there was an easier way to do this.
Each store has many business hours, which has the
week_day and an
close_at. The store can have several business hours in one week_day. F.ex a store can close at 1AM, but open at 11AM and go til 11:59PM.