日历中的重复事件 - Rails [英] Recurring Events in Calendar - Rails

查看:373
本文介绍了日历中的重复事件 - Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找建立周期性活动的最佳方法。我使用fullcalendar来显示事件。但我认为定期事件最好在rails后端处理。



我已经看过其他问题和现有的示例代码,但我没有找到任何适合。 p>

它应该类似google日历。因此,应该可以删除/修改单个事件的周期性事件系列。但是在数据库中保存事件系列的所有事件似乎是低效的。



什么是好的模型架构?



我的事件模型现在看起来像(没有其他属性):

 #表名:事件

#id:整数不为空,主键
#employee_id:整数
#created_at:datetime
#updated_at:datetime
#starts_at: datetime
#ends_at:datetime


class Event< ActiveRecord :: Base
attr_accessible:starts_at,:ends_at
end


方案

这里是我如何建模这个。我没有使用Google日历,因此我将基于 iCal 的定期活动的功能。 p>

所有模型都应该有通常的id,created_at,updated_at属性。列出的是自定义属性。如果属性是另一个模型,您将实现一个关联,如 has_one belongs_to




  • RecurrencePeriod

    • Event base_event has_one:base_event,:class_name'Event'

    • 时间 end_date #可能为零,如果它永远重复。

    • recurrence has_one:recurrence,:as =>:recurrence

    • 数组[OccurrenceOverride] 覆盖 has_many:overrides,:class_name =>'OccurrenceOverride' em>




RecurrencePeriod 从其base_event开始的日期开始。此外,我假设 Event 的employee_id引用创建该事件的员工。 RecurrencePeriod 也将属于创建base_event的员工。



模型取决于您想要如何灵活能够指定重复。你是否支持每周二和周四从上午10点到上午11点和从下午2点到下午3点或只是每周重复?这里有一个支持每周重复,每两周重复等的模型;




  • WeeklyRecurrence

    • 整数 weeks_between_recurrences

    • RecurrencePeriod recurrence_period belongs_to:recurrence,:polymorphic => true




我在这里使用多态关联,因为我认为如果你想要多个类型的重现,例如 WeeklyRecurrence DailyRecurrence ,它们可能是有用的。但我不确定他们是正确的方式来建模,所以如果他们不是,只需使用 has_one:weekly_recurrence belongs_to:recurrence_period



Ice cube 库似乎可能对计算重复有用。如果上面的 WeeklyRecurrence 不够强大,你可能只是想在模型中存储一个Ice cube Schedule 替换 WeeklyRecurrence 。要在模型中存储计划对象,将其保存为属性schedule,将 serialize:schedule 模型定义,并在数据库中生成文本列schedule。



OccurrenceOverride 处理单个




  • OccurrenceOverride

    • RecurrencePeriod recurrence_period_to_override belongs_to:recurrence_period_to_override,:class_name =>'RecurrencePeriod'

    • 时间 original_start_time

    • 事件 replacement_event has_one:replacement_event,:class_name =>'Event';




而不是单独存储每个事件的发生,而是在需要在视图中显示它们时暂时生成它们。在 RecurrencePeriod 中,创建一个生成事件 generate_events_in_range(start_date,end_date)



当用户编辑一个循环时,它们应该是可以显示的,而不是保存在数据库中,可以选择修改所有事件,所有未来事件或仅事件。如果它们修改所有出现,请修改 RecurrencePeriod 的base_event。如果他们修改所有未来的事件,请使用您应该在 RecurrencePeriod 上实现的方法,该方法将自身分为两个 RecurrencePeriod 侧的某个日期,然后将更改保存到仅第二个期间。如果他们仅修改该事件,请在它们覆盖的时间创建 OccurrenceOverride ,并将更改保存到覆盖的replacement_event。



当用户说某个事件现在应该在可预见的未来每两周重复一次,您应该创建一个新的 RecurrencePeriod 作为base_event和nil结束日期。它的重现应该是一个新的 WeeklyRecurrence with weeks_between_recurrence = 2,它应该没有 OccurrenceOverride s。

