Class: Candl::MonthModel

Inherits:
Object
  • Object
show all
Defined in:
lib/candl/month_model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, current_shift_factor, date_today = Date.today) ⇒ MonthModel

Minimal json conifg for month_model example: config = { \

calendar: { \
  google_calendar_api_host_base_path: "https://www.googleapis.com/calendar/v3/calendars/", \
  calendar_id: "schau-hnh%40web.de", \
  api_key: "AIzaSyB5F1X5hBi8vPsmt1itZTpMluUAjytf6hI" \
}, \
agenda: { \
  display_day_count: 14, \
  days_shift_coefficient: 7 \
}, \
month: { \
  summary_teaser_length_in_characters: 42, \
  delta_start_of_weekday_from_sunday: 1 \
}, \
general: { \
  maps_query_host: "https://www.google.de/maps", \
  maps_query_parameter: "q", \
  cache_update_interval_in_s: 7200 \
} \

}



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/candl/month_model.rb', line 34

def initialize(config, current_shift_factor, date_today = Date.today)
  self.google_calendar_base_path = config[:calendar][:google_calendar_api_host_base_path]
  self.calendar_id = config[:calendar][:calendar_id]
  self.api_key = config[:calendar][:api_key]

  self.summary_teaser_length = config[:month][:summary_teaser_length_in_characters]
  self.delta_start_of_weekday_from_sunday = config[:month][:delta_start_of_weekday_from_sunday]
  self.days_shift_coefficient = config[:agenda][:days_shift_coefficient]

  self.maps_query_host = config[:general][:maps_query_host]
  self.maps_query_parameter = config[:general][:maps_query_parameter]

  date_month_start = MonthModel.current_month_start(current_shift_factor, date_today)
  date_month_end = MonthModel.current_month_end(current_shift_factor, date_today)

  self.view_dates = generate_months_view_dates(date_month_start, date_month_end)

  calendar_adress = { path: google_calendar_base_path, id: calendar_id, key: api_key }
  result = EventLoaderModel.get_events(calendar_adress, view_dates.first, view_dates.last, :month)

  events = result[:events]
  self.initialization_successful = result[:success]

  self.grouped_events = MonthModel::group_events(events, view_dates.first, view_dates.last)
  self.grouped_multiday_events = MonthModel::group_multiday_events(events, view_dates)
end

Instance Attribute Details

#delta_start_of_weekday_from_sundayObject

Returns the value of attribute delta_start_of_weekday_from_sunday.



6
7
8
# File 'lib/candl/month_model.rb', line 6

def delta_start_of_weekday_from_sunday
  @delta_start_of_weekday_from_sunday
end

#grouped_eventsObject

Returns the value of attribute grouped_events.



10
11
12
# File 'lib/candl/month_model.rb', line 10

def grouped_events
  @grouped_events
end

#grouped_multiday_eventsObject

Returns the value of attribute grouped_multiday_events.



11
12
13
# File 'lib/candl/month_model.rb', line 11

def grouped_multiday_events
  @grouped_multiday_events
end

#initialization_successfulObject

Attributes one needs to access from the “outside”



4
5
6
# File 'lib/candl/month_model.rb', line 4

def initialization_successful
  @initialization_successful
end

#summary_teaser_lengthObject

Returns the value of attribute summary_teaser_length.



7
8
9
# File 'lib/candl/month_model.rb', line 7

def summary_teaser_length
  @summary_teaser_length
end

#view_datesObject

Returns the value of attribute view_dates.



9
10
11
# File 'lib/candl/month_model.rb', line 9

def view_dates
  @view_dates
end

Class Method Details

.current_month_end(current_shift_factor, today_date = Date.today) ⇒ Object

get date of current months end



171
172
173
# File 'lib/candl/month_model.rb', line 171

def self.current_month_end(current_shift_factor, today_date = Date.today)
  (today_date + (current_shift_factor.to_i).month).end_of_month
end

.current_month_start(current_shift_factor, today_date = Date.today) ⇒ Object

get date of current months start



166
167
168
# File 'lib/candl/month_model.rb', line 166

def self.current_month_start(current_shift_factor, today_date = Date.today)
  (today_date + (current_shift_factor.to_i).month).beginning_of_month
end

.emphasize_date(check_date, emphasized_date, emphasized, regular) ⇒ Object

helps apply styling to a special date



105
106
107
# File 'lib/candl/month_model.rb', line 105

def self.emphasize_date(check_date, emphasized_date, emphasized, regular)
  check_date.to_date == emphasized_date.to_date ? emphasized : regular
end

.find_best_fit_for_day(first_weekday, day, event_heap) ⇒ Object

finds the best event, among those multiday events within a week-group, for the current day (the algorithm will find the longest events first to display them above shorter multiday events)



62
63
64
65
66
67
# File 'lib/candl/month_model.rb', line 62

def self.find_best_fit_for_day(first_weekday, day, event_heap)
  best_fit = event_heap.select{ |event|
    (day == first_weekday ?
      (event.dtstart.to_date <= day.to_date && event.dtend.to_date >= day.to_date) :
      (event.dtstart.to_date == day.to_date)) }.sort_by{ |event| [event.dtstart.to_time.to_i, -event.dtend.to_time.to_i] }.first
