ice_cube and recurring_select gems and occurrences
Asked Answered
B

1

8

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)?

Bills answered 11/6, 2013 at 22:6 Comment(0)
B
14

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.

Bills answered 13/6, 2013 at 19:24 Comment(1)
I'm not sure I follow. I've been working with this quite a bit lately as well, and I'm starting to realize (think?) that what I really want is to store the rules, and construct a schedule with those rules after pulling from the database. Can you confirm/deny if you mixed up your use of "rules" and "schedule"? Did you mean you wanted to store the rules and not the schedule?Innovation

© 2022 - 2024 — McMap. All rights reserved.