Class: AddOns::Agenda
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- AddOns::Agenda
- Includes:
- IdentityCache
- Defined in:
- app/models/add_ons/agenda.rb
Class Method Summary collapse
- .day_to_number(day_name) ⇒ Object
- .days_attr_to_ice_cube(day_names) ⇒ Object
- .days_attributes ⇒ Object
Instance Method Summary collapse
- #all_day ⇒ Object
- #all_day=(bool) ⇒ Object
- #available_at?(start_date, end_date, time_zone, date_time) ⇒ Boolean
- #available_times(start_date, end_date, time_zone, date) ⇒ Object
- #fetch_addon ⇒ Object
- #schedule_obj(start_date, end_date, time_zone) ⇒ Object
- #special? ⇒ Boolean
- #to_ice_cube(start_date, end_date) ⇒ Object
Methods inherited from ApplicationRecord
Class Method Details
.day_to_number(day_name) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'app/models/add_ons/agenda.rb', line 345 def day_to_number(day_name) case day_name.to_s.downcase when 'sun', 'sunday' 0 when 'mon', 'monday' 1 when 'tue', 'tuesday' 2 when 'wed', 'wednesday' 3 when 'thu', 'thursday' 4 when 'fri', 'friday' 5 when 'sat', 'saturday' 6 else raise NotImplementedError end end |
.days_attr_to_ice_cube(day_names) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'app/models/add_ons/agenda.rb', line 366 def days_attr_to_ice_cube(day_names) day_names = day_names.select { |_day_name, value| value == true } day_names.map do |day_name, _| case day_name when 'sun', 'sunday' :sunday when 'mon', 'monday' :monday when 'tue', 'tuesday' :tuesday when 'wed', 'wednesday' :wednesday when 'thu', 'thursday' :thursday when 'fri', 'friday' :friday when 'sat', 'saturday' :saturday else raise NotImplementedError end end end |
.days_attributes ⇒ Object
390 391 392 |
# File 'app/models/add_ons/agenda.rb', line 390 def days_attributes %w[sun mon tue wed thu fri sat] end |
Instance Method Details
#all_day ⇒ Object
59 60 61 62 63 64 |
# File 'app/models/add_ons/agenda.rb', line 59 def all_day return false if end_time.blank? duration = end_time - start_time start_time == end_time || duration == 1.day || duration == 1.day - 1.minute end |
#all_day=(bool) ⇒ Object
66 67 68 69 70 71 |
# File 'app/models/add_ons/agenda.rb', line 66 def all_day=(bool) return unless bool self.start_time = '00:00' self.end_time = '23:59' end |
#available_at?(start_date, end_date, time_zone, date_time) ⇒ Boolean
73 74 75 |
# File 'app/models/add_ons/agenda.rb', line 73 def available_at?(start_date, end_date, time_zone, date_time) schedule_obj(start_date, end_date, time_zone)&.occurs_at?(date_time).presence || false end |
#available_times(start_date, end_date, time_zone, date) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'app/models/add_ons/agenda.rb', line 77 def available_times(start_date, end_date, time_zone, date) unless date.is_a?(Date) date = Time.use_zone(time_zone) { Time.zone.parse(date).to_date } end result = schedule_obj(start_date, end_date, time_zone)&.occurrences_between(date.beginning_of_day, date.end_of_day) return [] if result.blank? result end |
#fetch_addon ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'app/models/add_ons/agenda.rb', line 49 def fetch_addon @fetch_addon ||= begin Rails.cache.fetch "#{cache_key}/fetch_addon", expires_in: CACHEFLOW.generate_expiry do add_on end rescue TypeError add_on end end |
#schedule_obj(start_date, end_date, time_zone) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'app/models/add_ons/agenda.rb', line 88 def schedule_obj(start_date, end_date, time_zone) ice_cube_obj = Time.use_zone(time_zone) do to_ice_cube(start_date.to_date, end_date.to_date) end return nil if ice_cube_obj.blank? IceCube::Schedule.from_yaml(ice_cube_obj) rescue StandardError => e APMErrorHandler.report(e) nil end |
#special? ⇒ Boolean
100 101 102 |
# File 'app/models/add_ons/agenda.rb', line 100 def special? fetch_addon.special_agenda? end |
#to_ice_cube(start_date, end_date) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'app/models/add_ons/agenda.rb', line 104 def to_ice_cube(start_date, end_date) return nil if special? && single_occurrences.blank? return nil if !special? && start_time.blank? st = if special? single = single_occurrences.min_by do |single_occurrence| Time.zone.parse("#{single_occurrence[:start_date].to_date} #{single_occurrence[:start_time]}") end if single[:all_day].to_s == 'true' Time.zone.parse(single[:start_date].to_date.to_s).beginning_of_day else return nil if single[:start_date].blank? || single[:start_time].blank? Time.zone.parse("#{single[:start_date].to_date} #{single[:start_time]}") end else return nil if start_date.nil? || start_time.blank? # Extract hour and minute as integers to avoid any timezone interpretation hour = start_time.hour min = start_time.min # Create time in the current Time.zone context (restaurant's timezone) Time.zone.local(start_date.year, start_date.month, start_date.day, hour, min) end sc = IceCube::Schedule.new(st) rules = if special? special_agenda_rules else regular_agenda_rules(st, end_date) end if special? rules.each do |rule| sc.rtime(rule) end else rules.each do |rule| sc.rrule(rule) end end unless special? now = Time.zone.now exception_occurrences.each do |exco| exco = exco.to_hash.with_indifferent_access all_day = exco[:all_day].to_s == 'true' start_time = Time.zone.parse("#{exco[:start_date].to_date} #{all_day ? '00:00' : exco[:start_time]}") end_time = Time.zone.parse("#{exco[:end_date].to_date} #{all_day ? '23:59' : determine_end_time(exco[:end_time]).strftime('%H:%M')}") next if end_time < now next if end_time <= start_time sc.occurrences_between(start_time, end_time).each do |occ_time| sc.add_exception_time(occ_time.to_time) end end end sc.to_yaml rescue StandardError => e APMErrorHandler.report(e) raise e end |