calendar/app/models/event.rb

115 lines
5.5 KiB
Ruby
Raw Normal View History

2014-12-16 11:40:15 +00:00
require 'time'
require 'date'
class Event
include Mongoid::Document
include Mongoid::Timestamps
include OrbitTag::Taggable
# include Mongoid::MultiParameterAttributes
field :title
field :note
field :start, type: DateTime
field :end, type: DateTime
field :all_day, type: Boolean
field :recurring, type: Boolean
field :frequency
field :period
belongs_to :calendar_type
attr_accessor :agenda_start, :agenda_end, :get_agenda_events
validates_presence_of :title, :message => "Please fill the title of the Event"
def self.format_date(date_time)
Time.at(date_time.to_i).to_formatted_s(:db)
end
REPEATS = [
"Daily" ,
"Weekly" ,
"Monthly" ,
"Yearly"
]
def as_json(options = {})
{
:id => self.id.to_s,
:title => self.title,
:note => self.note || "",
:start => self.start.rfc822,
:end => self.end.rfc822,
:allDay => self.all_day,
:recurring => self.recurring,
:calendar => self.calendar_type_id.to_s,
:color => (self.calendar_type.color rescue nil),
:edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>self.id),
:delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>self.id)
}
end
def self.monthly_event(start_date,end_date)
self.any_of(:start.gte => start_date, :end.gte => start_date).and(:start.lte => end_date).asc(:start)
end
def self.recurring_event(start_date,end_date)
@recurring_events = self.where(:recurring => true)
@recurring = []
@recurring_events.each do |re|
case re.period
when 'Daily'
if (start_date..end_date).cover?(re.start)
@i = TimeDifference.between(re.start,end_date).in_days.to_i
(1..@i).each do |i|
@start_date = re.start + i
@recurring << {:id => re.id.to_s, :title=>re.title, :note=>re.note, :start=>@start_date, :end => @start_date, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.calendar_type.id.to_s, :color => re.calendar_type.color, :edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>re.id), :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>re.id)}
end
elsif re.start < start_date
@i = TimeDifference.between(start_date,end_date).in_days.to_i
(0..@i-1).each do |i|
@start_date = start_date.to_date + i
@recurring << {:id => re.id.to_s, :title=>re.title, :note=>re.note, :start=>@start_date, :end => @start_date, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.calendar_type.id.to_s, :color => re.calendar_type.color, :edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>re.id), :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>re.id)}
end
end
when "Weekly"
@start_date = re.start
@end_date = re.end
@i = TimeDifference.between(re.start,end_date).in_weeks.to_i
(1..@i).each do |i|
@start_date += (7*re.frequency.to_i)
@end_date += (7*re.frequency.to_i)
@recurring << {:id => re.id.to_s, :title=>re.title, :note=>re.note, :start=>@start_date, :end => @end_date, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.calendar_type.id.to_s, :color => re.calendar_type.color, :edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>re.id), :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>re.id)}
end
when "Monthly"
if !(start_date..end_date).cover?(re.start)
sd = re.start
ed = re.end
@i = TimeDifference.between(re.start,end_date).in_months.to_i
@start_date = sd
sd = sd >> @i*re.frequency.to_i
ed = ed >> @i*re.frequency.to_i
@recurring << {:id => re.id.to_s, :title=>re.title, :note=>re.note, :start=>sd, :end => ed, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.calendar_type.id.to_s, :color => re.calendar_type.color, :edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>re.id), :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>re.id)}
end
when "Yearly"
if !(start_date..end_date).cover?(re.start)
sd = re.start
ed = re.end
@i = TimeDifference.between(re.start,end_date).in_years.to_i
@start_date = sd
sd = sd >> 12 * @i*re.frequency.to_i
ed = ed >> 12 * @i*re.frequency.to_i
@recurring << {:id => re.id.to_s, :title=>re.title, :note=>re.note, :start=>sd, :end => ed, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.calendar_type.id.to_s, :color => re.calendar_type.color, :edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>re.id), :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>re.id)}
end
end
end
@recurring
end
def self.agenda_events(agenda_start, agenda_end)
@all_events = self.any_of(:start.gte => agenda_start, :end.gte => agenda_start).and(:start.lte => agenda_end).asc(:start)
end
end