class Candl::MonthModel

Attributes

api_key[RW]
calendar_id[RW]
days_shift_coefficient[RW]
delta_start_of_weekday_from_sunday[RW]
google_calendar_base_path[RW]
grouped_events[RW]
grouped_multiday_events[RW]
initialization_successful[RW]

Attributes one needs to access from the “outside”

maps_query_host[RW]
maps_query_parameter[RW]
summary_teaser_length[RW]
view_dates[RW]

Public Class Methods

current_month_end(current_shift_factor, today_date = Date.today) click to toggle source

get date of current months end

# 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) click to toggle source

get date of current months start

# 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) click to toggle source

helps apply styling to a special date

# 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) click to toggle source

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)

# 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) click to toggle source

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

# 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
new(config, current_shift_factor, date_today = Date.today) click to toggle source

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 \
} \

}

# 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
summary_title(event) click to toggle source

build a short event summary (for popups etc.)

# 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) click to toggle source

how many weeks are within this months view dates

# 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

Private Class Methods

group_events(events, from, to) click to toggle source

gets events within a day grouped by day

# File lib/candl/month_model.rb, line 191
def self.group_events(events, from, to)
  events.select{ |event| (event.dtstart.to_date == event.dtend.to_date) }.sort_by{ |event| event.dtstart.localtime }.group_by{ |event| event.dtstart.to_date }
end
group_multiday_events(events, view_dates) click to toggle source

gets events that are multiple day's long grouped by the week

# File lib/candl/month_model.rb, line 196
def self.group_multiday_events(events, view_dates)
  multiday_events = events.select { |event| event.dtstart.to_date != event.dtend.to_date }

  grouped_multiday_events = []

  weeks_in_months_view_dates(view_dates).times do |week|
    first_weekday = view_dates[7 * week]
    last_weekday = view_dates[7 * week + 6]

    weeks_events = multiday_events.select{ |event| event.dtend > first_weekday && event.dtstart < last_weekday.next_day }

    grouped_multiday_events[week] = weeks_events
  end

  grouped_multiday_events
end

Public Instance Methods

address_to_maps_path(address) click to toggle source

build a google maps path from the adress details

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source
# 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 = {}) click to toggle source

builds base path of current view

# 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) click to toggle source

builds path to previous/upcoming month

# 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) click to toggle source
# 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) click to toggle source

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

# 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