Module: Ecm::CalendarHelper

Defined in:
lib/ecm/calendar_helper/engine.rb,
app/helpers/ecm/calendar_helper.rb,
lib/ecm/calendar_helper/version.rb,
app/concerns/ecm/calendar_helper/controller/calendar_concern.rb,
lib/generators/ecm/calendar_helper/controller/controller_generator.rb

Defined Under Namespace

Modules: Controller, Generators Classes: Engine

Constant Summary collapse

VERSION =
"1.0.1"

Instance Method Summary collapse

Instance Method Details

#month_calendar(date = Time.zone.now.to_date, elements = [], options = {}) ⇒ Object

renders a calendar table

Example with elements that span more than 1 day:

= month_calendar @date, @reservations, start_date_method: :start_at, end_date_method: :end_at

Example of using a lamda as display method:

= month_calendar(display_method: ->(context, resource) { context.link_to(resource.booker.human, resource.booker) })


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
60
61
62
# File 'app/helpers/ecm/calendar_helper.rb', line 13

def month_calendar(date = Time.zone.now.to_date, elements = [], options = {})
  options.reverse_merge! :date_method => :start_at, :display_method => :to_s, :link_elements => true, :start_day => :sunday, start_date_method: nil, end_date_method: nil

  display_method = options.delete(:display_method)
  link_elements  = options.delete(:link_elements)

  start_date_method = options.delete(:start_date_method)
  end_date_method   = options.delete(:end_date_method)
  
  # calculate beginning and end of month
  beginning_of_month = date.beginning_of_month.to_date
  end_of_month = date.end_of_month.to_date

  # Get localized day names
  localized_day_names = I18n.t('date.abbr_day_names').dup

  # Shift day names to suite start day
  english_day_names = [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday]

  # Raise an exception if the passed start day is not an english day name
  raise ":start_day option for month_calendar must be in: #{english_day_names.join(', ')}, but you passed: #{options[:start_day].to_s} (class: #{options[:start_day].class.to_s})" unless english_day_names.include?(options[:start_day])

  # Get the offset of start day
  offset = english_day_names.index(options[:start_day])
  last_day_of_week =  Time.zone.now.end_of_week.wday


  # Change calendar heading if offset is not 0 (offset 0 means sunday is the first day of the week)
  offset.times do 
    localized_day_names.push(localized_day_names.shift)
  end

  days_by_week = {}
  first_day = beginning_of_month.beginning_of_week
  last_day = end_of_month.end_of_week

  days = (first_day..last_day).each_with_object({}).with_index do |(day, memo), index|
    memo[day.to_date] = elements.find_all do |e|
      if start_date_method.present? && end_date_method.present?
        day.to_date.between?(e.send(start_date_method), e.send(end_date_method))
      else
        e.send(options[:date_method]).to_date == day.to_date
      end
    end || {}
  end

  days_by_week = days.each_with_object({}) { |(k, v), m| (m[k.cweek] ||= {})[k] = v }

  render partial: 'ecm/calendar_helper/month_calendar', locals: { localized_day_names: localized_day_names, days_by_week: days_by_week, display_method: display_method, link_elements: link_elements }
end

#month_calendar_pagination(date, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/ecm/calendar_helper.rb', line 64

def month_calendar_pagination(date, options = {})
  options.reverse_merge!(show_today_link: true, remote: false)

  show_today_link = options.delete(:show_today_link)
  remote          = options.delete(:remote)

  actual_month   = Time.zone.now.beginning_of_month.to_date
  previous_month = date.beginning_of_month.to_date - 1.month
  next_month     = date.beginning_of_month.to_date + 1.months

  render partial: 'ecm/calendar_helper/month_calendar_pagination', locals: { actual_month: actual_month, previous_month: previous_month, next_month: next_month, show_today_link: show_today_link, date: date, remote: remote }
end