Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (40 sloc) 1.23 KB
class Calendar
attr_reader :date
attr_writer :days
def initialize(year_or_date = nil, month = nil)
@date = if [year_or_date, month].none?
Date.current
elsif year_or_date.is_a?(Date) && month.nil?
year_or_date
else
Date.new(year_or_date.to_i, (month ? month.to_i : 1))
end
end
def days
unless defined?(@days)
@days = []
date.beginning_of_month.wday.times { |i| @days << date.beginning_of_month - date.beginning_of_month.wday + i }
@days.push *date.beginning_of_month..date.end_of_month # would be nice if we could do @date.all_month, but that's only on the Time class in 3.1
(6-date.end_of_month.wday).times { |i| @days << Date.new(date.next_month.year, date.next_month.month, i + 1) }
end
return @days
end
# This is used to allow us to do:
#
# render @calendar
#
# instead of:
#
# render 'calendar', object: @calendar
def to_partial_path
'events/calendar'
end
def self.week_of(date)
object = new(date)
object.days = object.days[object.days.index(object.date.beginning_of_week(:sunday))..object.days.index(object.date.end_of_week(:sunday))]
object
end
def method_missing(method, *args)
self.date.send(method, *args)
end
end