Class: CalMonth
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- CalMonth
- Defined in:
- app/models/cal_month.rb
Class Method Summary collapse
- .create_or_update(attributes = {}) ⇒ Object
- .current_month ⇒ Object
- .fetch_month(year, month) ⇒ Object
- .find_month(year, month) ⇒ Object
-
.upcoming_events(date = nil) ⇒ Object
Returns event data: array of hash, sorted by start datetime e.g.
Instance Method Summary collapse
- #date_events ⇒ Object
- #days ⇒ Object
- #each_week ⇒ Object
- #events_after_day(day = 1) ⇒ Object
- #events_for_day(day) ⇒ Object
- #events_for_day?(day) ⇒ Boolean
- #key ⇒ Object
- #next_cal_month ⇒ Object
- #next_month_array ⇒ Object
- #next_month_key ⇒ Object
- #prev_cal_month ⇒ Object
- #prev_month_array ⇒ Object
- #prev_month_key ⇒ Object
- #replace_event_data(new_data) ⇒ Object
- #to_a ⇒ Object
- #to_date ⇒ Object
- #update_event_data(new_data) ⇒ Object
Class Method Details
.create_or_update(attributes = {}) ⇒ Object
25 26 27 28 29 30 |
# File 'app/models/cal_month.rb', line 25 def self.create_or_update(attributes = {}) month = find_month(attributes[:year], attributes[:month]) || CalMonth.new(year: attributes[:year], month: attributes[:month]) month.update_event_data(attributes[:event_data]) end |
.current_month ⇒ Object
12 13 14 15 |
# File 'app/models/cal_month.rb', line 12 def self.current_month date = Date.today fetch_month(date.year, date.month) end |
.fetch_month(year, month) ⇒ Object
17 18 19 20 21 22 23 |
# File 'app/models/cal_month.rb', line 17 def self.fetch_month(year, month) if found = find_month(year, month) found else CalMonth.new(year: year, month: month) end end |
.find_month(year, month) ⇒ Object
8 9 10 |
# File 'app/models/cal_month.rb', line 8 def self.find_month(year, month) CalMonth.where(year: year, month: month).limit(1).first end |
.upcoming_events(date = nil) ⇒ Object
Returns event data: array of hash, sorted by start datetime e.g. [{“id” => 1, “name” => “Next Event”, “start_datetime” =>
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/models/cal_month.rb', line 34 def self.upcoming_events(date = nil) date ||= Date.today months = where('year >= ? AND month >= ?', date.year, date.month) .order(:year).order(:month).limit(2) unless months.empty? events = months.first.events_after_day(date.day) if events.empty? months.last.events_after_day else events end else [{ start_datetime: Date.today.to_datetime, end_datetime: Date.today.to_datetime, name: '' }] end end |
Instance Method Details
#date_events ⇒ Object
118 119 120 |
# File 'app/models/cal_month.rb', line 118 def date_events date_events_hash.sort_by { |date,_| date.to_i } end |
#days ⇒ Object
106 107 108 |
# File 'app/models/cal_month.rb', line 106 def days (1..to_date.end_of_month.day).to_a end |
#each_week ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'app/models/cal_month.rb', line 134 def each_week date = to_date data = date_events day_of_week_offset = date.wday - 1 day_of_week_offset.times { data.unshift([nil, []]) } if block_given? data.each_slice(7) { |week| yield(week) } else data.each_slice(7) end end |
#events_after_day(day = 1) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/models/cal_month.rb', line 55 def events_after_day(day = 1) eventful_day = date_events[(day-1)..-1].find { |day, data| !data.empty? && data.any? { |event| event[:end_datetime] > Time.zone.now } } return [] unless eventful_day events = eventful_day.last.sort_by {|event| event[:start_datetime]} events.delete_if { |event| event[:end_datetime] < Time.zone.now } end |
#events_for_day(day) ⇒ Object
114 115 116 |
# File 'app/models/cal_month.rb', line 114 def events_for_day(day) date_events_hash[day.to_s] end |
#events_for_day?(day) ⇒ Boolean
110 111 112 |
# File 'app/models/cal_month.rb', line 110 def events_for_day?(day) !events_for_day(day).empty? end |
#key ⇒ Object
98 99 100 |
# File 'app/models/cal_month.rb', line 98 def key key_from_array(to_a) end |
#next_cal_month ⇒ Object
68 69 70 |
# File 'app/models/cal_month.rb', line 68 def next_cal_month CalMonth.fetch_month(*next_month_array) end |
#next_month_array ⇒ Object
80 81 82 83 |
# File 'app/models/cal_month.rb', line 80 def next_month_array next_month = to_date + 1.month [next_month.year, next_month.month] end |
#next_month_key ⇒ Object
90 91 92 |
# File 'app/models/cal_month.rb', line 90 def next_month_key key_from_array(next_month_array) end |
#prev_cal_month ⇒ Object
72 73 74 |
# File 'app/models/cal_month.rb', line 72 def prev_cal_month CalMonth.fetch_month(*prev_month_array) end |
#prev_month_array ⇒ Object
85 86 87 88 |
# File 'app/models/cal_month.rb', line 85 def prev_month_array prev_month = to_date - 1.month [prev_month.year, prev_month.month] end |
#prev_month_key ⇒ Object
94 95 96 |
# File 'app/models/cal_month.rb', line 94 def prev_month_key key_from_array(prev_month_array) end |
#replace_event_data(new_data) ⇒ Object
130 131 132 |
# File 'app/models/cal_month.rb', line 130 def replace_event_data(new_data) update_attribute(:event_data, merge_default_data(new_data).to_json) end |
#to_a ⇒ Object
76 77 78 |
# File 'app/models/cal_month.rb', line 76 def to_a [year, month] end |
#to_date ⇒ Object
102 103 104 |
# File 'app/models/cal_month.rb', line 102 def to_date Date.new(year, month) end |
#update_event_data(new_data) ⇒ Object
122 123 124 125 126 127 128 |
# File 'app/models/cal_month.rb', line 122 def update_event_data(new_data) new_data.stringify_keys old_data = JSON.parse(event_data || '{}') data = merge_default_data(old_data).merge(new_data) update_attribute(:event_data, data.to_json) end |