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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/models/cal_event.rb', line 19 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
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 69 70 |
# File 'app/models/cal_event.rb', line 40 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
15 16 17 |
# File 'app/models/cal_event.rb', line 15 def self.months_affected all.flat_map(&:months_affected).uniq.sort end |
.to_event_data(year, month) ⇒ Object
72 73 74 75 76 77 78 |
# File 'app/models/cal_event.rb', line 72 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],…]
96 97 98 99 |
# File 'app/models/cal_event.rb', line 96 def datetimes data = JSON.parse(rdate) data.map { |pair| pair.map { |str| DateTime.parse(str) } } end |
#months_affected ⇒ Object
101 102 103 |
# File 'app/models/cal_event.rb', line 101 def months_affected datetimes.flatten.map { |date| [date.year, date.month] }.uniq.sort end |
#to_event_data ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/models/cal_event.rb', line 80 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 |