Class: CalMonth

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/cal_month.rb

Class Method Summary collapse

Instance Method Summary collapse

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_monthObject



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_eventsObject



118
119
120
# File 'app/models/cal_month.rb', line 118

def date_events
  date_events_hash.sort_by { |date,_| date.to_i } 
end

#daysObject



106
107
108
# File 'app/models/cal_month.rb', line 106

def days
  (1..to_date.end_of_month.day).to_a
end

#each_weekObject



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

Returns:

  • (Boolean)


110
111
112
# File 'app/models/cal_month.rb', line 110

def events_for_day?(day)
  !events_for_day(day).empty?
end

#keyObject



98
99
100
# File 'app/models/cal_month.rb', line 98

def key
  key_from_array(to_a)
end

#next_cal_monthObject



68
69
70
# File 'app/models/cal_month.rb', line 68

def next_cal_month
  CalMonth.fetch_month(*next_month_array)
end

#next_month_arrayObject



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_keyObject



90
91
92
# File 'app/models/cal_month.rb', line 90

def next_month_key
  key_from_array(next_month_array)
end

#prev_cal_monthObject



72
73
74
# File 'app/models/cal_month.rb', line 72

def prev_cal_month
  CalMonth.fetch_month(*prev_month_array)
end

#prev_month_arrayObject



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_keyObject



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_aObject



76
77
78
# File 'app/models/cal_month.rb', line 76

def to_a
  [year, month]
end

#to_dateObject



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