table_builder 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.1
@@ -1,139 +1,79 @@
1
1
  module CalendarHelper
2
2
 
3
- def calendar_for(objects, *args)
4
- raise ArgumentError, "Missing block" unless block_given?
5
- options = args.last.is_a?(Hash) ? args.pop : {}
6
- html_options = options[:html]
7
- builder = options[:builder] || CalendarBuilder
8
- calendar = options[:calendar] || Calendar
9
- concat(tag(:table, html_options, true))
10
- yield builder.new(objects || [], self, calendar, options)
11
- concat('</table>')
3
+ def calendar_for objects, options = {}
4
+ html_options = options.delete(:html)
5
+ builder = options.delete(:builder) || CalendarBuilder
6
+ calendar = options.delete(:calendar) || Calendar
7
+ concat content_tag(:table, html_options){ yield builder.new(objects || [], self, calendar, options) }
12
8
  end
13
9
 
14
10
  class CalendarBuilder < TableHelper::TableBuilder
15
- def initialize(objects, template, calendar, options)
16
- super(objects, template, options)
11
+ def initialize objects, template, calendar, options
17
12
  @calendar = calendar.new(options)
18
- @today = options[:today] || Time.now
19
- end
20
-
21
- def day(*args)
13
+ @today = options[:today] || Time.now
14
+ super objects, template, options
15
+ end
16
+
17
+ def day options = {}
22
18
  raise ArgumentError, "Missing block" unless block_given?
23
- options = options_from_hash(args)
24
19
  day_method = options.delete(:day_method) || :date
25
20
  id_pattern = options.delete(:id)
26
- tbody do
27
- @calendar.objects_for_days(@objects, day_method).to_a.sort{|a1, a2| a1.first <=> a2.first }.each do |o|
28
- key, array = o
29
- day, objects = array
30
- concat(tag(:tr, options, true)) if(day.wday == @calendar.first_weekday)
31
- concat(tag(:td, td_options(day, id_pattern), true))
32
- yield(day, objects)
33
- concat('</td>')
34
- concat('</tr>') if(day.wday == @calendar.last_weekday)
21
+
22
+ tag(:tbody) do
23
+ output = ''
24
+ @calendar.objects_for_days(@objects, &day_method).each_slice(7) do |week|
25
+ output = r do
26
+ week.map do |day, objects|
27
+ d capture{yield day, objects}, td_options(day, objects, id_pattern)
28
+ end
29
+ end
35
30
  end
31
+ output
36
32
  end
33
+
37
34
  end
38
-
39
- private
40
-
41
- def objects_for_days
42
- @calendar.objects_for_days(@objects)
43
- end
44
-
45
- def td_options(day, id_pattern)
35
+
36
+ def td_options day, objects, id_pattern
46
37
  options = {}
47
- if(day.strftime("%Y-%m-%d") == @today.strftime("%Y-%m-%d"))
48
- options[:class] = 'today'
49
- elsif(day.month != @calendar.month)
50
- options[:class] = 'notmonth'
51
- elsif(day.wday == 0 or day.wday == 6)
52
- options[:class] = 'weekend'
53
- end
54
- if id_pattern
55
- options[:id] = day.strftime(id_pattern)
56
- end
38
+ css = []
39
+ css << 'notmonth' if day.month != @calendar.month
40
+ css << 'today' if @today == day
41
+ css << 'weekend' if day.wday == 0 or day.wday == 6
42
+ # css << 'empty' if objects.empty?
43
+
44
+ options[:class] = css.join(' ')
45
+ options[:id] = day.strftime(id_pattern) if id_pattern
46
+
47
+ options.delete_if{ |k,v| v.blank? }
57
48
  options
58
49
  end
59
-
50
+
60
51
  end
61
52
 
62
53
  class Calendar
63
- attr_accessor :first_weekday, :last_weekday, :month
64
- def initialize(options={})
65
- @year = options[:year] || Time.now.year
66
- @month = options[:month] || Time.now.month
67
- @first_day_of_week = options[:first_day_of_week] || 0
68
- @first_weekday = first_day_of_week(@first_day_of_week)
69
- @last_weekday = last_day_of_week(@first_day_of_week)
70
- @first = Date.civil(@year, @month, 1)
71
- @last = Date.civil(@year, @month, -1)
54
+ attr_reader :month, :first_weekday, :last_weekday, :first_day, :last_day
55
+ def initialize options = {}
56
+ @year = options[:year] || Time.now.year
57
+ @month = options[:month] || Time.now.month
58
+ @first_weekday = (options[:first_weekday] || 0) % 7
59
+ @last_weekday = (@first_weekday + 6) % 7
60
+ @first = Date.civil @year, @month, 1
61
+ @last = Date.civil @year, @month, -1
62
+ @first_day = @first - (@first.wday - @first_weekday + 7) % 7
63
+ @last_day = @last + (@last_weekday - @last.wday + 7) % 7
72
64
  end
73
65
 
74
- def each_day
75
- first_day.upto(last_day) do |day|
76
- yield(day)
77
- end
66
+ def each_day &block
67
+ (first_day..last_day).map(&block)
78
68
  end
79
69
 
