Skip to content

Instantly share code, notes, and snippets.

@camelpunch
Created December 13, 2010 14:56
Show Gist options
  • Select an option

  • Save camelpunch/739058 to your computer and use it in GitHub Desktop.

Select an option

Save camelpunch/739058 to your computer and use it in GitHub Desktop.
class CalendarMonth
extend ActiveModel::Naming
attr_accessor :start_time, :end_time
FORMAT = '%Y-%m'
def initialize(date_string = nil)
if date_string.blank?
now = Time.now
self.start_time = now.beginning_of_month
self.end_time = now.end_of_month
else
self.start_time = DateTime.strptime(date_string, FORMAT)
self.end_time = start_time.end_of_month
end
end
def to_param
start_time.strftime(FORMAT)
end
def previous
CalendarMonth.new((start_time.to_date << 1).strftime(FORMAT))
end
def next
CalendarMonth.new((start_time.to_date >> 1).strftime(FORMAT))
end
def days
tutorials = Tutorial.for_month(self)
(start_time.to_date..end_time.to_date).map do |date|
tutorials_this_day = tutorials.select do |tutorial|
tutorial.starts_at.to_date == date
end
CalendarDay.new(:date => date,
:tutorials => tutorials_this_day)
end
end
def to_s
I18n.localize(start_time, :format => :month)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment