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



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

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

  @params = @view_context.respond_to?(:params) ? @view_context.params : Hash.new
  @params = @params.to_unsafe_h if @params.respond_to?(:to_unsafe_h)
  @params = @params.with_indifferent_access.except(*PARAM_KEY_BLACKLIST)
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

#render(&block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/simple_calendar/calendar.rb', line 18

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

#td_classes_for(day) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/simple_calendar/calendar.rb', line 31

def td_classes_for(day)
  today = Time.zone.now.to_date

  td_class = ["day"]
  td_class << "wday-#{day.wday.to_s}"
  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

#url_for_next_viewObject



48
49
50
# File 'lib/simple_calendar/calendar.rb', line 48

def url_for_next_view
  view_context.url_for(@params.merge(start_date: date_range.last + 1.day))
end

#url_for_previous_viewObject



52
53
54
# File 'lib/simple_calendar/calendar.rb', line 52

def url_for_previous_view
  view_context.url_for(@params.merge(start_date: date_range.first - 1.day))
end