80
- def last_day
81
- last = @last
82
- while(last.wday % 7 != @last_weekday % 7)
83
- last = last.next
84
- end
85
- last
86
- end
87
-
88
- def first_day
89
- first = @first - 6
90
- while(first.wday % 7 != (@first_weekday) % 7)
91
- first = first.next
92
- end
93
- first
94
- end
95
-
96
- def objects_for_days(objects, day_method)
97
- unless @objects_for_days
98
- @objects_for_days = {}
99
- days.each{|day| @objects_for_days[day.strftime("%Y-%m-%d")] = [day, []]}
100
- objects.each do |o|
101
- date = o.send(day_method.to_sym).strftime("%Y-%m-%d")
102
- if @objects_for_days[date]
103
- @objects_for_days[date][1] << o
104
- end
105
- end
106
- end
107
- @objects_for_days
70
+ def objects_for_days objects, &day_method
71
+ objects = objects.group_by(&day_method)
72
+ each_day { |day| [day, objects[day] || []] }
108
73
  end
109
74
 
110
75
  def days
111
- unless @days
112
- @days = []
113
- each_day{|day| @days << day}
114
- end
115
- @days
116
- end
117
-
118
- def mjdays
119
- unless @mjdays
120
- @mdays = []
121
- each_day{|day| @days << day}
122
- end
123
- @days
124
- end
125
-
126
- def first_day_of_week(day)
127
- day
76
+ @days ||= each_day
128
77
  end
129
-
130
- def last_day_of_week(day)
131
- if day > 0
132
- day - 1
133
- else
134
- 6
135
- end
136
- end
137
78
  end
138
-
139
- end
79
+ end
@@ -1,125 +1,65 @@
1
1
  module TableHelper
2
2
 
3
- def table_for(objects, *args)
4
- raise ArgumentError, "Missing block" unless block_given?
5
- options = args.last.is_a?(Hash) ? args.pop : {}
6
- html_options = options[:html]
7
- builder = options[:builder] || TableBuilder
8
-
9
- concat(tag(:table, html_options, true))
10
- yield builder.new(objects || [], self, options)
11
- concat('</table>')
3
+ def table_for objects = [], options = {}
4
+ html_options = options.delete(:html)
5
+ builder = options.delete(:builder) || TableBuilder
6
+ concat content_tag(:table, html_options) { yield builder.new(objects, self, options) }
12
7
  end
13
8
 
14
9
  class TableBuilder
15
- include ::ActionView::Helpers::TagHelper
16
-
17
- def initialize(objects, template, options)
18
- raise ArgumentError, "TableBuilder expects an Array but found a #{objects.inspect}" unless objects.is_a? Array
10
+ def initialize objects, template, options
11
+ raise ArgumentError, "TableBuilder expects an Array or ActiveRecord::NamedScope::Scope but found a #{objects.class}" unless Array === objects or ActiveRecord::NamedScope::Scope === objects
19
12
  @objects, @template, @options = objects, template, options
20
13
  end
21
-
22
- def head(*args)
23
- if block_given?
24
- concat(tag(:thead, options_from_hash(args), true))
25
- yield
26
- concat('</thead>')
27
- else
28
- @num_of_columns = args.size
29
- content_tag(:thead,
30
- content_tag(:tr,
31
- args.collect { |c| content_tag(:th, c)}.join('')
32
- )
33
- )
34
- end
14
+
15
+ def body options = {}
16
+ concat content_tag(:tbody, @objects.map{ |o| capture {yield o} }, options)
35
17
  end
36
-
37
- def head_r(*args)
38
- raise ArgumentError, "Missing block" unless block_given?
39
- options = options_from_hash(args)
40
- head do
41
- concat(tag(:tr, options, true))
42
- yield
43
- concat('</tr>')
44
- end
18
+
19
+ def body_r options = {}
20
+ concat content_tag(:tbody, @objects.map{ |o| r capture {yield o}, options })
21
+ end
22
+
23
+ def head *args, &block
24
+ return tag(:thead, *args, &block) if block_given?
25
+ options = args.extract_options!
26
+ content = (args.size == 1 ? args.first : args).map{|c|"<th>#{c}</th>"}
27
+ content_tag :thead, r(content), options
45
28
  end
46
29
 
47
- def body(*args)
48
- raise ArgumentError, "Missing block" unless block_given?
49
- options = options_from_hash(args)
50
- tbody do
51
- @objects.each { |c| yield(c) }
52
- end
30
+ def head_r *args, &block
31
+ head { tag :tr, *args, &block }
53
32
  end
54
33
 
55
- def body_r(*args)
56
- raise ArgumentError, "Missing block" unless block_given?
57
- options = options_from_hash(args)
58
- tbody do
59
- @objects.each { |c|
60
- concat(tag(:tr, options, true))
61
- yield(c)
62
- concat('</tr>')
63
- }
64
- end
65
- end
66
-
67
- def r(*args)
68
- raise ArgumentError, "Missing block" unless block_given?
69
- options = options_from_hash(args)
70
- tr(options) do
71
- yield
72
- end
34
+ def r *args, &block
35
+ tag :tr, *args, &block
73
36
  end
74
37
 
75
- def h(*args)
76
- if block_given?
77
- concat(tag(:th, options_from_hash(args), true))
78
- yield
79
- concat('</th>')
80
- else
81
- content = args.shift
82
- content_tag(:th, content, options_from_hash(args))
83
- end
38
+ def h *args, &block
39
+ tag :th, *args, &block
84
40
  end
85
41
 
86
- def d(*args)
87
- if block_given?
88
- concat(tag(:td, options_from_hash(args), true))
89
- yield
90
- concat('</td>')
91
- else
92
- content = args.shift
93
- content_tag(:td, content, options_from_hash(args))
94
- end
42
+ def d *args, &block
43
+ tag :td, *args, &block
95
44
  end
