ice_cube 和 recurring_select gems 和事件 [英] ice_cube and recurring_select gems and occurrences

查看:32
本文介绍了ice_cube 和 recurring_select gems 和事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试利用 ice_cube 和 recurring_select gems 的 [awesome] 功能来处理重复发生的事件.我的数据库中有一个 schedule(文本)列,事件模型中有以下内容:

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 

查看 psql 中的 schedule 列,它似乎正确存储了时间表.

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

这是我的表格:

.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

但是,当我尝试输出 converted_schedule 时,它只显示开始日期,不会显示任何其他日期.我有一些怀疑,我的修补没有成功……也许 YAML 没有为 converted_schedule 方法正确转换?也许我需要一个结束日期(我在 recurring_select 上看不到此功能可用的位置)?

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

推荐答案

在咨询了 John Crepezzi(ice_cube gem 的作者 – 感谢 John!)后,我发现我存储的是重复规则而不是日程表本身.以下代码修复了该问题:

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

注意:我还切换到将该列存储为哈希,而不是之前发布的 YAML.

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

这篇关于ice_cube 和 recurring_select gems 和事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