Module: ConcernsOnRails::Models::Schedulable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/concerns_on_rails/models/schedulable.rb
Constant Summary collapse
- DEFAULT_STARTS_AT_FIELD =
:starts_at- DEFAULT_ENDS_AT_FIELD =
:ends_at
Instance Method Summary collapse
-
#active_at?(time) ⇒ Boolean
Is the record active at the given time? Inclusive start, exclusive end.
- #current? ⇒ Boolean
- #expired? ⇒ Boolean
- #finish!(time = Time.zone.now) ⇒ Object
-
#reschedule!(**changes) ⇒ Object
Update either or both window columns: only the keywords you pass are written (nil clears a side).
- #start!(time = Time.zone.now) ⇒ Object
- #upcoming? ⇒ Boolean
Instance Method Details
#active_at?(time) ⇒ Boolean
Is the record active at the given time? Inclusive start, exclusive end.
64 65 66 |
# File 'lib/concerns_on_rails/models/schedulable.rb', line 64 def active_at?(time) schedulable_started_by?(time) && schedulable_not_ended_at?(time) end |
#current? ⇒ Boolean
68 69 70 |
# File 'lib/concerns_on_rails/models/schedulable.rb', line 68 def current? active_at?(Time.zone.now) end |
#expired? ⇒ Boolean
80 81 82 83 84 85 86 |
# File 'lib/concerns_on_rails/models/schedulable.rb', line 80 def expired? field = self.class.schedulable_ends_at_field value = field && self[field] return false unless value value <= Time.zone.now end |
#finish!(time = Time.zone.now) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/concerns_on_rails/models/schedulable.rb', line 95 def finish!(time = Time.zone.now) field = self.class.schedulable_ends_at_field raise "ConcernsOnRails::Models::Schedulable: ends_at field not configured" unless field update(field => time) end |
#reschedule!(**changes) ⇒ Object
Update either or both window columns: only the keywords you pass are written (nil clears a side). Passing a value for a column that isn't configured raises instead of silently dropping it (the pre-1.22 behavior), and single-column models no longer have to fabricate the missing keyword.
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/concerns_on_rails/models/schedulable.rb', line 107 def reschedule!(**changes) extra = changes.keys - %i[starts_at ends_at] raise ArgumentError, "ConcernsOnRails::Models::Schedulable: unknown option(s): #{extra.join(', ')}" if extra.any? raise ArgumentError, "ConcernsOnRails::Models::Schedulable: reschedule! needs starts_at: and/or ends_at:" if changes.empty? attrs = changes.to_h do |kind, value| field = kind == :starts_at ? self.class.schedulable_starts_at_field : self.class.schedulable_ends_at_field raise ArgumentError, "ConcernsOnRails::Models::Schedulable: #{kind} column is not configured" unless field [field, value] end update(attrs) end |
#start!(time = Time.zone.now) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/concerns_on_rails/models/schedulable.rb', line 88 def start!(time = Time.zone.now) field = self.class.schedulable_starts_at_field raise "ConcernsOnRails::Models::Schedulable: starts_at field not configured" unless field update(field => time) end |
#upcoming? ⇒ Boolean
72 73 74 75 76 77 78 |
# File 'lib/concerns_on_rails/models/schedulable.rb', line 72 def upcoming? field = self.class.schedulable_starts_at_field value = field && self[field] return false unless value value > Time.zone.now end |