Class: Candle::MonthModel

Inherits:
Object
  • Object
show all
Defined in:
lib/candle/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 \
} \

}



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

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]
  self.cache_update_interval_in_ms = config[:general][:cache_update_interval_in_ms]

  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)

  events = get_month_events(view_dates.first, view_dates.last)

  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

Attributes one needs to access from the “outside”



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

def delta_start_of_weekday_from_sunday
  @delta_start_of_weekday_from_sunday
end

#grouped_eventsObject

Returns the value of attribute grouped_events.



8
9
10
# File 'lib/candle/month_model.rb', line 8

def grouped_events
  @grouped_events
end

#grouped_multiday_eventsObject

Returns the value of attribute grouped_multiday_events.



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

def grouped_multiday_events
  @grouped_multiday_events
end

#summary_teaser_lengthObject

Returns the value of attribute summary_teaser_length.



5
6
7
# File 'lib/candle/month_model.rb', line 5

def summary_teaser_length
  @summary_teaser_length
end

#view_datesObject

Returns the value of attribute view_dates.



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

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



165
166
167
# File 'lib/candle/month_model.rb', line 165

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



160
161
162
# File 'lib/candle/month_model.rb', line 160

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



98
99
100
# File 'lib/candle/month_model.rb', line 98

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)



58
59
60
# File 'lib/candle/month_model.rb', line 58

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

.multiday_event_cutoff(cutoff_start_condition, cutoff_end_condition, cutoff_start_style, cutoff_both_style, cutoff_end_style) ⇒ 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



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/candle/month_model.rb', line 103

def self.multiday_event_cutoff(cutoff_start_condition, cutoff_end_condition, cutoff_start_style, cutoff_both_style, cutoff_end_style)
  if (cutoff_start_condition && cutoff_end_condition)
    cutoff_both_style
  elsif (cutoff_start_condition)
    cutoff_start_style
  elsif (cutoff_end_condition)
    cutoff_end_style
  else
    ''
  end
end

.summary_title(event) ⇒ Object

build a short event summary (for popups etc.)



116
117
118
# File 'lib/candle/month_model.rb', line 116

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



155
156
157
# File 'lib/candle/month_model.rb', line 155

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



121
122
123
124
# File 'lib/candle/month_model.rb', line 121

def address_to_maps_path(address)
  # URI::HTTP.build( host: maps_query_host, query: { maps_query_parameter: address.force_encoding("UTF-8").gsub(" ", "+") }.to_query).to_s
  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



81
82
83
84
85
86
87
88
89
90
# File 'lib/candle/month_model.rb', line 81

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



93
94
95
# File 'lib/candle/month_model.rb', line 93

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/candle/month_model.rb', line 137

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



76
77
78
# File 'lib/candle/month_model.rb', line 76

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



63
64
65
# File 'lib/candle/month_model.rb', line 63

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



68
69
70
# File 'lib/candle/month_model.rb', line 68

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



72
73
74
# File 'lib/candle/month_model.rb', line 72

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)



127
128
129
130
131
132
133
134
# File 'lib/candle/month_model.rb', line 127

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