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
SCOPE_BASES =
%i[active_at current upcoming expired].freeze

Instance Method Summary collapse

Instance Method Details

#active_at?(time) ⇒ Boolean

Is the record active at the given time? Inclusive start, exclusive end.

Returns:

  • (Boolean)


95
96
97
# File 'lib/concerns_on_rails/models/schedulable.rb', line 95

def active_at?(time)
  schedulable_started_by?(time) && schedulable_not_ended_at?(time)
end

#current?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/concerns_on_rails/models/schedulable.rb', line 99

def current?
  active_at?(Time.zone.now)
end

#expired?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
# File 'lib/concerns_on_rails/models/schedulable.rb', line 111

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



126
127
128
129
130
131
# File 'lib/concerns_on_rails/models/schedulable.rb', line 126

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.

Raises:

  • (ArgumentError)


138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/concerns_on_rails/models/schedulable.rb', line 138

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



119
120
121
122
123
124
# File 'lib/concerns_on_rails/models/schedulable.rb', line 119

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

Returns:

  • (Boolean)


103
104
105
106
107
108
109
# File 'lib/concerns_on_rails/models/schedulable.rb', line 103

def upcoming?
  field = self.class.schedulable_starts_at_field
  value = field && self[field]
  return false unless value

  value > Time.zone.now
end