Class: SimpleCalendar::Calendar
- Inherits:
-
Object
- Object
- SimpleCalendar::Calendar
show all
- Defined in:
- lib/simple_calendar/calendar.rb
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, options = {}, &block) ⇒ Calendar
Returns a new instance of Calendar.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/simple_calendar/calendar.rb', line 9
def initialize(view_context, options = {}, &block)
@view_context = view_context
@locals = options.delete(:locals) || {}
@options = options
@block = block
@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)
@params.merge!(@options.fetch(:params, {}))
end
|
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
7
8
9
|
# File 'lib/simple_calendar/calendar.rb', line 7
def options
@options
end
|
#view_context ⇒ Object
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
#additional_days ⇒ Object
135
136
137
|
# File 'lib/simple_calendar/calendar.rb', line 135
def additional_days
options.fetch(:number_of_days, 4) - 1
end
|
#attribute ⇒ Object
86
87
88
|
# File 'lib/simple_calendar/calendar.rb', line 86
def attribute
options.fetch(:attribute, :start_time).to_sym
end
|
#date_range ⇒ Object
78
79
80
|
# File 'lib/simple_calendar/calendar.rb', line 78
def date_range
(start_date..(start_date + additional_days.days)).to_a
end
|
#end_attribute ⇒ Object
90
91
92
|
# File 'lib/simple_calendar/calendar.rb', line 90
def end_attribute
options.fetch(:end_attribute, :end_time).to_sym
end
|
#end_date ⇒ Object
131
132
133
|
# File 'lib/simple_calendar/calendar.rb', line 131
def end_date
date_range.last
end
|
#group_events_by_date(events) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/simple_calendar/calendar.rb', line 109
def group_events_by_date(events)
events_grouped_by_date = Hash.new { |h, k| h[k] = [] }
events.each do |event|
event_start_date = event.send(attribute).to_date
event_end_date = (event.respond_to?(end_attribute) && !event.send(end_attribute).nil?) ? event.send(end_attribute).to_date : event_start_date
(event_start_date..event_end_date.to_date).each do |enumerated_date|
events_grouped_by_date[enumerated_date] << event
end
end
events_grouped_by_date
end
|
#locals(&block) ⇒ Object
31
32
33
34
35
36
37
38
39
|
# File 'lib/simple_calendar/calendar.rb', line 31
def locals(&block)
@locals.merge(
passed_block: @block,
calendar: self,
date_range: date_range,
start_date: start_date,
sorted_events: sorted_events
)
end
|
#partial_name ⇒ Object
82
83
84
|
# File 'lib/simple_calendar/calendar.rb', line 82
def partial_name
@options[:partial] || self.class.name.underscore
end
|
#render_in(view_context) ⇒ Object
24
25
26
27
28
29
|
# File 'lib/simple_calendar/calendar.rb', line 24
def render_in(view_context)
view_context.render(
partial: partial_name,
locals: locals
)
end
|
#sorted_events ⇒ Object
98
99
100
101
102
103
|
# File 'lib/simple_calendar/calendar.rb', line 98
def sorted_events
@sorted_events ||= begin
events = options.fetch(:events, []).reject { |e| e.send(attribute).nil? }.sort_by(&attribute)
group_events_by_date(events)
end
end
|
#sorted_events_for(day) ⇒ Object
105
106
107
|
# File 'lib/simple_calendar/calendar.rb', line 105
def sorted_events_for(day)
sorted_events.fetch(day, [])
end
|
#start_date ⇒ Object
123
124
125
126
127
128
129
|
# File 'lib/simple_calendar/calendar.rb', line 123
def start_date
if options.has_key?(:start_date)
options.fetch(:start_date).to_date
else
view_context.params.fetch(start_date_param, Date.current).to_date
end
end
|
#start_date_param ⇒ Object
94
95
96
|
# File 'lib/simple_calendar/calendar.rb', line 94
def start_date_param
options.fetch(:start_date_param, :start_date).to_sym
end
|
#td_classes_for(day) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/simple_calendar/calendar.rb', line 41
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
58
59
60
61
62
63
64
|
# File 'lib/simple_calendar/calendar.rb', line 58
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_view ⇒ Object
66
67
68
|
# File 'lib/simple_calendar/calendar.rb', line 66
def url_for_next_view
view_context.url_for(@params.merge(start_date_param => (date_range.last + 1.day).iso8601))
end
|
#url_for_previous_view ⇒ Object
70
71
72
|
# File 'lib/simple_calendar/calendar.rb', line 70
def url_for_previous_view
view_context.url_for(@params.merge(start_date_param => (date_range.first - date_range.count.days).iso8601))
end
|
#url_for_today_view ⇒ Object
74
75
76
|
# File 'lib/simple_calendar/calendar.rb', line 74
def url_for_today_view
view_context.url_for(@params.merge(start_date_param => Time.current.to_date.iso8601))
end
|