96
-
97
45
 
98
46
  private
47
+ def tag tag, *args, &block
48
+ options = args.extract_options!
49
+ return concat content_tag(tag, capture(&block), options) if block_given?
50
+ content_tag tag, args, options
51
+ end
99
52
 
100
- def options_from_hash(args)
101
- args.last.is_a?(Hash) ? args.pop : {}
53
+ def concat str
54
+ @template.concat str
102
55
  end
103
56
 
104
- def concat(tag)
105
- @template.concat(tag)
57
+ def capture &block
58
+ @template.capture &block
106
59
  end
107
60
 
108
- def content_tag(tag, content, *args)
109
- options = options_from_hash(args)
61
+ def content_tag tag, content, options = {}
110
62
  @template.content_tag(tag, content, options)
111
63
  end
112
-
113
- def tbody
114
- concat('<tbody>')
115
- yield
116
- concat('</tbody>')
117
- end
118
-
119
- def tr options
120
- concat(tag(:tr, options, true))
121
- yield
122
- concat('</tr>')
123
- end
124
64
  end
125
65
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{table_builder}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Petrik de Heus"]
12
- s.date = %q{2010-06-29}
12
+ s.date = %q{2010-07-05}
13
13
  s.description = %q{Rails builder for creating tables and calendars inspired by ActionView's FormBuilder.}
14
14
  s.email = %q{}
15
15
  s.extra_rdoc_files = [
@@ -1,162 +1,163 @@
1
- require File.join(File.dirname(__FILE__), 'test_helper.rb')
1
+ require 'test_helper'
2
2
 
3
- class CalendarHelperTest < Test::Unit::TestCase
4
- include ActionView::Helpers::TextHelper
5
- include ActionView::Helpers::TagHelper
6
- include ActionController::TestCase::Assertions
3
+ class CalendarHelperTest < ActionView::TestCase
7
4
  include CalendarHelper
8
- attr_accessor :output_buffer
9
5
 
10
6
  def setup
11
- @events = [Event.new(3, 'Jimmy Page', Date.civil(2008, 12, 26)),
12
- Event.new(4, 'Robert Plant', Date.civil(2008, 12, 26))]
13
- end
14
-
15
- def test_calendar_for
16
- self.output_buffer = ''
17
- calendar_for(@events, :html => { :id => 'id', :style => 'style', :class => 'class'}) do |t|
18
- end
19
- expected = %(<table id="id" style="style" class="class">) <<
20
- %(</table>)
21
- assert_dom_equal expected, output_buffer
7
+ @events = [
8
+ Event.new(3, 'Jimmy Page', Date.civil(2008, 12, 26)),
9
+ Event.new(4, 'Robert Plant', Date.civil(2008, 12, 26))
10
+ ]
22
11
  end
23
12
 
24
- def test_calendar_for_without_an_array
25
- self.output_buffer = ''
26
- assert_raises(ArgumentError) do
27
- calendar_for('a') {|t| }
28
- end
13
+ should 'raise error if called without array' do
14
+ assert_raises(ArgumentError) { calendar_for('a') {|t|} }
29
15
  end
30
16
 
31
- def test_calendar_for_with_empty_array
32
- self.output_buffer = ''
33
- calendar_for([], :year=> 2008, :month => 12) do |c|
34
- c.day do |day, events|
35
- output_buffer.concat(events.collect{|e| e.id}.join)
36
- end
37
- end
38
- expected = %(<table>) <<
39
- %(<tbody>) <<
40
- %(<tr><td class="notmonth"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>) <<
41
- %(<tr><td class="weekend"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>) <<
42
- %(<tr><td class="weekend"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>) <<
43
- %(<tr><td class="weekend"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>) <<
44
- %(<tr><td class="weekend"></td><td></td><td></td><td></td><td class="notmonth"></td><td class="notmonth"></td><td class="notmonth"></td></tr>) <<
45
- %(</tbody>) <<
46
- %(</table>)
47
- assert_dom_equal expected, self.output_buffer
48
- end
49
-
50
- def test_calendar_for_with_events
51
- self.output_buffer = ''
52
- calendar_for(@events, :year=> 2008, :month => 12) do |c|
53
- c.day do |day, events|
54
- content = events.collect{|e| e.id}.join
55
- output_buffer.concat("(#{day.day})#{content}")
56
- end
17
+ context 'Calendar days' do
18
+ should 'return objects_for_days with day and empty array' do
19
+ calendar = CalendarHelper::Calendar.new :year=> 2008, :month => 12
20
+ objects_for_days = (Date.civil(2008, 11, 30)..Date.civil(2009, 1, 3)).map { |day| [day, []] }
21
+ assert_equal objects_for_days, calendar.objects_for_days([], &:date)
57
22
  end
58
- expected = %(<table>) <<
59
- %(<tbody>) <<
60
- %(<tr><td class="notmonth">(30)</td><td>(1)</td><td>(2)</td><td>(3)</td><td>(4)</td><td>(5)</td><td class="weekend">(6)</td></tr>) <<
61
- %(<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>) <<
62
- %(<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>) <<
63
- %(<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>) <<
64
- %(<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">(3)</td></tr>) <<
65
- %(</tbody>) <<
66
- %(</table>)
67
- assert_dom_equal expected, output_buffer
68
- end
69
-
70
- def test_calendar_for_sets_css_classes
71
- self.output_buffer = ''
72
- calendar_for([], :year=> 2008, :month => 12, :today => Date.civil(2008, 12, 15)) do |c|
73
- c.day do |day, events|
74
- output_buffer.concat(events.collect{|e| e.id}.join)
23
+
24
+ should 'return objects_for_days with days and events' do
25
+ calendar = CalendarHelper::Calendar.new :year=> 2008, :month => 12
26
+ objects_for_days = (Date.civil(2008, 11, 30)..Date.civil(2009, 1, 3)).map do |day|
27
+ [day, Date.civil(2008, 12, 26) == day ? @events : []]
75
28
  end
29
+ assert_equal objects_for_days, calendar.objects_for_days(@events, &:date)
76
30
  end
77
- expected = %(<table>) <<
78
- %(<tbody>) <<
79
- %(<tr><td class="notmonth"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>) <<
80
- %(<tr><td class="weekend"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>) <<
81
- %(<tr><td class="weekend"></td><td class="today"></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>) <<
82
- %(<tr><td class="weekend"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>) <<
83
- %(<tr><td class="weekend"></td><td></td><td></td><td></td><td class="notmonth"></td><td class="notmonth"></td><td class="notmonth"></td></tr>) <<
84
- %(</tbody>) <<
85
- %(</table>)
86
- assert_dom_equal expected, self.output_buffer
87
- end
88
-
89
- def test_calendar_for_sets_css_ids
90
- self.output_buffer = ''
91
- calendar_for([], :year=> 2008, :month => 12, :today => Date.civil(2008, 12, 15)) do |c|
92
- c.day(:id => 'day_%d') do |day, events|
93
- output_buffer.concat(events.collect{|e| e.id}.join)
94
- end
31
+
32
+ should 'map day range for calendar' do
33
+ calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12)
34
+ assert_equal (Date.civil(2008, 11, 30)..Date.civil(2009, 1, 3)).map, calendar.days
95
35
  end
96
- expected = %(<table>) <<
97
- %(<tbody>) <<
98
- %(<tr><td class="notmonth" id="day_30"></td><td id="day_01"></td><td id="day_02"></td><td id="day_03"></td><td id="day_04"></td><td id="day_05"></td><td class="weekend" id="day_06"></td></tr>) <<
99
- %(<tr><td class="weekend" id="day_07"></td><td id="day_08"></td><td id="day_09"></td><td id="day_10"></td><td id="day_11"></td><td id="day_12"></td><td class="weekend" id="day_13"></td></tr>) <<
100
- %(<tr><td class="weekend" id="day_14"></td><td class="today"id="day_15"></td><td id="day_16"></td><td id="day_17"></td><td id="day_18"></td><td id="day_19"></td><td class="weekend" id="day_20"></td></tr>) <<
101
- %(<tr><td class="weekend" id="day_21"></td><td id="day_22"></td><td id="day_23"></td><td id="day_24"></td><td id="day_25"></td><td id="day_26"></td><td class="weekend" id="day_27"></td></tr>) <<
102
- %(<tr><td class="weekend" id="day_28"></td><td id="day_29"></td><td id="day_30"></td><td id="day_31"></td><td class="notmonth" id="day_01"></td><td class="notmonth" id="day_02"></td><td class="notmonth" id="day_03"></td></tr>) <<
103
- %(</tbody>) <<
104
- %(</table>)
105
- assert_dom_equal expected, self.output_buffer
106
- end
107
-
108
- end
109
36
 
110
- class CalendarHelperTest < Test::Unit::TestCase
37
+ should 'map day range starting from monday when passed first_weekday' do
38
+ calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12, :first_weekday => 1)
39
+ assert_equal (Date.civil(2008, 12, 1)..Date.civil(2009, 1, 4)).map, calendar.days
40
+ end
111
41
 
112
- def setup
113
- @events = [Event.new(3, 'Jimmy Page', Date.civil(2008, 12, 26)),
114
- Event.new(4, 'Robert Plant', Date.civil(2008, 12, 26))]
115
- end
42
+ should 'set first day to previous sunday' do
43
+ calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12)
44
+ assert_equal Date.civil(2008, 11, 30), calendar.first_day
45
+ end
116
46
 
