From cab025d5ee305644ee9237f630c3a8f55e2ef19d Mon Sep 17 00:00:00 2001 From: Mark Edmondson Date: Thu, 6 Jun 2019 17:12:59 -0700 Subject: [PATCH] Accept a proc, symbol or string for default schedule --- lib/schedule_attributes/active_record.rb | 9 ++++++++- spec/active_record_integration_spec.rb | 14 ++++++++++++++ spec/support/scheduled_active_record_model.rb | 11 +++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/schedule_attributes/active_record.rb b/lib/schedule_attributes/active_record.rb index 96356fa..d94898c 100644 --- a/lib/schedule_attributes/active_record.rb +++ b/lib/schedule_attributes/active_record.rb @@ -7,9 +7,16 @@ module ScheduleAttributes::ActiveRecord module ClassMethods attr_accessor :schedule_field - attr_accessor :default_schedule + attr_writer :default_schedule def default_schedule + @default_schedule = case @default_schedule + when Proc + @default_schedule.call + when Symbol, String + self.public_send(@default_schedule) + end if @default_schedule + @default_schedule || ScheduleAttributes.default_schedule end end diff --git a/spec/active_record_integration_spec.rb b/spec/active_record_integration_spec.rb index a564fc5..b5676c0 100644 --- a/spec/active_record_integration_spec.rb +++ b/spec/active_record_integration_spec.rb @@ -16,6 +16,20 @@ def hourly end +describe CustomScheduledMethodActiveRecordModel do + + it "should have a default schedule" do + subject.schedule.should == today + end + + def today + IceCube::Schedule.new(Date.today.to_time).tap { |s| + s.add_recurrence_rule IceCube::SingleOccurrenceRule.new(Date.today) + } + end + +end + describe DefaultScheduledActiveRecordModel do alias :model :subject diff --git a/spec/support/scheduled_active_record_model.rb b/spec/support/scheduled_active_record_model.rb index 169b365..9698a00 100644 --- a/spec/support/scheduled_active_record_model.rb +++ b/spec/support/scheduled_active_record_model.rb @@ -31,6 +31,17 @@ def initialize(*args) end end +class CustomScheduledMethodActiveRecordModel < ActiveRecord::Base + self.table_name = :calendars + has_schedule_attributes default_schedule: :today + + def self.today + s = IceCube::Schedule.new(Date.today.to_time) + s.add_recurrence_rule IceCube::SingleOccurrenceRule.new(Date.today) + s + end +end + class DefaultScheduledActiveRecordModel < ActiveRecord::Base self.table_name = :calendars has_schedule_attributes