Class: SimpleCalendar::Calendar

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

Direct Known Subclasses

MonthCalendar, WeekCalendar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Calendar.



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

def initialize(view_context, opts={})
  @view_context = view_context
  @events       = opts.delete(:events) { [] }
  @timezone     = opts.fetch(:timezone, Time.zone)

  opts.reverse_merge!(
    header: {class: "calendar-header"},
    previous_link: default_previous_link,
    title: default_title,
    next_link: default_next_link,
    td: default_td_classes,
    thead: default_thead,
  )

  @options      = opts
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



5
6
7
# File 'lib/simple_calendar/calendar.rb', line 5

def block
  @block
end

#eventsObject (readonly)

Returns the value of attribute events.



5
6
7
# File 'lib/simple_calendar/calendar.rb', line 5

def events
  @events
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/simple_calendar/calendar.rb', line 5

def options
  @options
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



5
6
7
# File 'lib/simple_calendar/calendar.rb', line 5

def view_context
  @view_context
end

Instance Method Details

#date_rangeObject



113
114
115
116
117
118
# File 'lib/simple_calendar/calendar.rb', line 113

def date_range
  @date_range ||= begin
                    number_of_days = options.fetch(:number_of_days, 4) - 1
                    start_date..(start_date + number_of_days.days)
                  end
end


91
92
93
# File 'lib/simple_calendar/calendar.rb', line 91

def default_next_link
  ->(param, date_range) { link_to raw("»"), param => date_range.last + 1.day }
end


83
84
85
# File 'lib/simple_calendar/calendar.rb', line 83

def default_previous_link
  ->(param, date_range) { link_to raw("«"), param => date_range.first - 1.day }
end

#default_td_classesObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/simple_calendar/calendar.rb', line 120

def default_td_classes
  ->(start_date, current_calendar_date) {
    today = Time.zone.now.to_date
    td_class = ["day"]

    td_class << "today"  if today == current_calendar_date
    td_class << "past"   if today > current_calendar_date
    td_class << "future" if today < current_calendar_date
    td_class << "prev-month"    if start_date.month != current_calendar_date.month && current_calendar_date < start_date
    td_class << "next-month"    if start_date.month != current_calendar_date.month && current_calendar_date > start_date
    td_class << "current-month" if start_date.month == current_calendar_date.month
    td_class << "wday-#{current_calendar_date.wday.to_s}"

    { class: td_class.join(" ") }
  }
end

#default_theadObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/simple_calendar/calendar.rb', line 95

def default_thead
  ->(dates) {
    (:thead) do
      (:tr) do
        capture do
          dates.each do |date|
            concat (:th, I18n.t(options.fetch(:day_names, "date.abbr_day_names"))[date.wday])
          end
        end
      end
    end
  }
end

#default_titleObject



87
88
89
# File 'lib/simple_calendar/calendar.rb', line 87

def default_title
  ->(start_date) {  }
end

#events_for_date(current_date) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/simple_calendar/calendar.rb', line 73

def events_for_date(current_date)
  if events.any? && events.first.respond_to?(:simple_calendar_start_time)
    events.select do |e|
      current_date == @timezone.utc_to_local(e.send(:simple_calendar_start_time).beginning_of_day).to_date
    end.sort_by(&:simple_calendar_start_time)
  else
    events
  end
end

#get_option(name, *params) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/simple_calendar/calendar.rb', line 137

def get_option(name, *params)
  option = options[name]
  case option
  when Hash
    option
  else
    option.respond_to?(:call) ? option.call(*params) : option
  end
end

#param_nameObject



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

def param_name
  @param_name ||= options.fetch(:param_name, :start_date)
end

#render(block) ⇒ Object



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

def render(block)
  @block = block

  capture do
    concat render_header
    concat render_table
  end
end

#render_headerObject



33
34
35
36
37
38
39
40
41
# File 'lib/simple_calendar/calendar.rb', line 33

def render_header
  capture do
     :header, get_option(:header) do
      concat get_option(:previous_link, param_name, date_range)
      concat get_option(:title, start_date)
      concat get_option(:next_link, param_name, date_range)
    end
  end
end

#render_tableObject



43
44
45
46
47
48
49
50
# File 'lib/simple_calendar/calendar.rb', line 43

def render_table
   :table, get_option(:table)  do
    capture do
      concat get_option(:thead, date_range.to_a.slice(0, 7))
      concat (:tbody, render_weeks, get_option(:tbody))
    end
  end
end

#render_week(week) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/simple_calendar/calendar.rb', line 60

def render_week(week)
  results = week.map do |day|
     :td, get_option(:td, start_date, day) do
      block.call(day, events_for_date(day))
    end
  end
  safe_join results
end

#render_weeksObject



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

def render_weeks
  capture do
    date_range.each_slice(7) do |week|
      concat (:tr, render_week(week), get_option(:tr, week))
    end
  end
end

#start_dateObject



109
110
111
# File 'lib/simple_calendar/calendar.rb', line 109

def start_date
  @start_date ||= (get_option(:start_date) || params[param_name] || Time.zone.now).to_date
end