Class: SimpleCalendar::Calendar
- Inherits:
-
Object
- Object
- SimpleCalendar::Calendar
show all
- Defined in:
- lib/simple_calendar/calendar.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(view_context, options = {}) ⇒ Calendar
Returns a new instance of Calendar.
7
8
9
10
11
12
13
14
15
|
# File 'lib/simple_calendar/calendar.rb', line 7
def initialize(view_context, options={})
@view_context = view_context
@events = options.delete(:events) { [] }
@options = options
@options[:previous_link] ||= default_previous_link
@options[:header] ||=
@options[:next_link] ||= default_next_link
@options[:td] ||= default_td_classes
end
|
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
5
6
7
|
# File 'lib/simple_calendar/calendar.rb', line 5
def block
@block
end
|
#events ⇒ Object
Returns the value of attribute events.
5
6
7
|
# File 'lib/simple_calendar/calendar.rb', line 5
def events
@events
end
|
#options ⇒ Object
Returns the value of attribute options.
5
6
7
|
# File 'lib/simple_calendar/calendar.rb', line 5
def options
@options
end
|
#view_context ⇒ Object
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_range ⇒ Object
91
92
93
94
95
96
|
# File 'lib/simple_calendar/calendar.rb', line 91
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
|
79
80
81
|
# File 'lib/simple_calendar/calendar.rb', line 79
def
->(start_date) { }
end
|
#default_next_link ⇒ Object
83
84
85
|
# File 'lib/simple_calendar/calendar.rb', line 83
def default_next_link
->(param, date_range) { link_to raw("»"), param => date_range.last + 1.day }
end
|
#default_previous_link ⇒ Object
75
76
77
|
# File 'lib/simple_calendar/calendar.rb', line 75
def default_previous_link
->(param, date_range) { link_to raw("«"), param => date_range.first - 1.day }
end
|
#default_td_classes ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/simple_calendar/calendar.rb', line 98
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
|
#events_for_date(current_date) ⇒ Object
65
66
67
68
69
70
71
72
73
|
# File 'lib/simple_calendar/calendar.rb', line 65
def events_for_date(current_date)
if events.any? && events.first.respond_to?(:simple_calendar_start_time)
events.select do |e|
current_date == e.send(:simple_calendar_start_time).to_date
end.sort_by(&:simple_calendar_start_time)
else
events
end
end
|
#get_option(name, *params) ⇒ Object
115
116
117
118
119
120
121
122
123
|
# File 'lib/simple_calendar/calendar.rb', line 115
def get_option(name, *params)
option = options[name]
case option
when Hash
option
else
option.call(*params) if option.respond_to? :call
end
end
|
#param_name ⇒ Object
61
62
63
|
# File 'lib/simple_calendar/calendar.rb', line 61
def param_name
@param_name ||= options.fetch(:param_name, :start_date)
end
|
#render(block) ⇒ Object
17
18
19
20
21
22
23
24
|
# File 'lib/simple_calendar/calendar.rb', line 17
def render(block)
@block = block
capture do
concat
concat render_table
end
end
|
26
27
28
29
30
31
32
33
34
|
# File 'lib/simple_calendar/calendar.rb', line 26
def
capture do
content_tag :div do
concat get_option(:previous_link, param_name, date_range)
concat get_option(:header, start_date)
concat get_option(:next_link, param_name, date_range)
end
end
end
|
#render_table ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/simple_calendar/calendar.rb', line 36
def render_table
content_tag :table, get_option(:table) do
content_tag :tbody, get_option(:tbody) do
render_weeks
end
end
end
|
#render_week(week) ⇒ Object
52
53
54
55
56
57
58
59
|
# File 'lib/simple_calendar/calendar.rb', line 52
def render_week(week)
results = week.map do |day|
content_tag :td, get_option(:td, start_date, day) do
block.call day, events_for_date(day)
end
end
safe_join results
end
|
#render_weeks ⇒ Object
44
45
46
47
48
49
50
|
# File 'lib/simple_calendar/calendar.rb', line 44
def render_weeks
capture do
date_range.each_slice(7) do |week|
concat content_tag(:tr, render_week(week), get_option(:tr, week))
end
end
end
|
#start_date ⇒ Object
87
88
89
|
# File 'lib/simple_calendar/calendar.rb', line 87
def start_date
@start_date ||= (params[param_name] ||Time.zone.now).to_date
end
|