From fcff3619890e0b8f8a51f7fa4bd4528e25e1b744 Mon Sep 17 00:00:00 2001 From: Michael Hellein Date: Fri, 12 Oct 2012 11:28:51 -0400 Subject: [PATCH 1/3] Added Gemfile.lock to gitginore. --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7750a07..e2b48c7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -pkg/* \ No newline at end of file +pkg/* +Gemfile.lock \ No newline at end of file From 227ffd0231cd920afa46e3bdde60e6ff62b5bdc0 Mon Sep 17 00:00:00 2001 From: Michael Hellein Date: Fri, 12 Oct 2012 12:09:42 -0400 Subject: [PATCH 2/3] Added support for defining custom HTML tags for output. --- lib/later_dude/calendar.rb | 31 +++++++++++++++++++++---------- test/calendar_test.rb | 22 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/lib/later_dude/calendar.rb b/lib/later_dude/calendar.rb index 7277898..51eafd4 100644 --- a/lib/later_dude/calendar.rb +++ b/lib/later_dude/calendar.rb @@ -38,8 +38,8 @@ def initialize(year, month, options={}, &block) def to_html(&block) @block = block if block_given? - content_tag(:table, :class => "#{@options[:calendar_class]}") do - content_tag(:thead, "#{show_month_names}#{show_day_names}".html_safe) + content_tag(:tbody, show_days) + content_tag(@options[:html_tags][:month], :class => "#{@options[:calendar_class]}") do + content_tag(@options[:html_tags][:heading], "#{show_month_names}#{show_day_names}".html_safe) + content_tag(@options[:html_tags][:grid], show_days, :class => 'days') end end @@ -53,7 +53,7 @@ def last_rendered_date private def show_days - content_tag(:tr, "#{show_previous_month}#{show_current_month}#{show_following_month}".html_safe) + content_tag(@options[:html_tags][:week], "#{show_previous_month}#{show_current_month}#{show_following_month}".html_safe, :class => 'week') end def show_previous_month @@ -97,11 +97,12 @@ def show_day(day) content = day.day end - content = content_tag(:td, content.to_s.html_safe, options) + content = content_tag(@options[:html_tags][:day], content.to_s.html_safe, options) # close table row at the end of a week and start a new one # opening and closing tag for the first and last week are included in #show_days - content << "".html_safe if day < @days.last && day.wday == last_day_of_week + tagname = @options[:html_tags][:week].to_s + content << "<#{tagname} class=\"week\">".html_safe if day < @days.last && day.wday == last_day_of_week content end @@ -114,7 +115,7 @@ def beginning_of_week(day) def show_month_names return if @options[:hide_month_name] - content_tag(:tr, "#{previous_month}#{current_month}#{next_month}".html_safe, :class => 'month_names') + content_tag(@options[:html_tags][:week], "#{previous_month}#{current_month}#{next_month}".html_safe, :class => 'month_names') end # @options[:previous_month] can either be a single value or an array containing two values. For a single value, the @@ -143,8 +144,9 @@ def current_month def show_month(month, format, options={}) options[:colspan] ||= 2 + options[:colspan] = nil unless [:tr, :th].include? @options[:html_tags][:label] - content_tag(:th, :colspan => options[:colspan], :class => "#{options[:class]} #{Date::MONTHNAMES[month.month].downcase}") do + content_tag(@options[:html_tags][:label], :colspan => options[:colspan], :class => "#{options[:class]} #{Date::MONTHNAMES[month.month].downcase}") do if format.kind_of?(Array) && format.size == 2 text = I18n.localize(month, :format => format.first.to_s).html_safe format.last.respond_to?(:call) ? link_to(text, format.last.call(month)) : text @@ -168,9 +170,10 @@ def abbreviated_day_names def show_day_names return if @options[:hide_day_names] - content_tag(:tr, :class => 'day_names') do + content_tag(@options[:html_tags][:week], :class => 'day_names') do apply_first_day_of_week(day_names).inject('') do |output, day| - output << content_tag(:th, include_day_abbreviation(day), :scope => 'col', :class => Date::DAYNAMES[day_names.index(day)].downcase) + scope = @options[:html_tags][:label] == :th ? 'col' : nil + output << content_tag(@options[:html_tags][:label], include_day_abbreviation(day), :scope => scope, :class => Date::DAYNAMES[day_names.index(day)].downcase) end.html_safe end end @@ -212,7 +215,15 @@ def default_calendar_options :next_month => false, :previous_month => false, :next_and_previous_month => false, - :yield_surrounding_days => false + :yield_surrounding_days => false, + :html_tags => { + :month => :table, + :heading => :thead, + :label => :th, + :week => :tr, + :day => :td, + :grid => :tbody, + } } end end diff --git a/test/calendar_test.rb b/test/calendar_test.rb index 6bf49e9..116ffd8 100644 --- a/test/calendar_test.rb +++ b/test/calendar_test.rb @@ -280,5 +280,27 @@ class CalendarTest < ActiveSupport::TestCase assert_equal calendar_html1, calendar_html2 end + test "allows overriding of html tags" do + tags = { + :month => :div, + :heading => :header, + :label => :label, + :grid => :section, + :week => :div, + :day => :article, + } + calendar = LaterDude::Calendar.new(2012, 7, html_tags: tags, :next_month => "»") + html = calendar.to_html + # There should be no colspan on tags other than TH and TD. + assert_no_match %r(colspan=), html + # There should be no scope on tags other than TH. + assert_no_match %r(scope=), html + # Each of the tags we define should be found in the output. + tags.each do |key, tag| + assert_match /<#{tag}/, html + end + assert_equal html, 'blah' + end + # TODO: Should I do "real" output testing despite the good coverage of output-related methods? Testing HTML is tedious ... end From 055dad68ca594e2c02c8a34554ed86d0bd8cacb0 Mon Sep 17 00:00:00 2001 From: Michael Hellein Date: Fri, 12 Oct 2012 15:47:32 -0400 Subject: [PATCH 3/3] Corrected willfully failing test. --- test/calendar_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/calendar_test.rb b/test/calendar_test.rb index 116ffd8..353c2f7 100644 --- a/test/calendar_test.rb +++ b/test/calendar_test.rb @@ -299,7 +299,6 @@ class CalendarTest < ActiveSupport::TestCase tags.each do |key, tag| assert_match /<#{tag}/, html end - assert_equal html, 'blah' end # TODO: Should I do "real" output testing despite the good coverage of output-related methods? Testing HTML is tedious ...