From 0b1efc8b30f7804f2ffa8e8adaac2ffed3a4f246 Mon Sep 17 00:00:00 2001 From: Arthaey Date: Sat, 2 Jul 2011 12:20:41 -0700 Subject: [PATCH 1/2] Support custom CSS class names on day by passing in a lambda. --- lib/table_builder/calendar_helper.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/table_builder/calendar_helper.rb b/lib/table_builder/calendar_helper.rb index 5b8871d..42c51e3 100644 --- a/lib/table_builder/calendar_helper.rb +++ b/lib/table_builder/calendar_helper.rb @@ -23,12 +23,13 @@ def day(*args) options = options_from_hash(args) day_method = options.delete(:day_method) || :date id_pattern = options.delete(:id) + css_lambda = options.delete(:class) tbody do @calendar.objects_for_days(@objects, day_method).to_a.sort{|a1, a2| a1.first <=> a2.first }.each do |o| key, array = o day, objects = array concat(tag(:tr, options, true)) if(day.wday == @calendar.first_weekday) - concat(tag(:td, td_options(day, id_pattern), true)) + concat(tag(:td, td_options(day, id_pattern, css_lambda), true)) yield(day, objects) concat('') concat('') if(day.wday == @calendar.last_weekday) @@ -42,13 +43,14 @@ def objects_for_days @calendar.objects_for_days(@objects) end - def td_options(day, id_pattern) + def td_options(day, id_pattern, css_lambda) options = {} css_classes = [] - css_classes << 'today' if day.strftime("%Y-%m-%d") == @today.strftime("%Y-%m-%d") - css_classes << 'notmonth' if day.month != @calendar.month - css_classes << 'weekend' if day.wday == 0 or day.wday == 6 - css_classes << 'future' if day > @today.to_date + css_classes << 'today' if day.strftime("%Y-%m-%d") == @today.strftime("%Y-%m-%d") + css_classes << 'notmonth' if day.month != @calendar.month + css_classes << 'weekend' if day.wday == 0 or day.wday == 6 + css_classes << 'future' if day > @today.to_date + css_classes << css_lambda.call(day) if !css_lambda.nil? options[:class] = css_classes.join(' ') unless css_classes.empty? options[:id] = day.strftime(id_pattern) if id_pattern options From c98cecb1b7cdd0b71728fbe7a21cfb86aa951d16 Mon Sep 17 00:00:00 2001 From: Arthaey Date: Sat, 2 Jul 2011 12:38:54 -0700 Subject: [PATCH 2/2] Added test for custom CSS for date elements. --- test/calendar_helper_test.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/calendar_helper_test.rb b/test/calendar_helper_test.rb index 62a4810..16f26ae 100644 --- a/test/calendar_helper_test.rb +++ b/test/calendar_helper_test.rb @@ -81,6 +81,25 @@ def test_calendar_for_sets_css_classes assert_dom_equal expected, output end + def test_calendar_for_sets_custom_css_classes + custom_css = lambda { |day| day.day.even? ? "even" : "odd" } + output = calendar_for([], :year=> 2008, :month => 12, :today => Date.civil(2008, 12, 15)) do |c| + c.day(:class => custom_css) do |day, events| + concat(events.collect{|e| e.id}.join) + end + end + expected = %() << + %() << + %() << + %() << + %() << + %() << + %() << + %() << + %(
) + assert_dom_equal expected, output + end + def test_calendar_for_thirty_days today = Date.civil(2008, 12, 15) output = calendar_for([], :today => today, :year=>2008, :month=>12, :first=>today, :last=>:thirty_days) do |c|