Class: SimpleCalendar::Calendar

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_calendar/calendar.rb

Direct Known Subclasses

MonthCalendar, WeekCalendar

Constant Summary collapse

PARAM_KEY_BLACKLIST =
:authenticity_token, :commit, :utf8, :_method, :script_name

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context, opts = {}) ⇒ Calendar

Returns a new instance of Calendar.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/simple_calendar/calendar.rb', line 9

def initialize(view_context, opts = {})
  @view_context = view_context
  @options = opts

  # Next and previous view links should use the same params as the current view
  @params = @view_context.respond_to?(:params) ? @view_context.params : {}
  @params = @params.to_unsafe_h if @params.respond_to?(:to_unsafe_h)
  @params = @params.with_indifferent_access.except(*PARAM_KEY_BLACKLIST)

  # Add in any additional params the user passed in
  @params.merge!(@options.fetch(:params, {}))
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/simple_calendar/calendar.rb', line 7

def options
  @options
end

#view_contextObject

Returns the value of attribute view_context.



7
8
9
# File 'lib/simple_calendar/calendar.rb', line 7

def view_context
  @view_context
end

Instance Method Details

#date_rangeObject



68
69
70
# File 'lib/simple_calendar/calendar.rb', line 68

def date_range
  (start_date..(start_date + additional_days.days)).to_a
end

#render(&block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/simple_calendar/calendar.rb', line 22

def render(&block)
  view_context.render(
    partial: partial_name,
    locals: {
      passed_block: block,
      calendar: self,
      date_range: date_range,
      start_date: start_date,
      sorted_events: sorted_events
    }
  )
end

#td_classes_for(day) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/simple_calendar/calendar.rb', line 35

def td_classes_for(day)
  today = Date.current

  td_class = ["day"]
  td_class << "wday-#{day.wday}"
  td_class << "today" if today == day
  td_class << "past" if today > day
  td_class << "future" if today < day
  td_class << "start-date" if day.to_date == start_date.to_date
  td_class << "prev-month" if start_date.month != day.month && day < start_date
  td_class << "next-month" if start_date.month != day.month && day > start_date
  td_class << "current-month" if start_date.month == day.month
  td_class << "has-events" if sorted_events.fetch(day, []).any?

  td_class
end

#tr_classes_for(week) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/simple_calendar/calendar.rb', line 52

def tr_classes_for(week)
  today = Date.current
  tr_class = ["week"]
  tr_class << "current-week" if week.include?(today)

  tr_class
end

#url_for_next_viewObject



60
61
62
# File 'lib/simple_calendar/calendar.rb', line 60

def url_for_next_view
  view_context.url_for(@params.merge(start_date_param => (date_range.last + 1.day).iso8601))
end

#url_for_previous_viewObject



64
65
66
# File 'lib/simple_calendar/calendar.rb', line 64

def url_for_previous_view
  view_context.url_for(@params.merge(start_date_param => (date_range.first - 1.day).iso8601))
end