Class: RestaurantInfoLib

Inherits:
Object
  • Object
show all
Defined in:
app/my_lib/restaurant_info_lib.rb

Overview

this class result is cached by today date

Instance Method Summary collapse

Constructor Details

#initialize(restaurant) ⇒ RestaurantInfoLib

Returns a new instance of RestaurantInfoLib.



5
6
7
# File 'app/my_lib/restaurant_info_lib.rb', line 5

def initialize(restaurant)
  @restaurant = restaurant
end

Instance Method Details

#get_hour(day) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/my_lib/restaurant_info_lib.rb', line 25

def get_hour(day)
  closed = I18n.t('restaurant.closed')
  day_id = if restaurant.is_take_away? && !restaurant.is_dine_in?
             restaurant.public_send(:"#{day}_take_away")
           else
             restaurant.public_send(day)
           end
  return closed if day_id.blank?

  Rails.cache.fetch(get_hour_cache_key(day)) do
    inventory_template_group = InventoryTemplateGroup.find_by(id: day_id)
    return closed if inventory_template_group.blank?

    inventory_templates = inventory_template_group.inventory_templates
    return closed if inventory_templates.blank?

    start_time = inventory_templates.order(start_time: :asc).first.start_time

    # Treat 00:00 end_time as "end of day"
    end_time = inventory_templates.
      pluck(:end_time).
      max_by { |t| t.hour == 0 && t.min == 0 && t.sec == 0 ? t + 1.day : t }

    start_str = start_time.strftime('%H:%M')
    end_str = end_time.hour == 0 && end_time.min == 0 && end_time.sec == 0 ? '24:00' : end_time.strftime('%H:%M')

    "#{start_str} - #{end_str}"
  end
end

#get_hour_cache_key(day) ⇒ Object



60
61
62
63
# File 'app/my_lib/restaurant_info_lib.rb', line 60

def get_hour_cache_key(day)
  today = Time.now_in_tz(restaurant.time_zone).to_date
  "#{self.class}:#{restaurant.id}:#{today}:get_hour:#{day}:#{I18n.locale}"
end

#opening_hours_shortObject



9
10
11
12
# File 'app/my_lib/restaurant_info_lib.rb', line 9

def opening_hours_short
  today = DateTime.now.strftime('%a').downcase
  get_hour(today)
end

#weekday_opening_hoursObject



14
15
16
17
18
19
20
21
22
23
# File 'app/my_lib/restaurant_info_lib.rb', line 14

def weekday_opening_hours
  today = Time.now_in_tz(restaurant.time_zone).to_date
  Rails.cache.fetch(weekday_opening_hours_cache_key) do
    weekday_hours = {}
    today.upto(today + 6.days).sort_by(&:cwday).map do |day|
      weekday_hours[day.strftime('%a').downcase.to_s] = get_hour(day.strftime('%a').downcase)
    end
    weekday_hours
  end
end

#weekday_opening_hours_cache_keyObject



55
56
57
58
# File 'app/my_lib/restaurant_info_lib.rb', line 55

def weekday_opening_hours_cache_key
  today = Time.now_in_tz(restaurant.time_zone).to_date
  "#{self.class}:#{restaurant.id}:#{today}:#{I18n.locale}:weekday_opening_hours"
end