I am searching for the best way to model recurring events. I am using fullcalendar to display events. But I guess recurring events are best handled on the rails backend.

I already looked at other questions and existing example code but I didn't find anything which fits.

It should behave similar like google calendar. So it should be possible to delete/modify single events of the recurring event series. But saving all events of the event series in the database seems inefficient. Also it should be possible to create single events without any recurrence.

What would be a good model architecture?

My event model right now looks like that (without additional attributes):

# Table name: events
#
#  id              :integer         not null, primary key
#  employee_id     :integer
#  created_at      :datetime
#  updated_at      :datetime
#  starts_at       :datetime
#  ends_at         :datetime
#

class Event < ActiveRecord::Base
  attr_accessible :starts_at, :ends_at
end

解决方案

Here is how I would model this. I haven't used Google Calendar much, so I'm basing the functionality off of iCal's recurring events.

All models should have the usual id, created_at, updated_at properties. Listed are the custom properties. If the property is another model, you will implement it an association such as has_one or belongs_to.

  • RecurrencePeriod
    • Event base_event # has_one :base_event, :class_name'Event'
    • Time end_date # may be nil, if it recurs forever
    • WeeklyRecurrence recurrence # has_one :recurrence, :as=>:recurrence
    • Array[OccurrenceOverride] overrides # has_many :overrides, :class_name=>'OccurrenceOverride'

The RecurrencePeriod starts on the date that its base_event starts. Also, I assume that an Event's employee_id refers to the employee that created that event. A RecurrencePeriod will also belong to the employee that created the base_event.

The model depends on how flexibly you want to be able to specify recurrences. Are you going to support "Tuesday and Thursday every two weeks from 10 AM to 11 AM and from 2 PM to 3 PM" or just "repeats weekly"? Here's a model that supports just "repeats weekly", "repeats every two weeks", etc.; you can expand it if you need to.

  • WeeklyRecurrence
    • Integer weeks_between_recurrences
    • RecurrencePeriod recurrence_period # belongs_to :recurrence, :polymorphic=>true

I use polymorphic associations here, because I think they might be useful if you want more than one type of recurrence, such both WeeklyRecurrence and DailyRecurrence. But I'm not sure that they're the correct way to model that, so if they turn out not to be, just use has_one :weekly_recurrence and belongs_to :recurrence_period instead.

The Ice cube library seems like it might be useful for calculating recurrences. If WeeklyRecurrence above isn't powerful enough, you might just want to store an Ice cube Schedule object in a model, replacing WeeklyRecurrence. To store a Schedule object in a model, save it as an attribute "schedule", put serialize :schedule in the model definition, and generate a text column "schedule" in the database.

OccurrenceOverride handles the case of a single instance of a recurring event being edited.

  • OccurrenceOverride
    • RecurrencePeriod recurrence_period_to_override # belongs_to :recurrence_period_to_override, :class_name=>'RecurrencePeriod'
    • Time original_start_time # uniquely identifies which recurrence within that RecurrencePeriod to replace
    • Event replacement_event # has_one :replacement_event, :class_name=>'Event'; may be nil, if that recurrence was deleted instead of edited

Instead of storing each occurrence of an event individually, generate them temporarily when you need to show them in the view. In RecurrencePeriod, create a method generate_events_in_range(start_date, end_date) that generates Events, not to save in the database, but just to pass to the view so it can show them.

When a user edits a recurrence, they should have the option to modify all occurrences, all future occurrences, or just that event. If they modify all occurrences, modify the RecurrencePeriod's base_event. If they modify all future occurrences, use a method you should implement on RecurrencePeriod that splits itself into two RecurrencePeriods on either side of a certain date, and then save the changes to just the second period. If they modify only that event, create an OccurrenceOverride for the time that they are overriding, and save the changes to the override's replacement_event.

When a user says a certain event should now recur every two weeks for the foreseeable future, you should create a new RecurrencePeriod with that event as base_event and a nil end_date. Its recurrence should be a new WeeklyRecurrence with weeks_between_recurrence=2, and it should have no OccurrenceOverrides.

这篇关于日历中的重复事件 - Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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