Class: CalendarEvent
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- CalendarEvent
- Defined in:
- app/models/calendar_event.rb
Instance Method Summary collapse
-
#add_dates_by_date_value(date_values) ⇒ Object
creates Calendar Event Mods for new dates by date value takes an array.
- #any_weekday_selected? ⇒ Boolean
-
#date_range ⇒ Object
def to_rrules return nil unless recurrences rrules = [] weekly = [] recurrences.each do |recurrence| if recurrence.monthly? rrules << (params = => ‘MONTHLY’) if !recurrence.weekly? params = recurrence.monthday.to_s else icalmw = ((mw = recurrence.monthweek) >= 0) ? mw + 1 : mw icaldc = Icalendar::DAYCODES params = icalmw.to_s + icaldc end else weekly << recurrence.weekday end end if !weekly.empty? rrules << => ‘WEEKLY’, ‘BYDAY’ => weekly.map {|w| Icalendar::DAYCODES.join(‘,’)} end rrules end.
- #event_type_matches?(*event_types) ⇒ Boolean
-
#remove_dates_by_id(date_ids) ⇒ Object
creates Calendar Event Mods for removed dates by date id takes an array.
- #weekday_selected?(n) ⇒ Boolean
Instance Method Details
#add_dates_by_date_value(date_values) ⇒ Object
creates Calendar Event Mods for new dates by date value takes an array
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/calendar_event.rb', line 85 def add_dates_by_date_value(date_values) transaction do date_values.each do |date_value| next if date_value.blank? begin CalendarDate.create_for_date(date_value.to_date) event_date = event_dates.first(:conditions => {:date_value => date_value}) next if event_date && !event_date.removed? if event_date event_date.mod.destroy else date_id = CalendarDate.find_by_value(date_value).id mods.create(:calendar_date_id => date_id) end rescue errors.add_to_base("Invalid date: #{date_value}") raise ActiveRecord::Rollback end end end end |
#any_weekday_selected? ⇒ Boolean
72 73 74 75 76 77 |
# File 'app/models/calendar_event.rb', line 72 def any_weekday_selected? (0..6).each do |n| return true if weekday_selected?(n) end return false end |
#date_range ⇒ Object
def to_rrules
return nil unless recurrences
rrules = []
weekly = []
recurrences.each do |recurrence|
if recurrence.monthly?
rrules << (params = {'FREQ' => 'MONTHLY'})
if !recurrence.weekly?
params['BYMONTHDAY'] = recurrence.monthday.to_s
else
icalmw = ((mw = recurrence.monthweek) >= 0) ? mw + 1 : mw
icaldc = Icalendar::DAYCODES[recurrence.weekday]
params['BYDAY'] = icalmw.to_s + icaldc
end
else
weekly << recurrence.weekday
end
end
if !weekly.empty?
rrules << {'FREQ' => 'WEEKLY',
'BYDAY' => weekly.map {|w| Icalendar::DAYCODES[w]}.join(',')}
end
rrules
end
60 61 62 63 |
# File 'app/models/calendar_event.rb', line 60 def date_range e_date = self.end_date.blank? ? self.start_date : self.end_date self.start_date.to_date..e_date.to_date end |
#event_type_matches?(*event_types) ⇒ Boolean
65 66 67 68 69 70 |
# File 'app/models/calendar_event.rb', line 65 def event_type_matches?(*event_types) return false unless event_type name = event_type.name.to_sym event_types.each {|et| return true if et.to_sym == name } return false end |
#remove_dates_by_id(date_ids) ⇒ Object
creates Calendar Event Mods for removed dates by date id takes an array
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/models/calendar_event.rb', line 109 def remove_dates_by_id(date_ids) date_ids.each do |id| next if id.blank? event_date = event_dates.first(:conditions => {:calendar_date_id => id}) next unless event_date && !event_date.removed? if event_date.added? || event_date.modified? event_date.mod.destroy end unless event_date.added? mods.create(:calendar_date_id => id, :removed => true) end end end |
#weekday_selected?(n) ⇒ Boolean
79 80 81 |
# File 'app/models/calendar_event.rb', line 79 def weekday_selected?(n) self.send("repeat_#{n}") == true end |