117
- def test_objects_for_days_with_events
118
- calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12)
119
- objects_for_days = {}
120
- Date.civil(2008, 11, 30).upto(Date.civil(2009, 1, 3)){|day| objects_for_days[day.strftime("%Y-%m-%d")] = [day, []]}
121
- objects_for_days['2008-12-26'][1] = @events
122
- assert_equal objects_for_days, calendar.objects_for_days(@events, :date)
47
+ should 'set last day to following sunday' do
48
+ calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12)
49
+ assert_equal Date.civil(2009, 1, 3), calendar.last_day
50
+ end
51
+
52
+ should 'start range from previous monday when first_weekday is one' do
53
+ calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12, :first_weekday => 1)
54
+ assert_equal Date.civil(2009, 1, 4), calendar.last_day
55
+ end
56
+
123
57
  end
58
+
59
+ context 'ERB Rendering' do
60
+ should 'render table for calendar' do
61
+ erb = <<-ERB
62
+ <% calendar_for @events, :html => { :id => 'id', :style => 'style', :class => 'class'} do |t| %>
63
+ <% end %>
64
+ ERB
65
+ assert_dom_equal %(<table id="id" style="style" class="class"></table>), render(:inline => erb)
66
+ end
67
+
68
+ should 'render trs and tds with empty array' do
69
+ erb = <<-ERB
70
+ <% calendar_for [], :year=> 2008, :month => 12 do |c| %>
71
+ <% c.day do |day, events| %>
72
+ <% end %>
73
+ <% end %>
74
+ ERB
75
+
76
+ html = <<-HTML
77
+ <table>
78
+ <tbody>
79
+ <tr><td class="notmonth weekend"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>
80
+ <tr><td class="weekend"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>
81
+ <tr><td class="weekend"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>
82
+ <tr><td class="weekend"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>
83
+ <tr><td class="weekend"></td><td></td><td></td><td></td><td class="notmonth"></td><td class="notmonth"></td><td class="notmonth weekend"></td></tr>
84
+ </tbody>
85
+ </table>
86
+ HTML
87
+ assert_dom_equal html, render(:inline => erb)
88
+ end
89
+
90
+ should 'output day numbers' do
91
+ erb = <<-ERB
92
+ <% calendar_for @events, :year=> 2008, :month => 12 do |c| %>
93
+ <% c.day do |day, events| %>
94
+ <%= day.day %>
95
+ <% end %>
96
+ <% end %>
97
+ ERB
98
+
99
+ html = <<-HTML
100
+ <table>
101
+ <tbody>
102
+ <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>
103
+ <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>
104
+ <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>
105
+ <tr><td class="weekend">21</td><td>22</td><td>23</td><td>24</td><td>25</td><td>26</td><td class="weekend">27</td></tr>
106
+ <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>
107
+ </tbody>
108
+ </table>
109
+ HTML
124
110
 
