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!(starts_at:, ends_at:) ⇒ Object
- #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!(starts_at:, ends_at:) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/concerns_on_rails/models/schedulable.rb', line 102 def reschedule!(starts_at:, ends_at:) attrs = {} starts_field = self.class.schedulable_starts_at_field ends_field = self.class.schedulable_ends_at_field attrs[starts_field] = starts_at if starts_field attrs[ends_field] = ends_at if ends_field 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 |