Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept a proc, symbol or string for default schedule #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/schedule_attributes/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/active_record_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions spec/support/scheduled_active_record_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down