125
- def test_objects_for_days
126
- calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12)
127
- objects_for_days = {}
128
- Date.civil(2008, 11, 30).upto(Date.civil(2009, 1, 3)){|day| objects_for_days[day.strftime("%Y-%m-%d")] = [day, []]}
129
- assert_equal objects_for_days, calendar.objects_for_days([], :date)
130
- end
111
+ assert_dom_equal html, render(:inline => erb)
112
+ end
113
+
114
+ should 'render events' do
115
+ erb = <<-ERB
116
+ <% calendar_for @events, :year=> 2008, :month => 12 do |c| %>
117
+ <% c.day do |day, events| %>
118
+ <%= events.map(&:id).join(',') %>
119
+ <% end %>
120
+ <% end %>
121
+ ERB
131
122
 
132
- def test_days
133
- calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12)
134
- days = []
135
- Date.civil(2008, 11, 30).upto(Date.civil(2009, 1, 3)){|day| days << day}
136
- assert_equal days, calendar.days
137
- end
123
+ html = <<-HTML
124
+ <table>
125
+ <tbody>
126
+ <tr><td class="notmonth weekend"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>
127
+ <tr><td class="weekend"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>
128
+ <tr><td class="weekend"></td><td></td><td></td><td></td><td></td><td></td><td class="weekend"></td></tr>
129
+ <tr><td class="weekend"></td><td></td><td></td><td></td><td></td><td>3,4</td><td class="weekend"></td></tr>
130
+ <tr><td class="weekend"></td><td></td><td></td><td></td><td class="notmonth"></td><td class="notmonth"></td><td class="notmonth weekend"></td></tr>
131
+ </tbody>
132
+ </table>
133
+ HTML
134
+ assert_dom_equal html, render(:inline => erb)
135
+ end
138
136
 
139
- def test_days_with_first_day_of_week_set
140
- calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12, :first_day_of_week => 1)
141
- days = []
142
- Date.civil(2008, 12, 1).upto(Date.civil(2009, 1, 4)){|day| days << day}
143
- assert_equal days, calendar.days
144
- end
137
+ should 'render id attribute for using pattern' do
138
+ erb = <<-ERB
139
+ <% calendar_for @events, :year=> 2008, :month => 12, :today => Date.civil(2008, 12, 15) do |c| %>
140
+ <% c.day :id => 'day_%d' do |day, events| %>
141
+ <%= events.map(&:id).join(',') %>
142
+ <% end %>
143
+ <% end %>
144
+ ERB
145
145
 
146
- def test_first_day
147
- calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12)
148
- assert_equal Date.civil(2008, 11, 30), calendar.first_day
149
- end
146
+ html = <<-HTML
147
+ <table>
148
+ <tbody>
149
+ <tr><td class="notmonth weekend" id="day_30"></td><td id="day_01"></td><td id="day_02"></td><td id="day_03"></td><td id="day_04"></td><td id="day_05"></td><td class="weekend" id="day_06"></td></tr>
150
+ <tr><td class="weekend" id="day_07"></td><td id="day_08"></td><td id="day_09"></td><td id="day_10"></td><td id="day_11"></td><td id="day_12"></td><td class="weekend" id="day_13"></td></tr>
151
+ <tr><td class="weekend" id="day_14"></td><td class="today"id="day_15"></td><td id="day_16"></td><td id="day_17"></td><td id="day_18"></td><td id="day_19"></td><td class="weekend" id="day_20"></td></tr>
152
+ <tr><td class="weekend" id="day_21"></td><td id="day_22"></td><td id="day_23"></td><td id="day_24"></td><td id="day_25"></td><td id="day_26">3,4</td><td class="weekend" id="day_27"></td></tr>
153
+ <tr><td class="weekend" id="day_28"></td><td id="day_29"></td><td id="day_30"></td><td id="day_31"></td><td class="notmonth" id="day_01"></td><td class="notmonth" id="day_02"></td><td class="notmonth weekend" id="day_03"></td></tr>
154
+ </tbody>
155
+ </table>
156
+ HTML
157
+ assert_dom_equal html, render(:inline => erb)
158
+ end
150
159
 
