Skip to content

Commit

Permalink
Allow CalendarHelper to use any object that responds to 'each'
Browse files Browse the repository at this point in the history
Previously were requiring an object to be an array. This is not the ruby
way! All we really care about is that it responds to the each method.

Test case added to cover this. All tests passing.
  • Loading branch information
derekprior committed Feb 13, 2012
1 parent b8b1d43 commit 15bc48f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 22 deletions.
26 changes: 13 additions & 13 deletions lib/table_builder/table_builder.rb
Expand Up @@ -15,7 +15,7 @@ class TableBuilder
include ::ActionView::Helpers::TagHelper

def initialize(objects, template, options)
raise ArgumentError, "TableBuilder expects an Array but found a #{objects.inspect}" unless objects.is_a? Array
raise ArgumentError, "TableBuilder expects an Enumerable object but found #{objects.inspect}" unless objects.respond_to? :each
@objects, @template, @options = objects, template, options
end

Expand All @@ -24,7 +24,7 @@ def head(*args)
concat(tag(:thead, options_from_hash(args), true))
yield
concat('</thead>')
else
else
@num_of_columns = args.size
content_tag(:thead,
content_tag(:tr,
Expand All @@ -51,7 +51,7 @@ def body(*args)
@objects.each { |c| yield(c) }
end
end

def body_r(*args)
raise ArgumentError, "Missing block" unless block_given?
options = options_from_hash(args)
Expand All @@ -62,7 +62,7 @@ def body_r(*args)
concat('</tr>'.html_safe)
}
end
end
end

def r(*args)
raise ArgumentError, "Missing block" unless block_given?
Expand All @@ -80,7 +80,7 @@ def h(*args)
else
content = args.shift
content_tag(:th, content, options_from_hash(args))
end
end
end

def d(*args)
Expand All @@ -91,16 +91,16 @@ def d(*args)
else
content = args.shift
content_tag(:td, content, options_from_hash(args))
end
end
end


private

def options_from_hash(args)
args.last.is_a?(Hash) ? args.pop : {}
end

def concat(tag)
@template.safe_concat(tag)
""
Expand All @@ -110,17 +110,17 @@ def content_tag(tag, content, *args)
options = options_from_hash(args)
@template.content_tag(tag, content, options)
end

def tbody
concat('<tbody>')
yield
concat('</tbody>')
end

def tr options
concat(tag(:tr, options, true))
yield
concat('</tr>')
concat('</tr>')
end
end
end
end
55 changes: 46 additions & 9 deletions test/calendar_helper_test.rb
Expand Up @@ -10,7 +10,7 @@ def setup
@events = [Event.new(3, 'Jimmy Page', Date.civil(2008, 12, 26)),
Event.new(4, 'Robert Plant', Date.civil(2008, 12, 26))]
end

def test_calendar_for
output = calendar_for(@events, :html => { :id => 'id', :style => 'style', :class => 'class'}) do |t|
end
Expand All @@ -25,7 +25,7 @@ def test_calendar_for_without_an_array
calendar_for('a') {|t| }
end
end

def test_calendar_for_with_empty_array
output = calendar_for([], :year=> 2008, :month => 12) do |c|
c.day do |day, events|
Expand All @@ -43,7 +43,7 @@ def test_calendar_for_with_empty_array
%(</table>)
assert_dom_equal expected, output
end

def test_calendar_for_with_events
output = calendar_for(@events, :year=> 2008, :month => 12) do |c|
c.day do |day, events|
Expand All @@ -62,7 +62,7 @@ def test_calendar_for_with_events
%(</table>)
assert_dom_equal expected, output
end

def test_calendar_for_sets_css_classes
output = calendar_for([], :year=> 2008, :month => 12, :today => Date.civil(2008, 12, 15)) do |c|
c.day do |day, events|
Expand Down Expand Up @@ -131,7 +131,7 @@ def test_calendar_for_sets_css_ids
%(</tbody>) <<
%(</table>)
assert_dom_equal expected, output
end
end

def test_calendar_for_with_row_headers
output = calendar_for([], :year=> 2008, :month => 12, :row_header => true) do |c|
Expand All @@ -154,6 +154,26 @@ def test_calendar_for_with_row_headers
%(</table>)
assert_dom_equal expected, output
end

def test_calendar_for_with_enumerable_object
output = calendar_for(Wrapped.new(@events), :year=> 2008, :month => 12) do |c|
c.day do |day, events|
content = events.collect{|e| e.id}.join
concat("(#{day.day})#{content}")
end
end
expected = %(<table>) <<
%(<tbody>) <<
%(<tr><td class="notmonth weekend">(30)</td><td>(1)</td><td>(2)</td><td>(3)</td><td>(4)</td><td>(5)</td><td class="weekend">(6)</td></tr>) <<
%(<tr><td class="weekend">(7)</td><td>(8)</td><td>(9)</td><td>(10)</td><td>(11)</td><td>(12)</td><td class="weekend">(13)</td></tr>) <<
%(<tr><td class="weekend">(14)</td><td>(15)</td><td>(16)</td><td>(17)</td><td>(18)</td><td>(19)</td><td class="weekend">(20)</td></tr>) <<
%(<tr><td class="weekend">(21)</td><td>(22)</td><td>(23)</td><td>(24)</td><td>(25)</td><td>(26)34</td><td class="weekend">(27)</td></tr>) <<
%(<tr><td class="weekend">(28)</td><td>(29)</td><td>(30)</td><td>(31)</td><td class="notmonth">(1)</td><td class="notmonth">(2)</td><td class="notmonth weekend">(3)</td></tr>) <<
%(</tbody>) <<
%(</table>)
assert_dom_equal expected, output
end

end

class CalendarHelperTest < ActionView::TestCase
Expand All @@ -167,7 +187,7 @@ def test_objects_for_days_with_events
calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12)
objects_for_days = {}
Date.civil(2008, 11, 30).upto(Date.civil(2009, 1, 3)){|day| objects_for_days[day.strftime("%Y-%m-%d")] = [day, []]}
objects_for_days['2008-12-26'][1] = @events
objects_for_days['2008-12-26'][1] = @events
assert_equal objects_for_days, calendar.objects_for_days(@events, :date)
end

Expand Down Expand Up @@ -196,16 +216,33 @@ def test_first_day
calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12)
assert_equal Date.civil(2008, 11, 30), calendar.first_day
end

def test_last_day
calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12)
assert_equal Date.civil(2009, 1, 3), calendar.last_day
end

def test_last_day_with_first_day_of_week_set
calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12, :first_day_of_week => 1)
assert_equal Date.civil(2009, 1, 4), calendar.last_day
end
end
end

class Event < Struct.new(:id, :name, :date); end

class Wrapped
include Enumerable
attr_accessor :objects

def initialize(objects)
@objects = objects
end

def each
@objects.each { |item| yield item }
end

def <=>(other)
@objects <=> other
end
end

0 comments on commit 15bc48f

Please sign in to comment.