end

.multiday_event_cutoff(start_end_conditions, cutoff_styles) ⇒ Object

depending on the cutoff conditions this will apply a cutoff style to the start of the event, the end of it, both ends or neither



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/candl/month_model.rb', line 110

def self.multiday_event_cutoff(start_end_conditions, cutoff_styles)
  if (start_end_conditions[:start] && start_end_conditions[:end])
    cutoff_styles[:both]
  elsif (start_end_conditions[:start])
    cutoff_styles[:start]
  elsif (start_end_conditions[:end])
    cutoff_styles[:end]
  else
    ''
  end
end

.summary_title(event) ⇒ Object

build a short event summary (for popups etc.)



123
124
125
# File 'lib/candl/month_model.rb', line 123

def self.summary_title(event)
  event.summary.to_s.force_encoding("UTF-8") + "\n" + event.location.to_s.force_encoding("UTF-8") + "\n" + event.description.to_s.force_encoding("UTF-8")
end

.weeks_in_months_view_dates(months_view_dates) ⇒ Object

how many weeks are within this months view dates



161
162
163
# File 'lib/candl/month_model.rb', line 161

def self.weeks_in_months_view_dates(months_view_dates)
  months_view_dates.length.div 7
end

Instance Method Details

#address_to_maps_path(address) ⇒ Object

build a google maps path from the adress details



128
129
130
# File 'lib/candl/month_model.rb', line 128

def address_to_maps_path(address)
  ActionDispatch::Http::URL.path_for path: maps_query_host, params: Hash[maps_query_parameter.to_s, address.force_encoding("UTF-8").gsub(" ", "+")]
end

#current_shift_for_agenda(current_shift_factor) ⇒ Object

current shift factor for switching between calendar views from month to agenda



88
89
90
91
92
93
94
95
96
97
# File 'lib/candl/month_model.rb', line 88

def current_shift_for_agenda(current_shift_factor)
  today_date = Date.today
  current_shift_in_days = (MonthModel.current_month_start(current_shift_factor, today_date) - today_date).to_i

  current_shift_in_days = (MonthModel.current_month_start(current_shift_factor, today_date) + ((MonthModel.current_month_end(current_shift_factor, today_date) - MonthModel.current_month_start(current_shift_factor, today_date)).div 5) - today_date).to_i

  current_shift_factor_for_agenda = (current_shift_in_days.div days_shift_coefficient)

  current_shift_factor_for_agenda
end

#current_shift_for_month(current_shift_factor) ⇒ Object

current shift factor for switching between calendar views from month to month



100
101
102
# File 'lib/candl/month_model.rb', line 100

def current_shift_for_month(current_shift_factor)
  current_shift_factor
end

#generate_months_view_dates(date_month_start, date_month_end) ⇒ Object

generates all needed dates within the start and the end of a month



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/candl/month_model.rb', line 143

def generate_months_view_dates(date_month_start, date_month_end)
  dates_in_month_view = []
  ((date_month_start.wday - delta_start_of_weekday_from_sunday) % 7).times do |day|
    dates_in_month_view = dates_in_month_view + [(date_month_start - (((date_month_start.wday - delta_start_of_weekday_from_sunday) % 7) - day))]
  end

  date_month_end.day.times do |day|
    dates_in_month_view = dates_in_month_view + [date_month_start + day]
  end

  (6 - date_month_end.wday + delta_start_of_weekday_from_sunday).times do |day|
    dates_in_month_view = dates_in_month_view + [date_month_end + day + 1]
  end

  dates_in_month_view
end

#month_shift_path(page_path, current_shift_factor, shift_factor) ⇒ Object



83
84
85
# File 'lib/candl/month_model.rb', line 83

def month_shift_path(page_path, current_shift_factor, shift_factor)
  path(page_path, s: (current_shift_factor.to_i + shift_factor.to_i).to_s)
end

#path(page_path, params = {}) ⇒ Object

builds base path of current view



70
71
72
# File 'lib/candl/month_model.rb', line 70

def path(page_path, params = {})
  ActionDispatch::Http::URL.path_for path: page_path, params: {v: 'm'}.merge(params)
end

#previous_path(page_path, current_shift_factor) ⇒ Object

builds path to previous/upcoming month



75
76
77
# File 'lib/candl/month_model.rb', line 75

def previous_path(page_path, current_shift_factor)
  month_shift_path(page_path, current_shift_factor, -1)
end

#upcoming_path(page_path, current_shift_factor) ⇒ Object



79
80
81
# File 'lib/candl/month_model.rb', line 79

def upcoming_path(page_path, current_shift_factor)
  month_shift_path(page_path, current_shift_factor, 1)
end

#weekday_dates(today_date = Date.today) ⇒ Object

will generate the dates of a whole week around the date given (starting from the configured day)



133
134
135
136
137
138
139
140
# File 'lib/candl/month_model.rb', line 133

def weekday_dates(today_date = Date.today)
  weekdays_dates = []
  first_day_of_week = today_date - (today_date.wday - delta_start_of_weekday_from_sunday)
  7.times do |day|
    weekdays_dates += [first_day_of_week + day]
  end
  weekdays_dates
end