Introduction
A single helper method that generates a CSS-styled calendar.
Originally written by Jeremy Voorhis.
Enhanced by Jarkko Laine.
Pluginized and test-driven by Geoffrey Grosenbach.
Sample here: http://rubyonrailsworkshops.com
Also includes a calendar_styles generator that copies a stylesheet to the public stylesheets directory.
How to Install it
<%=calendar(:year => 2005, :month => 6)%>
undefined method `calendar' for . . .
.weekendDay.specialDay {
background-color: #000000;
}
Usage with Globalize plugin
i wanted to create calendars in my locale and i was already using Globalize, to make calendar_helper globalize aware i just added a couple things and to my surprise it worked well.
here is a patch for calendar_helper.rb
--- calendar_helper.original 2006-06-25 00:56:40.000000000 -0500
+++ calendar_helper.rb 2006-06-25 11:44:37.000000000 -0500
@@ -48,7 +48,19 @@
# An additional 'weekend' class is applied to weekend days.
#
# For consistency with the themes provided in the calendar_styles generator, use "specialDay" as the CSS class for marked days.
- #
+ # i
+
+ def daynames_t(abb)
+ days = []
+ Date::DAYNAMES.each {|d|
+ if abb == (0..-1)
+ days << "#{d} [weekday]".t(d)
+ else
+ days << "#{d.slice(abb)} [abbreviated weekday]".t(d.slice(abb))
+ end
+ }
+ days
+ end
def calendar(options = {}, &block)
raise(ArgumentError, "No year given") unless options.has_key?(:year)
raise(ArgumentError, "No month given") unless options.has_key?(:month)
@@ -71,15 +83,16 @@
first_weekday = first_day_of_week(options[:first_day_of_week])
last_weekday = last_day_of_week(options[:first_day_of_week])
-
- day_names = Date::DAYNAMES.dup
+ day_names = daynames_t(options[:abbrev])
+ #day_names = Date::DAYNAMES.dup
+ month_name = "#{Date::MONTHNAMES[options[:month]]} [month]".t("#{Date::MONTHNAMES[options[:month]]}")
first_weekday.times do
day_names.push(day_names.shift)
end
cal = %(<table class="#{options[:table_class]}" border="0" cellspacing="0" cellpadding="0">)
- cal << %(<thead><tr class="#{options[:month_name_class]}"><th colspan="7">#{Date::MONTHNAMES[options[:month]]}</th></tr><tr class="#{options[:day_name_class]}">)
- day_names.each {|d| cal << "<th>#{d[options[:abbrev]]}</th>"}
+ cal << %(<thead><tr class="#{options[:month_name_class]}"><th colspan="7">#{month_name}</th></tr><tr class="#{options[:day_name_class]}">)
+ day_names.each {|d| cal << "<th>#{d}</th>"}
cal << "</tr></thead><tbody><tr>"
beginning_of_week(first, first_weekday).upto(first - 1) do |d|
cal << %(<td class="#{options[:other_month_class]})
make sure you have correctly set a locale in the controller you are showing the calendar!!!