151
- def test_last_day
152
- calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12)
153
- assert_equal Date.civil(2009, 1, 3), calendar.last_day
154
160
  end
155
-
156
- def test_last_day_with_first_day_of_week_set
157
- calendar = CalendarHelper::Calendar.new(:year=> 2008, :month => 12, :first_day_of_week => 1)
158
- assert_equal Date.civil(2009, 1, 4), calendar.last_day
159
- end
160
161
  end
161
162
 
162
- class Event < Struct.new(:id, :name, :date); end
163
+
@@ -1,185 +1,226 @@
1
- require File.join(File.dirname(__FILE__), 'test_helper.rb')
1
+ require 'test_helper'
2
2
 
3
- class TableBuilderTest < Test::Unit::TestCase
4
- include ActionView::Helpers::TextHelper
5
- include ActionView::Helpers::TagHelper
6
- include ActionController::TestCase::Assertions
3
+ class TableBuilderTest < ActionView::TestCase
7
4
  include TableHelper
8
5
  attr_accessor :output_buffer
9
6
 
10
7
  def setup
11
- @drummer1 = Drummer.new(1, 'John "Stumpy" Pepys')
12
- @drummer2 = Drummer.new(2, 'Eric "Stumpy Joe" Childs')
13
- @drummer3 = Drummer.new(3, 'Peter "James" Bond')
14
- @drummer4 = Drummer.new(4, 'Mick Shrimpton (R. J. "Ric" Parnell)')
15
- self.output_buffer = ''
8
+ @drummers = [
9
+ Drummer.new(1, 'John "Stumpy" Pepys'),
10
+ Drummer.new(2, 'Eric "Stumpy Joe" Childs'),
11
+ ]
16
12
  end
17
13
 
18
- def test_table_for
19
- table_for([], :html => { :id => 'id', :style => 'style', :class => 'class'}) do |t|
20
- end
21
- expected = %(<table id="id" style="style" class="class">) <<
22
- %(</table>)
23
- assert_dom_equal expected, output_buffer
14
+ should 'raise argument error with out array' do
15
+ assert_raises(ArgumentError) { table_for('a') {|t|} }
24
16
  end
25
-
26
- def test_table_for_without_an_array_raises_error
27
- assert_raises(ArgumentError) do
28
- table_for('a') {|t| }
17
+
18
+ context 'ERB rendering' do
19
+ should 'output table tag' do
20
+ erb = <<-ERB
21
+ <% table_for [], :html => { :id => 'id', :style => 'style', :class => 'class'} do |t| %>
22
+ <% end %>
23
+ ERB
24
+ assert_dom_equal %(<table id="id" style="style" class="class"></table>), render(:inline => erb)
29
25
  end
30
- end
31
-
32
- def test_head
33
- table_for([]) do |t|
34
- t.head do
35
- t.r do
36
- output_buffer.concat t.h('Id')
37
- output_buffer.concat t.h('Name')
38
- end
39
- end
26
+
27
+ should 'output table tag with content' do
28
+ erb = <<-ERB
29
+ <% table_for [] do |t| %>
30
+ <tr></tr>
31
+ <tr></tr>
32
+ <% end %>
33
+ ERB
34
+ assert_dom_equal %(<table><tr></tr><tr></tr></table>), render(:inline => erb)
40
35
  end
41
- expected = %(<table>) <<
42
- %(<thead>) <<
43
- %(<tr>) <<
44
- %(<th>Id</th>) <<
45
- %(<th>Name</th>) <<
46
- %(</tr>) <<
47
- %(</thead>) <<
48
- %(</table>)
49
- assert_dom_equal expected, output_buffer
50
- end
51
-
52
- def test_head_r
53
- table_for([]) do |t|
54
- t.head_r do
55
- output_buffer.concat t.h('Id')
56
- output_buffer.concat t.h('Name')
57
- end
36
+
37
+ should 'output table tag with content and erb tags' do
38
+ erb = <<-ERB
39
+ <% table_for [] do |t| %>
40
+ <%= '<tr></tr>' %>
41
+ <tr></tr>
42
+ <%= '<tr></tr>' %>
43
+ <% end %>
44
+ ERB
45
+ assert_dom_equal %(<table><tr></tr><tr></tr><tr></tr></table>), render(:inline => erb)
58
46
  end
