ice_cube and recurring_select gems and occurrences

偶尔善良 提交于 2019-12-09 16:41:19

问题


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.



来源:https://stackoverflow.com/questions/17054670/ice-cube-and-recurring-select-gems-and-occurrences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!