Schedule Attributes allows models (ORM agnostic) to accept recurring schedule form parameters and translate them into an IceCube Schedule object. Schedule Attributes adds #schedule_attributes and #schedule_attributes= methods that let your model automatically populate Rails forms and receive HTML form parameters. Additionally, it provides access to the IceCube::Schedule object itself.
To use, include the ScheduleAttributes module in your model class.
class SomeModel
include ScheduleAttributes
end
Your model table is expected have the following columns:
- schedule_yaml : String or Text
- exception_dates : String or Text
- additional_dates : String or Text
If you are using ActiveRecord, make a string or text column for each attribute. If you're using Mongoid, make a string field like so: field :schedule_yaml.
You model will gain the following methods:
Accepts form a parameter hash that represent a schedule, and creates an IceCube::Schedule object, and serializes it into your schedule_yaml field.
E.x.
@event.schedule_attributes = params[:event][:schedule_attributes]
Because they are coming from a form, all parameters are expected to be in string format.
Can be 0 or 1. The parameter should respond to :to_i. 0 indicates that the event does not repeat. Anything else indicates that the event repeats.
Should be 1 or greater. The parameter is delegated to the IceCube initialization.
The following parameters will only be used if the repeat parameter is 0.
The date that this (non-repeating) event is scheduled for. Should be parseable by Time.parse. This parameter is only used if :repeat is 0.
The following parameters will only be used if the repeat parameter is 1.
The date at which the event starts repeating. Must be parseable by Time.parse. If not given, will use the current date.
The interval unit by which the event repeats. Can be "day", "week" or "month". For example, if the even repeats every day, this would be "day". If it repeats weekly, this would be "week". If it repeats monthly, this would be "monthly".
The interval by which the event repeats. Should be an integer (in string format). For example, if the event repeats every other day, this parameter should be 2, and :interval_unit should be "day". If it repeats every other week, it should be 2 and interval unit should be "week".
Indicates with a 0 or 1 whether the event repeats on this day. For example, if the event repeats every other week on Tues and Thursday, then the parameters would include these: :interval => "2", :interval_unit => "week", :tuesday => "1", :thursday => "1". These parameters are only used if :repeat is 1 and :interval_unit is "week".
Only used if :interval_unit id "monthly". An array with the week-of-month numbers during which the event repeats. For example, if the event repeats in every first and third week of every month, :weeks_of_month would be [1, 3].
Provides an OStruct with methods that correspond to the attributes accepted by #schedule_attributes=. ActiveRecord can use this object to populate a form using FormHelper#fields_for.
##schedule
Returns a the IceCube::Schedule object that is serialized to the model's schedule_yaml field. If there is none, it returns a schedule with start_date of the current date, recurring daily, ending never.
##add_exception_date
Accepts either a Date, Time, or Array consisting of dates to be explicitly excluded from the schedule.
##add_additional_date
Accepts either a Date, Time, or Array consisting of dates to be explicitly added to the schedule.