Reimplement one off events now that things are stable
parent
56be7e96c3
commit
2456ab4707
|
@ -4,7 +4,7 @@
|
||||||
# forever.
|
# forever.
|
||||||
class FarmEvent < ActiveRecord::Base
|
class FarmEvent < ActiveRecord::Base
|
||||||
NEVER = "never"
|
NEVER = "never"
|
||||||
UNITS_OF_TIME = %w(minutely hourly daily weekly monthly yearly)
|
UNITS_OF_TIME = %w(never minutely hourly daily weekly monthly yearly)
|
||||||
UNITS_OF_TIME << NEVER
|
UNITS_OF_TIME << NEVER
|
||||||
EXECUTABLE_CLASSES = [Sequence, Regimen]
|
EXECUTABLE_CLASSES = [Sequence, Regimen]
|
||||||
FE_USE = "still in use by some farm events"
|
FE_USE = "still in use by some farm events"
|
||||||
|
|
|
@ -8,6 +8,7 @@ module FarmEvents
|
||||||
"weekly" => 60 * 60 * 24 * 7,
|
"weekly" => 60 * 60 * 24 * 7,
|
||||||
"monthly" => 60 * 60 * 24 * 30, # Not perfect...
|
"monthly" => 60 * 60 * 24 * 30, # Not perfect...
|
||||||
"yearly" => 60 * 60 * 24 * 365 }
|
"yearly" => 60 * 60 * 24 * 365 }
|
||||||
|
|
||||||
UNIT_TRANSLATION = { "minutely" => :minutes,
|
UNIT_TRANSLATION = { "minutely" => :minutes,
|
||||||
"hourly" => :hours,
|
"hourly" => :hours,
|
||||||
"daily" => :days,
|
"daily" => :days,
|
||||||
|
@ -25,6 +26,11 @@ module FarmEvents
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute
|
def execute
|
||||||
|
# Does the input have a valid repeat?
|
||||||
|
# Is it in the future?
|
||||||
|
# Then generate a calendar.
|
||||||
|
# Otherwise, return a "partial calendar" that is either empty or (in the
|
||||||
|
# case of one-off events) has only one date in it (start_time).
|
||||||
(every ? full_calendar : partial_calendar)
|
(every ? full_calendar : partial_calendar)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,15 +46,19 @@ module FarmEvents
|
||||||
end
|
end
|
||||||
|
|
||||||
def partial_calendar
|
def partial_calendar
|
||||||
start_time > Time.now ? [start_time] : []
|
in_future? ? [start_time] : []
|
||||||
end
|
end
|
||||||
|
|
||||||
def one_unit
|
def the_unit
|
||||||
UNIT_TRANSLATION[time_unit]
|
UNIT_TRANSLATION[time_unit]
|
||||||
end
|
end
|
||||||
|
|
||||||
def every
|
def every
|
||||||
one_unit && repeat.send(one_unit)
|
(the_unit != NEVER) && the_unit && repeat.send(the_unit)
|
||||||
|
end
|
||||||
|
|
||||||
|
def in_future?
|
||||||
|
start_time > Time.now
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -41,8 +41,7 @@ describe FarmEvents::GenerateCalendar do
|
||||||
|
|
||||||
it 'hit more bugs' do
|
it 'hit more bugs' do
|
||||||
tomorrow = Time.now + 1.day
|
tomorrow = Time.now + 1.day
|
||||||
calendar = FarmEvents::GenerateCalendar.run!(
|
calendar = FarmEvents::GenerateCalendar.run!("start_time" => tomorrow,
|
||||||
"start_time" => tomorrow,
|
|
||||||
"end_time" => tomorrow + 5.minutes,
|
"end_time" => tomorrow + 5.minutes,
|
||||||
"repeat" => 1,
|
"repeat" => 1,
|
||||||
"time_unit" => "minutely")
|
"time_unit" => "minutely")
|
||||||
|
@ -50,4 +49,15 @@ describe FarmEvents::GenerateCalendar do
|
||||||
expect(calendar.length).to be > 3
|
expect(calendar.length).to be > 3
|
||||||
expect(calendar.length).to be < 7
|
expect(calendar.length).to be < 7
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'schedules one-off events' do
|
||||||
|
tomorrow = Time.now + 1.day
|
||||||
|
params = { start_time: tomorrow,
|
||||||
|
end_time: nil,
|
||||||
|
repeat: 1,
|
||||||
|
time_unit: "never" }
|
||||||
|
calendar = FarmEvents::GenerateCalendar.run!(params)
|
||||||
|
expect(calendar.length).to eq(1)
|
||||||
|
expect(calendar.first).to eq(params[:start_time])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue