Module: ConcernsOnRails::Schedulable

Extended by:
ActiveSupport::Concern
Defined in:
lib/concerns_on_rails/schedulable.rb

Constant Summary collapse

DEFAULT_STARTS_AT_FIELD =
:starts_at
DEFAULT_ENDS_AT_FIELD =
:ends_at

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)


63
64
65
# File 'lib/concerns_on_rails/schedulable.rb', line 63

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

#current?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/concerns_on_rails/schedulable.rb', line 67

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

#expired?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'lib/concerns_on_rails/schedulable.rb', line 79

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



94
95
96
97
98
99
# File 'lib/concerns_on_rails/schedulable.rb', line 94

def finish!(time = Time.zone.now)
  field = self.class.schedulable_ends_at_field
  raise "ConcernsOnRails::Schedulable: ends_at field not configured" unless field

  update(field => time)
end

#reschedule!(starts_at:, ends_at:) ⇒ Object



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

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



87
88
89
90
91
92
# File 'lib/concerns_on_rails/schedulable.rb', line 87

def start!(time = Time.zone.now)
  field = self.class.schedulable_starts_at_field
  raise "ConcernsOnRails::Schedulable: starts_at field not configured" unless field

  update(field => time)
end

#upcoming?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'lib/concerns_on_rails/schedulable.rb', line 71

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

  value > Time.zone.now
end