module Ecm module CalendarHelper # renders a calendar table def month_calendar(date = Time.zone.now.to_date, elements = [], options = {}) # default options options.reverse_merge! :date_method => :start_at, :display_method => :to_s, :link_elements => true, :start_day => :sunday # calculate beginning and end of month beginning_of_month = date.beginning_of_month.to_date end_of_month = date.end_of_month.to_date # Get localized day names localized_day_names = I18n.t('date.day_names').dup # Shift day names to suite start day english_day_names = [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday] # Raise an exception if the passed start day is not an english day name raise ":start_day option for month_calendar must be in: #{english_day_names.join(', ')}, but you passed: #{options[:start_day].to_s} (class: #{options[:start_day].class.to_s})" unless english_day_names.include?(options[:start_day]) # Get the offset of start day offset = english_day_names.index(options[:start_day]) # Change calendar heading if offset is not 0 (offset 0 means sunday is the first day of the week) offset.times do localized_day_names.push(localized_day_names.shift) end content_tag(:table, :class => 'calendar') do table = '' table << content_tag(:tr, localized_day_names.collect { |day| content_tag(:th, day) }.join("").html_safe) table << content_tag(:tr) do table_content = '' (beginning_of_month..end_of_month).each do |d| if d == beginning_of_month column_offset = d.wday - offset column_offset += 7 if column_offset < 0 column_offset.times do table_content << content_tag(:td, nil, :class => 'offset') end end if d.wday == (0 + offset) table_content << "