59
- expected = %(<table>) <<
60
- %(<thead>) <<
61
- %(<tr>) <<
62
- %(<th>Id</th>) <<
63
- %(<th>Name</th>) <<
64
- %(</tr>) <<
65
- %(</thead>) <<
66
- %(</table>)
67
- assert_dom_equal expected, output_buffer
68
- end
69
-
70
- def test_head_with_array
71
- table_for([@drummer1, @drummer2]) do |t|
72
- output_buffer.concat t.head('Id', 'Name')
47
+
48
+ should 'output table head passing an array' do
49
+ erb = <<-ERB
50
+ <% table_for [] do |t| %>
51
+ <%= t.head %w(a b c), :class => 'head' %>
52
+ <% end %>
53
+ ERB
54
+ assert_dom_equal %(<table><thead class="head"><tr><th>a</th><th>b</th><th>c</th></tr></thead></table>), render(:inline => erb)
55
+ end
56
+
57
+ should 'output table head' do
58
+ erb = <<-ERB
59
+ <% table_for [] do |t| %>
60
+ <% t.head(:class => 'head') do %>
61
+ <% end %>
62
+ <% end %>
63
+ ERB
64
+ assert_dom_equal %(<table><thead class="head"></thead></table>), render(:inline => erb)
65
+ end
66
+
67
+ should 'output table head with content' do
68
+ erb = <<-ERB
69
+ <% table_for [] do |t| %>
70
+ <% t.head do %>
71
+ <%= '<th></th>' %>
72
+ <th></th>
73
+ <%= '<th></th>' %>
74
+ <% end %>
75
+ <% end %>
76
+ ERB
77
+ assert_dom_equal %(<table><thead><th></th><th></th><th></th></thead></table>), render(:inline => erb)
73
78
  end
74
- expected = %(<table>) <<
75
- %(<thead>) <<
76
- %(<tr>) <<
77
- %(<th>Id</th>) <<
78
- %(<th>Name</th>) <<
79
- %(</tr>) <<
80
- %(</thead>) <<
81
- %(</table>)
82
- assert_dom_equal expected, output_buffer
83
- end
84
79
 
85
- def test_body
86
- table_for([@drummer3, @drummer4]) do |t|
87
- t.body do |e|
88
- t.r do
89
- output_buffer.concat t.d(e.id)
90
- output_buffer.concat t.d(e.name)
91
- end
92
- end
80
+ should 'output table header inside head' do
81
+ erb = <<-ERB
82
+ <% table_for [] do |t| %>
83
+ <% t.head do %>
84
+ <%= t.h 'Id' %>
85
+ <th></th>
86
+ <%= t.h 'Name' %>
87
+ <% end %>
88
+ <% end %>
89
+ ERB
90
+ assert_dom_equal %(<table><thead><th>Id</th><th></th><th>Name</th></thead></table>), render(:inline => erb)
93
91
  end
94
- expected = %(<table>) <<
95
- %(<tbody>) <<
96
- %(<tr>) <<
97
- %(<td>3</td>) <<
98
- %(<td>Peter "James" Bond</td>) <<
99
- %(</tr>) <<
100
- %(<tr>) <<
101
- %(<td>4</td>) <<
102
- %(<td>Mick Shrimpton (R. J. "Ric" Parnell)</td>) <<
103
- %(</tr>) <<
104
- %(</tbody>) <<
105
- %(</table>)
106
- assert_dom_equal expected, output_buffer
107
- end
108
92
 
109
- def test_body_r
110
- table_for([@drummer3, @drummer4]) do |t|
111
- t.body_r do |e|
112
- output_buffer.concat t.d(e.id)
113
- output_buffer.concat t.d(e.name)
114
- end
93
+ should 'output table header with block inside head' do
94
+ erb = <<-ERB
95
+ <% table_for [] do |t| %>
96
+ <% t.head do %>
97
+ <%- t.h do -%>
98
+ <%= 'I' %>d
99
+ <%- end -%>
100
+ <th></th>
101
+ <%= t.h 'Name' %>
102
+ <% end %>
103
+ <% end %>
104
+ ERB
105
+ assert_dom_equal %(<table><thead><th>Id</th><th></th><th>Name</th></thead></table>), render(:inline => erb)
115
106
  end
116
- expected = %(<table>) <<
117
- %(<tbody>) <<
118
- %(<tr>) <<
119
- %(<td>3</td>) <<
120
- %(<td>Peter "James" Bond</td>) <<
121
- %(</tr>) <<
122
- %(<tr>) <<
123
- %(<td>4</td>) <<
124
- %(<td>Mick Shrimpton (R. J. "Ric" Parnell)</td>) <<
125
- %(</tr>) <<
126
- %(</tbody>) <<
127
- %(</table>)
128
- assert_dom_equal expected, output_buffer
129
- end
130
-
131
- def test_td_with_options
132
- table_for([@drummer1]) do |t|
133
- t.body_r do |e|
134
- output_buffer.concat t.d(e.name, :class => 'class')
135
- end
136
- end
137
- expected = %(<table>) <<
138
- %(<tbody>) <<
139
- %(<tr>) <<
140
- %(<td class="class">John "Stumpy" Pepys</td>) <<
141
- %(</tr>) <<
142
- %(</tbody>) <<
143
- %(</table>)
144
- assert_dom_equal expected, output_buffer
145
- end
146
107
 
147
- def test_td_with_block
148
- table_for([@drummer1]) do |t|
149
- t.body_r do |e|
150
- t.d do
151
- output_buffer.concat 'content'
152
- end
153
- end
154
- end
155
- expected = %(<table>) <<
156
- %(<tbody>) <<
157
- %(<tr>) <<
158
- %(<td>content</td>) <<
159
- %(</tr>) <<
160
- %(</tbody>) <<
161
- %(</table>)
162
- assert_dom_equal expected, output_buffer
108
+ should 'output head row with block' do
109
+ erb = <<-ERB
110
+ <% table_for [] do |t| %>
111
+ <% t.head_r :class => 'head' do %>
112
+ <%= t.h('a') %>
113
+ <th>b</th>
114
+ <% t.h do %>
115
+ c
116
+ <% end %>
117
+ <% end %>
118
+ <% end %>
119
+ ERB
120
+ assert_dom_equal "<table><thead><tr class='head'><th>a</th><th>b</th><th>c</th></tr></thead></table>", render(:inline => erb)
121
+ end
122
+
123
+ should 'output tbody' do
124
+ erb = <<-ERB
125
+ <% table_for @drummers do |t| %>
126
+ <% t.head_r do %>
127
+ <th>a</th><th>b</th><th>c</th>
128
+ <% end %>
129
+ <% t.body :class => 'body' do %>
130
+ <% end %>
131
+ <% end %>
132
+ ERB
133
+ assert_dom_equal "<table><thead><tr><th>a</th><th>b</th><th>c</th></tr></thead><tbody class='body'></tbody></table>", render(:inline => erb)
163
134
  end
