ice_cube and recurring_select gems and occurrences

2019-04-06 17:24发布

问题:

I'm attempting to utilize the [awesome] functionality of ice_cube and recurring_select gems to handle recurring events. I've got a schedule (text) column in my database and the following in the event model:

  def schedule=(new_schedule)
    write_attribute(:schedule, RecurringSelect.dirty_hash_to_rule(new_schedule).to_yaml)
  end

  def converted_schedule
     Schedule.from_yaml(self.schedule, :start_date_override => self.start_date)
  end 

Looking at the schedule column in psql, it appears to be storing the schedule correctly.

Here's my form:

.control-group
  = f.label 'Date', :class => 'control-label'
  .controls
    = f.text_field :start_date, :class => 'datepicker'

.control-group
  = f.label 'Recurring?', :class => 'control-label'
  .controls
    = f.select_recurring :schedule, :allow_blank => true

However, when I attempt to output converted_schedule, it only shows the start date and won't show any additional dates. I have a few suspicions that I've tinkered with no success... perhaps the YAML isn't being converted correctly for the converted_schedule method? Perhaps I need an end-date (I don't see where this functionality is available on recurring_select)?

回答1:

After consulting John Crepezzi (author of the ice_cube gem – thanks John!), I found that I was storing the rule for the recurrences as opposed to the schedule itself. The following code fixed the issue:

serialize :schedule, Hash

def schedule=(new_schedule)
  write_attribute(:schedule, RecurringSelect.dirty_hash_to_rule(new_schedule).to_hash)
end

def converted_schedule
  the_schedule = Schedule.new(self.start_date)
  the_schedule.add_recurrence_rule(RecurringSelect.dirty_hash_to_rule(self.schedule))
  the_schedule
end

Note: I also switched to storing that column as a hash instead of YAML as previously posted.