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
23 24 25 26 27 28 |
# File 'app/models/cal_month.rb', line 23 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
10 11 12 13 |
# File 'app/models/cal_month.rb', line 10 def self.current_month date = Date.today fetch_month(date.year, date.month) end |
.fetch_month(year, month) ⇒ Object
15 16 17 18 19 20 21 |
# File 'app/models/cal_month.rb', line 15 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
6 7 8 |
# File 'app/models/cal_month.rb', line 6 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” =>
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/cal_month.rb', line 32 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
116 117 118 |
# File 'app/models/cal_month.rb', line 116 def date_events date_events_hash.sort_by { |date,_| date.to_i } end |
#days ⇒ Object
104 105 106 |
# File 'app/models/cal_month.rb', line 104 def days (1..to_date.end_of_month.day).to_a end |
#each_week ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/models/cal_month.rb', line 132 def each_week date = to_date data = date_events # Because Sunday is 0 (but shown as end of week) day_of_week_offset = (date.wday + 6) % 7 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
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/cal_month.rb', line 53 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
112 113 114 |
# File 'app/models/cal_month.rb', line 112 def events_for_day(day) date_events_hash[day.to_s] end |
#events_for_day?(day) ⇒ Boolean
108 109 110 |
# File 'app/models/cal_month.rb', line 108 def events_for_day?(day) !events_for_day(day).empty? end |
#key ⇒ Object
96 97 98 |
# File 'app/models/cal_month.rb', line 96 def key key_from_array(to_a) end |
#next_cal_month ⇒ Object
66 67 68 |
# File 'app/models/cal_month.rb', line 66 def next_cal_month CalMonth.fetch_month(*next_month_array) end |
#next_month_array ⇒ Object
78 79 80 81 |
# File 'app/models/cal_month.rb', line 78 def next_month_array next_month = to_date + 1.month [next_month.year, next_month.month] end |
#next_month_key ⇒ Object
88 89 90 |
# File 'app/models/cal_month.rb', line 88 def next_month_key key_from_array(next_month_array) end |
#prev_cal_month ⇒ Object
70 71 72 |
# File 'app/models/cal_month.rb', line 70 def prev_cal_month CalMonth.fetch_month(*prev_month_array) end |
#prev_month_array ⇒ Object
83 84 85 86 |
# File 'app/models/cal_month.rb', line 83 def prev_month_array prev_month = to_date - 1.month [prev_month.year, prev_month.month] end |
#prev_month_key ⇒ Object
92 93 94 |
# File 'app/models/cal_month.rb', line 92 def prev_month_key key_from_array(prev_month_array) end |
#replace_event_data(new_data) ⇒ Object
128 129 130 |
# File 'app/models/cal_month.rb', line 128 def replace_event_data(new_data) update_attribute(:event_data, merge_default_data(new_data).to_json) end |
#to_a ⇒ Object
74 75 76 |
# File 'app/models/cal_month.rb', line 74 def to_a [year, month] end |
#to_date ⇒ Object
100 101 102 |
# File 'app/models/cal_month.rb', line 100 def to_date Date.new(year, month) end |
#update_event_data(new_data) ⇒ Object
120 121 122 123 124 125 126 |
# File 'app/models/cal_month.rb', line 120 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 |