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
-
#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.
Instance Method Details
#calculate_hours ⇒ Object
30 31 32 33 34 35 36 |
# File 'app/models/ecom/core/crew_time.rb', line 30 def calculate_hours self.hours = if checkout_time.nil? || checkin_time.nil? 0 else compute_hours end end |
#calculate_total ⇒ Object
38 39 40 |
# File 'app/models/ecom/core/crew_time.rb', line 38 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
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/models/ecom/core/crew_time.rb', line 91 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
42 43 44 45 46 47 48 |
# File 'app/models/ecom/core/crew_time.rb', line 42 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 |
#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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/ecom/core/crew_time.rb', line 55 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.
78 79 80 81 82 83 84 85 86 87 |
# File 'app/models/ecom/core/crew_time.rb', line 78 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 |