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



24
25
26
27
28
29
# File 'app/models/cal_month.rb', line 24

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



11
12
13
14
# File 'app/models/cal_month.rb', line 11

def self.current_month
  date = Date.today
  fetch_month(date.year, date.month)
end

.fetch_month(year, month) ⇒ Object



16
17
18
19
20
21
22
# File 'app/models/cal_month.rb', line 16

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



7
8
9
# File 'app/models/cal_month.rb', line 7

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” =>



33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/cal_month.rb', line 33

def self.upcoming_events(date = nil)
  date  ||= Date.today
  month = where('year >= ? AND month >= ?', date.year, date.month)
    .order(:year).order(:month).limit(1).first
  events = month.date_events[(date.day-1)..-1].find { |day, data| 
    !data.empty? && 
      data.any? { |event| 
        event[:end_datetime] > Time.zone.now
      }
  }.last.sort_by { |event| event[:start_datetime] }
  events.delete_if { |event| event[:end_datetime] < Time.zone.now }
end

Instance Method Details

#date_eventsObject



86
87
88
# File 'app/models/cal_month.rb', line 86

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

#daysObject



74
75
76
# File 'app/models/cal_month.rb', line 74

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

#each_weekObject



98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/cal_month.rb', line 98

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_for_day(day) ⇒ Object



82
83
84
# File 'app/models/cal_month.rb', line 82

def events_for_day(day)
  date_events_hash[day.to_s]
end

#events_for_day?(day) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/cal_month.rb', line 78

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

#next_month_arrayObject



50
51
52
53
54
55
56
57
58
# File 'app/models/cal_month.rb', line 50

def next_month_array
  n_year  = year
  n_month = month + 1
  if n_month == 13
    n_year += 1
    n_month = 1
  end
  [n_year, n_month]
end

#prev_month_arrayObject



60
61
62
63
64
65
66
67
68
# File 'app/models/cal_month.rb', line 60

def prev_month_array
  n_year  = year
  n_month = month - 1
  if n_month == 0 
    n_year -= 1
    n_month = 12
  end
  [n_year, n_month]
end

#to_aObject



46
47
48
# File 'app/models/cal_month.rb', line 46

def to_a
  [year, month]
end

#to_dateObject



70
71
72
# File 'app/models/cal_month.rb', line 70

def to_date
  Date.new(year, month)
end

#update_event_data(new_data) ⇒ Object



90
91
92
93
94
95
96
# File 'app/models/cal_month.rb', line 90

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