164
135
 
165
- def test_td_with_block_and_options
166
- table_for([@drummer1]) do |t|
167
- t.body_r do |e|
168
- t.d(:class => 'class') do
169
- output_buffer.concat 'content'
170
- end
171
- end
172
- end
173
- expected = %(<table>) <<
174
- %(<tbody>) <<
175
- %(<tr>) <<
176
- %(<td class="class">content</td>) <<
177
- %(</tr>) <<
178
- %(</tbody>) <<
179
- %(</table>)
180
- assert_dom_equal expected, output_buffer
136
+ should 'output tbody with row and content' do
137
+ erb = <<-ERB
138
+ <% table_for @drummers do |t| %>
139
+ <% t.head_r do %>
140
+ <th>id</th><th>name</th>
141
+ <% end %>
142
+ <% t.body do |d| %>
143
+ <% t.r do %>
144
+ <%= t.d d.id %>
145
+ <%= t.d d.name %>
146
+ <% end %>
147
+ <% end %>
148
+ <% end %>
149
+ ERB
150
+
151
+ html = <<-HTML
152
+ <table>
153
+ <thead>
154
+ <tr><th>id</th><th>name</th></tr>
155
+ </thead>
156
+ <tbody>
157
+ <tr><td>#{ @drummers.first.id }</td><td>#{ @drummers.first.name }</td></tr>
158
+ <tr><td>#{ @drummers.last.id }</td><td>#{ @drummers.last.name }</td></tr>
159
+ </tbody>
160
+ </table>
161
+ HTML
162
+
163
+ assert_dom_equal html, render(:inline => erb)
164
+ end
165
+
166
+ should 'output body rows' do
167
+ erb = <<-ERB
168
+ <% table_for @drummers do |t| %>
169
+ <% t.head_r do %>
170
+ <th>id</th><th>name</th>
171
+ <% end %>
172
+ <% t.body_r do |e| %>
173
+ <%= t.d e.id %>
174
+ <%= t.d e.name %>
175
+ <% end %>
176
+ <% end %>
177
+ ERB
178
+
179
+ html = <<-HTML
180
+ <table>
181
+ <thead>
182
+ <tr><th>id</th><th>name</th></tr>
183
+ </thead>
184
+ <tbody>
185
+ <tr><td>#{ @drummers.first.id }</td><td>#{ @drummers.first.name }</td></tr>
186
+ <tr><td>#{ @drummers.last.id }</td><td>#{ @drummers.last.name }</td></tr>
187
+ </tbody>
188
+ </table>
189
+ HTML
190
+
191
+ assert_dom_equal html, render(:inline => erb)
181
192
  end
193
+
194
+ should 'output td with block and options' do
195
+ erb = <<-ERB
196
+ <% table_for @drummers do |t| %>
197
+ <% t.head_r do %>
198
+ <th>id</th><th>name</th>
199
+ <% end %>
200
+ <% t.body_r do |e| %>
201
+ <% t.d :class => 'id' do %>
202
+ <%= e.id %>
203
+ <% end %>
204
+ <%= t.d e.name %>
205
+ <% end %>
206
+ <% end %>
207
+ ERB
208
+
209
+ html = <<-HTML
210
+ <table>
211
+ <thead>
212
+ <tr><th>id</th><th>name</th></tr>
213
+ </thead>
214
+ <tbody>
215
+ <tr><td class="id">#{ @drummers.first.id }</td><td>#{ @drummers.first.name }</td></tr>
216
+ <tr><td class="id">#{ @drummers.last.id }</td><td>#{ @drummers.last.name }</td></tr>
217
+ </tbody>
218
+ </table>
219
+ HTML
220
+
221
+ assert_dom_equal html, render(:inline => erb)
222
+ end
223
+ end
182
224
 
183
225
  end
184
226
 
185
- class Drummer < Struct.new(:id, :name); end
data/test/test_helper.rb CHANGED
@@ -1,10 +1,16 @@
1
1
  require 'test/unit'
2
2
 
3
- require "rubygems"
3
+ require 'rubygems'
4
+ require 'shoulda'
5
+
4
6
  require 'active_support'
5
7
  require 'action_pack'
6
8
  require 'action_controller'
7
9
  require 'action_view'
10
+ require 'action_view/test_case'
8
11
  require 'action_controller/test_process'
9
12
 
10
- require(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'table_builder')))
13
+ require "#{ File.dirname(__FILE__) }/../lib/table_builder"
14
+
15
+ class Drummer < Struct.new(:id, :name); end
16
+ class Event < Struct.new(:id, :name, :date); end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 2
7
8
  - 1
8
- - 0
9
- version: 0.1.0
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Petrik de Heus
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-29 00:00:00 -05:00
17
+ date: 2010-07-05 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20