Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for changing tagnames. #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pkg/*
pkg/*
Gemfile.lock
31 changes: 21 additions & 10 deletions lib/later_dude/calendar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 << "</tr><tr>".html_safe if day < @days.last && day.wday == last_day_of_week
tagname = @options[:html_tags][:week].to_s
content << "</#{tagname}><#{tagname} class=\"week\">".html_safe if day < @days.last && day.wday == last_day_of_week
content
end

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions test/calendar_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,26 @@ 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 => "&raquo;")
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
end

# TODO: Should I do "real" output testing despite the good coverage of output-related methods? Testing HTML is tedious ...
end