Reimplement one off events now that things are stable

pull/325/merge
Rick Carlino 2017-05-26 14:32:34 -05:00
parent 56be7e96c3
commit 2456ab4707
3 changed files with 29 additions and 9 deletions

View File

@ -4,7 +4,7 @@
# forever.
class FarmEvent < ActiveRecord::Base
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
EXECUTABLE_CLASSES = [Sequence, Regimen]
FE_USE = "still in use by some farm events"

View File

@ -8,6 +8,7 @@ module FarmEvents
"weekly" => 60 * 60 * 24 * 7,
"monthly" => 60 * 60 * 24 * 30, # Not perfect...
"yearly" => 60 * 60 * 24 * 365 }
UNIT_TRANSLATION = { "minutely" => :minutes,
"hourly" => :hours,
"daily" => :days,
@ -25,6 +26,11 @@ module FarmEvents
end
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)
end
@ -40,15 +46,19 @@ module FarmEvents
end
def partial_calendar
start_time > Time.now ? [start_time] : []
in_future? ? [start_time] : []
end
def one_unit
def the_unit
UNIT_TRANSLATION[time_unit]
end
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

View File

@ -41,13 +41,23 @@ describe FarmEvents::GenerateCalendar do
it 'hit more bugs' do
tomorrow = Time.now + 1.day
calendar = FarmEvents::GenerateCalendar.run!(
"start_time" => tomorrow,
"end_time" => tomorrow + 5.minutes,
"repeat" => 1,
"time_unit" => "minutely")
calendar = FarmEvents::GenerateCalendar.run!("start_time" => tomorrow,
"end_time" => tomorrow + 5.minutes,
"repeat" => 1,
"time_unit" => "minutely")
expect(calendar.length).to be > 3
expect(calendar.length).to be < 7
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