Class: Candl::AgendaModel

Inherits:
Object
  • Object
show all
Defined in:
lib/candl/agenda_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) ⇒ AgendaModel

Minimal json conifg for agenda_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
# File 'lib/candl/agenda_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] ||= "https://www.googleapis.com/calendar/v3/calendars/"
  self.calendar_id = config[:calendar][:calendar_id]
  self.api_key = config[:calendar][:api_key]

  self.display_day_count = config[:agenda][:display_day_count] ||= 14
  self.days_shift_coefficient = config[:agenda][:days_shift_coefficient] ||= 7

  self.maps_query_host = config[:general][:maps_query_host] ||= "https://www.google.de/maps"
  self.maps_query_parameter = config[:general][:maps_query_parameter] ||= "q"

  from = current_start_date(current_shift_factor, date_today)
  to = current_end_date(current_shift_factor, date_today)

  calendar_adress = { path: google_calendar_base_path, id: calendar_id, key: api_key }
  result = EventLoaderModel.get_events(calendar_adress, from, to, :agenda)

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

  self.agenda_grouped_events = get_days_grouped_events(events)
end

Instance Attribute Details

#agenda_grouped_eventsObject

Returns the value of attribute agenda_grouped_events.



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

def agenda_grouped_events
  @agenda_grouped_events
end

#days_shift_coefficientObject

Returns the value of attribute days_shift_coefficient.



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

def days_shift_coefficient
  @days_shift_coefficient
end

#display_day_countObject

Returns the value of attribute display_day_count.



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

def display_day_count
  @display_day_count
end

#initialization_successfulObject

Attributes one needs to access from the “outside”



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

def initialization_successful
  @initialization_successful
end

Class Method Details

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

helps apply styling to a special date



99
100
101
# File 'lib/candl/agenda_model.rb', line 99

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

.summary_title(event) ⇒ Object

build a short event summary (for popups etc.)



104
105
106
# File 'lib/candl/agenda_model.rb', line 104

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

Instance Method Details

#address_to_maps_path(address) ⇒ Object

build a google maps path from the adress details



109
110
111
# File 'lib/candl/agenda_model.rb', line 109

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_end_date(current_shift_factor, today_date = Date.today) ⇒ Object

date of current end day of agenda



94
95
96
# File 'lib/candl/agenda_model.rb', line 94

def current_end_date(current_shift_factor, today_date = Date.today)
  today_date + (current_shift_factor.to_i * days_shift_coefficient + display_day_count).days
end

#current_shift_for_agenda(current_shift_factor) ⇒ Object

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



74
75
76
# File 'lib/candl/agenda_model.rb', line 74

def current_shift_for_agenda(current_shift_factor)
  current_shift_factor
end

#current_shift_for_month(current_shift_factor, today_date = Date.today) ⇒ Object

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



79
80
81
82
83
84
85
86
# File 'lib/candl/agenda_model.rb', line 79

def current_shift_for_month(current_shift_factor, today_date = Date.today)
  date_span = (current_end_date(current_shift_factor, today_date) - current_start_date(current_shift_factor, today_date)).to_i
  midway_date = (current_start_date(current_shift_factor, today_date) + (date_span / 2))

  current_month_shift = ((midway_date.year * 12 + midway_date.month) - (today_date.year * 12 + today_date.month)).to_i

  current_month_shift
end

#current_start_date(current_shift_factor, today_date = Date.today) ⇒ Object

date of current start day of agenda



89
90
91
# File 'lib/candl/agenda_model.rb', line 89

def current_start_date(current_shift_factor, today_date = Date.today)
  today_date + (current_shift_factor.to_i * days_shift_coefficient).days
end

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

builds base path of current view



56
57
58
# File 'lib/candl/agenda_model.rb', line 56

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

#previous_path(page_path, current_shift_factor) ⇒ Object

builds path to previous/upcoming week



61
62
63
# File 'lib/candl/agenda_model.rb', line 61

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

#upcoming_path(page_path, current_shift_factor) ⇒ Object



65
66
67
# File 'lib/candl/agenda_model.rb', line 65

def upcoming_path(page_path, current_shift_factor)
  week_shift_path(page_path, current_shift_factor, +1)
end

#week_shift_path(page_path, current_shift_factor, shift_factor) ⇒ Object



69
70
71
# File 'lib/candl/agenda_model.rb', line 69

def week_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