Class: CalEvent
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- CalEvent
- Defined in:
- app/models/cal_event.rb
Class Method Summary collapse
- .create_or_update(attributes = {}) ⇒ Object
- .import_from_ical(ical) ⇒ Object
- .months_affected ⇒ Object
- .to_event_data(year, month) ⇒ Object
Instance Method Summary collapse
-
#datetimes ⇒ Object
array of array [[start_datetime, end_datetime],…].
- #months_affected ⇒ Object
- #to_event_data ⇒ Object
Class Method Details
.create_or_update(attributes = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/cal_event.rb', line 17 def self.create_or_update(attributes = {}) new_event = CalEvent.new(attributes) raise ActiveRecord::RecordInvalid unless new_event.valid? found = CalEvent.find_by_system_uid(new_event.system_uid) if found if found.system_updated_at < new_event.system_updated_at found.update_attributes( name: new_event.name, description: new_event.description, system_updated_at: new_event.system_updated_at ) end found else new_event.save new_event end end |
.import_from_ical(ical) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/models/cal_event.rb', line 38 def self.import_from_ical(ical) ids = [] cals = Icalendar.parse(ical) cals.each do |cal| cal.events.each do |event| db_event = CalEvent.create_or_update( name: event.summary.to_s, description: event.description.to_s, system_uid: event.uid.to_s, system_updated_at: event.last_modified.to_datetime, ) if event.rdate.empty? db_event.rdate = [ [event.dtstart.to_datetime, event.dtend.to_datetime] ].to_json else db_event.rdate = event.rdate[0].to_json end db_event.save ids << db_event.id end end events = CalEvent.where(id: ids) events.months_affected.each do |year, month| data = events.for_month(year, month).to_event_data(year, month) CalMonth.create_or_update( year: year, month: month, event_data: data ) end events end |
.months_affected ⇒ Object
13 14 15 |
# File 'app/models/cal_event.rb', line 13 def self.months_affected all.flat_map(&:months_affected).uniq.sort end |
.to_event_data(year, month) ⇒ Object
70 71 72 73 74 75 76 |
# File 'app/models/cal_event.rb', line 70 def self.to_event_data(year, month) all.each_with_object(Hash.new {|h,k| h[k] = []}) { |event, data| event.to_event_data[[year, month]].each do |day, event_data| data[day] << event_data end } end |
Instance Method Details
#datetimes ⇒ Object
array of array [[start_datetime, end_datetime],…]
94 95 96 97 |
# File 'app/models/cal_event.rb', line 94 def datetimes data = JSON.parse(rdate || "[]") data.map { |pair| pair.map { |str| DateTime.parse(str) } } end |
#months_affected ⇒ Object
99 100 101 |
# File 'app/models/cal_event.rb', line 99 def months_affected datetimes.flatten.map { |date| [date.year, date.month] }.uniq.sort end |
#to_event_data ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/models/cal_event.rb', line 78 def to_event_data data = Hash.new { |h,k| h[k] = {} } datetimes.each {|start_datetime, end_datetime| (start_datetime..end_datetime).each do |day| data[[day.year, day.month]][day.day] = { id: id, name: name, start_datetime: start_datetime, end_datetime: end_datetime } end } data end |