Skip to content

Instantly share code, notes, and snippets.

@phillyslick
Forked from JamesDullaghan/calendar_helper.rb
Created March 21, 2014 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillyslick/9696410 to your computer and use it in GitHub Desktop.
Save phillyslick/9696410 to your computer and use it in GitHub Desktop.
module CalendarHelper
def calendar(date = Date.today, &block)
Calendar.new(self, date, block).table
end
class Calendar < Struct.new(:view, :date, :callback)
HEADER = %w[SUN MON TUE WED THU FRI SAT]
START_DAY = :sunday
delegate :content_tag, to: :view
def table
content_tag :section, role: "main", class: 'calendar' do
header + week_rows
end
end
def header
content_tag :ul, class: "day-row" do
HEADER.map { |day| content_tag :li, day, class: "day" }.join.html_safe
end
end
def week_rows
weeks.map do |week|
content_tag :ul, class: "week-row" do
week.map { |day| day_cell(day) }.join.html_safe
end
end.join.html_safe
end
def day_cell(day)
content_tag :li, view.capture(day, &callback), class: day_classes(day)
end
def day_classes(day)
classes = []
classes << "today" if day == Date.today
classes << "not-month" if day.month != date.month
classes.empty? ? nil : classes.join(" ")
end
def weeks
first = date.beginning_of_month.beginning_of_week(START_DAY)
last = date.end_of_month.end_of_week(START_DAY)
(first..last).to_a.in_groups_of(7)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment