Thursday, August 5, 2010

Hours of operation aint that fun!

I geeked out for a while and worked on hours of operation named scope called 'open'.... This was a beeoych but in the end I kicked it's ass!


named_scope :open, lambda{|at_time| {:include => [:operation_hours],
:conditions => ["
# // FIRST OPEN TIMES DURNING THE PRESENT DAY
( operation_hours.open_time <= :compare_time
and operation_hours.close_time >= :compare_time
and operation_hours.open_day_id = :day_id
and operation_hours.close_day_id = :day_id
)

OR
# // OPEN PREVIOUS DAY AND EXTEND INTO THE EARLY MORNING
(
operation_hours.open_day_id = :day_id -1
and operation_hours.close_time >= :compare_time
and operation_hours.close_day_id = :day_id

)

# // FINALLY OPEN TILL LATE TOMORROW
OR
( operation_hours.open_day_id = :day_id
and operation_hours.open_time <= :compare_time
and operation_hours.close_day_id = :day_id +1
)

", {:day_id => OperationHour.parse(at_time)[0].to_i,
:compare_time => OperationHour.parse(at_time)[1]} ] }}

No comments:

Post a Comment