Class: Ecom::Core::CrewTime
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Ecom::Core::CrewTime
- Defined in:
- app/models/ecom/core/crew_time.rb
Constant Summary collapse
- MORNING =
Time Ranges
:morning- AFTERNOON =
:afternoon- FULL_DAY =
:full_day
Instance Method Summary collapse
- #calculate_hours ⇒ Object
- #calculate_total ⇒ Object
-
#compute_hours ⇒ Object
A method to adjust time ranges by trimming any time value outside of the defined morning and afternoon ranges.
- #compute_total_for_entry ⇒ Object
-
#computed_hour ⇒ Object
A similar method as
compute_hoursbut this one computes hours for the the currently presisted checkin_time and checkout_time. -
#define_range ⇒ Object
A method to get the available time ranges at a given point.
-
#find_range(start, finish) ⇒ Object
A method to check if checkin and checkout range falls in the morning, afternoon, or both.
- #time_range_validation ⇒ Object
- #total_time_validation ⇒ Object
Instance Method Details
#calculate_hours ⇒ Object
41 42 43 44 45 46 47 |
# File 'app/models/ecom/core/crew_time.rb', line 41 def calculate_hours self.hours = if checkout_time.nil? || checkin_time.nil? 0 else compute_hours end end |
#calculate_total ⇒ Object
49 50 51 |
# File 'app/models/ecom/core/crew_time.rb', line 49 def calculate_total compute_total_for_entry if saved_change_to_hours? end |
#compute_hours ⇒ Object
A method to adjust time ranges by trimming any time value outside of the defined morning and afternoon ranges
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/models/ecom/core/crew_time.rb', line 102 def compute_hours # Reparse time to avoid errors caused by date differences range = define_range start = Time.zone.parse(checkin_time.strftime('%I:%M%p')) finish = Time.zone.parse(checkout_time.strftime('%I:%M%p')) day_part = find_range(start, finish) left = start.before?(range[day_part][:start]) ? range[day_part][:start] : start right = finish.after?(range[day_part][:finish]) ? range[day_part][:finish] : finish time = (right - left) / 1.hour time -= 1 if day_part == FULL_DAY time end |
#compute_total_for_entry ⇒ Object
53 54 55 56 57 58 59 |
# File 'app/models/ecom/core/crew_time.rb', line 53 def compute_total_for_entry attendance_sheet_entry.total_hours = Ecom::Core::CrewTime.where( attendance_sheet_entry: attendance_sheet_entry, revised: false ).sum(:hours) attendance_sheet_entry.save end |
#computed_hour ⇒ Object
A similar method as compute_hours but this one computes hours for the the currently presisted checkin_time and checkout_time
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/ecom/core/crew_time.rb', line 117 def computed_hour # Reparse time to avoid errors caused by date differences return 0 if checkin_time_was.nil? || checkout_time_was.nil? range = define_range start = Time.zone.parse(checkin_time_was.strftime('%I:%M%p')) finish = Time.zone.parse(checkout_time_was.strftime('%I:%M%p')) day_part = find_range(start, finish) left = start.before?(range[day_part][:start]) ? range[day_part][:start] : start right = finish.after?(range[day_part][:finish]) ? range[day_part][:finish] : finish time = (right - left) / 1.hour time -= 1 if day_part == FULL_DAY time end |
#define_range ⇒ Object
A method to get the available time ranges at a given point. We cannot define the range variables as a constant because the parsing should be done at the exact moment we are about to do time range calculations to avoid errors caused by date mismatches
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/models/ecom/core/crew_time.rb', line 66 def define_range morning = { start: Time.zone.parse('5:00 AM'), finish: Time.zone.parse('9:00 AM') } afternoon = { start: Time.zone.parse('10:00 AM'), finish: Time.zone.parse('2:00 PM') } full_day = { start: Time.zone.parse('5:00 AM'), finish: Time.zone.parse('2:00 PM') } { morning: morning, afternoon: afternoon, full_day: full_day } end |
#find_range(start, finish) ⇒ Object
A method to check if checkin and checkout range falls in the morning, afternoon, or both.
89 90 91 92 93 94 95 96 97 98 |
# File 'app/models/ecom/core/crew_time.rb', line 89 def find_range(start, finish) range = define_range if start.before?(range[:morning][:finish]) && finish.before?(range[:afternoon][:start]) :morning elsif start.after?(range[:morning][:finish]) && finish.after?(range[:afternoon][:start]) :afternoon else :full_day end end |
#time_range_validation ⇒ Object
26 27 28 29 30 |
# File 'app/models/ecom/core/crew_time.rb', line 26 def time_range_validation return unless checkin_time && checkout_time && checkout_time <= checkin_time errors.add(:checkout_time, "can't be less than checkin time.") end |
#total_time_validation ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'app/models/ecom/core/crew_time.rb', line 32 def total_time_validation return if checkout_time.nil? || checkin_time.nil? if new_record? & !persisted? & (attendance_sheet_entry.total_hours + compute_hours > 8) || (attendance_sheet_entry.total_hours + compute_hours - computed_hour > 8) errors.add(:attendance_sheet_entry, 'has more than 8 hours') end end |