table_builder 0.1.0

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.
@@ -0,0 +1,12 @@
1
+ Autotest.add_hook :initialize do |at|
2
+ at.clear_mappings
3
+ at.add_mapping(/^lib\/.*\.rb$/) do |filename, _|
4
+ possible = File.basename(filename).gsub '_', '_?'
5
+ at.files_matching %r%^test/.*#{possible}$%
6
+ end
7
+
8
+ at.add_mapping(/^test\/.*\_test.rb/) do |f, _|
9
+ at.files_matching(/^test\/.*_test.rb$/)
10
+ end
11
+
12
+ end
@@ -0,0 +1,2 @@
1
+ .DS_Store
2
+ pkg
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Petrik de Heus
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,122 @@
1
+ = TableBuilder
2
+
3
+ Rails builder for creating tables and calendars inspired by ActionView's FormBuilder.
4
+
5
+ == Examples
6
+
7
+ table_for has methods for each tag used in a table (<table>, <thead> <tr>, <td>, etc.)
8
+
9
+ A basic example would look like this:
10
+
11
+ @front_men = [FrontMan.new(1, 'David St. Hubbins'), FrontMan.new(2, 'David Lee Roth')]
12
+
13
+ <% table_for(@front_men) do |t| %>
14
+ <% t.head do %>
15
+ <% t.r do %>
16
+ <%= t.h('Id') %>
17
+ <%= t.h('Name') %>
18
+ <% end %>
19
+ <% end %>
20
+ <% t.body do |front_man| %>
21
+ <% t.r do %>
22
+ <%= t.d(h(front_man.id)) %>
23
+ <%= t.d(h(front_man.name)) %>
24
+ <% end %>
25
+ <% end %>
26
+ <% end %>
27
+
28
+ You can pass an array to the head method:
29
+
30
+ <%= t.head('Id', 'Name') %>
31
+
32
+
33
+ The body and r method can be combined for easier usage:
34
+
35
+ <% t.body_r do |front_man| %>
36
+ <%= t.d(h(front_man.id)) %>
37
+ <%= t.d(h(front_man.name)) %>
38
+ <% end %>
39
+
40
+ You can also pass blocks to the d and h methods for more flexibility:
41
+
42
+ <%= t.d(:class => 'name') do %>
43
+ <%= link_to(h(front_man.name), front_man_url(front_man)) %>
44
+ <% end %>
45
+
46
+ All tag methods are rails tag methods, so they can have extra html options.
47
+
48
+ @drummers = [Drummer.new(1, 'John "Stumpy" Pepys'), Drummer.new(2, 'Eric "Stumpy Joe" Childs')]
49
+
50
+ <% table_for(@drummers, :html => { :id => 'spinal_tap', :class => 'drummers'}) do |t| %>
51
+ <% t.body_r(:class => 'row') do |e| %>
52
+ <%= t.d(h(e.id), :title => 'id') %>
53
+ <%= t.d(h(e.name)) %>
54
+ <% end %>
55
+ <% end %>
56
+
57
+ ... which produces the following html:
58
+
59
+ <table class="drummers" id="spinal_tap">
60
+ <tbody>
61
+ <tr class="row">
62
+ <td title="id">1</td>
63
+ <td>John &quot;Stumpy&quot; Pepys</td>
64
+ </tr>
65
+ <tr class="row">
66
+ <td title="id">2</td>
67
+ <td>Eric &quot;Stumpy Joe&quot; Childs</td>
68
+ </tr>
69
+ </tbody>
70
+ </table>
71
+
72
+ You can customize the table by creating your own TableBuilder:
73
+
74
+ <% table_for(@drummers, :builder => PagedTableBuilder) do |t| %>
75
+
76
+
77
+ == Calendar Table
78
+
79
+ calendar_for creates a table like table_for.
80
+ All objects get sorted per day of the month
81
+
82
+ A basic example would look like this:
83
+
84
+ @tasks = Task.this_month
85
+
86
+ <% calendar_for(@tasks) do |t| %>
87
+ <%= t.head('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun') %>
88
+ <% t.day do |day, tasks| %>
89
+ <%= day.day %>
90
+ <% tasks.each do |task| %>
91
+ <%= h(task.name) %>
92
+ <% end %>
93
+ <% end %>
94
+ <% end %>
95
+
96
+ To show a different month you can pass the :year and :month options:
97
+
98
+ <% calendar_for(@tasks, :year => 2009, :month => 1) do |t| %>
99
+
100
+ To highlight a different day you can pass the :today option:
101
+
102
+ <% calendar_for(@tasks, :today => Date.civil(2008, 12, 26)) do |t| %>
103
+
104
+ By default the :date method is called on the objects for sorting.
105
+ To use another method you can pass the :day_method option:
106
+
107
+ <% t.day(:day_method => :calendar_date) do |day, tasks| %>
108
+
109
+ If you want to add id's to your td tag you can pass a pattern:
110
+
111
+ <% t.day(:id => 'day_%d') do |day, tasks| %>
112
+
113
+
114
+ == Install
115
+
116
+ $ [sudo] gem install table_builder
117
+
118
+ == Contributors
119
+
120
+ Thanks to Sean Dague.
121
+
122
+ Copyright (c) 2008 Petrik de Heus, released under the MIT license
@@ -0,0 +1,39 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the table_builder plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the table_builder plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'TableBuilder'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+
25
+ begin
26
+ require 'jeweler'
27
+ Jeweler::Tasks.new do |gem|
28
+ gem.name = "table_builder"
29
+ gem.summary = %Q{Rails builder for creating tables and calendars inspired by ActionView's FormBuilder.}
30
+ gem.description = %Q{Rails builder for creating tables and calendars inspired by ActionView's FormBuilder.}
31
+ gem.email = ""
32
+ gem.homepage = "http://github.com/maca/table_builder"
33
+ gem.authors = ["Petrik de Heus"]
34
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
35
+ end
36
+ Jeweler::GemcutterTasks.new
37
+ rescue LoadError
38
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
39
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'table_builder'
2
+
@@ -0,0 +1,5 @@
1
+ require(File.expand_path(File.join(File.dirname(__FILE__), 'table_builder', 'table_builder.rb')))
2
+ require(File.expand_path(File.join(File.dirname(__FILE__), 'table_builder', 'calendar_helper.rb')))
3
+
4
+ ActionView::Base.send :include, TableHelper
5
+ ActionView::Base.send :include, CalendarHelper
@@ -0,0 +1,139 @@
1
+ module CalendarHelper
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>')
12
+ end
13
+
14
+ class CalendarBuilder < TableHelper::TableBuilder
15
+ def initialize(objects, template, calendar, options)
16
+ super(objects, template, options)
17
+ @calendar = calendar.new(options)
18
+ @today = options[:today] || Time.now
19
+ end
20
+
21
+ def day(*args)
22
+ raise ArgumentError, "Missing block" unless block_given?
23
+ options = options_from_hash(args)
24
+ day_method = options.delete(:day_method) || :date
25
+ 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)
35
+ end
36
+ end
37
+ 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)
46
+ 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
57
+ options
58
+ end
59
+
60
+ end
61
+
62
+ 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)
72
+ end
73
+
74
+ def each_day
75
+ first_day.upto(last_day) do |day|
76
+ yield(day)
77
+ end
78
+ end
79
+
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
108
+ end
109
+
110
+ 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
128
+ 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
+ end
138
+
139
+ end
@@ -0,0 +1,125 @@
1
+ module TableHelper
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>')
12
+ end
13
+
14
+ 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
19
+ @objects, @template, @options = objects, template, options
20
+ 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
35
+ 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
45
+ end
46
+
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
53
+ end
54
+
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
73
+ end
74
+
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
84
+ end
85
+
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
95
+ end
96
+
97
+
98
+ private
99
+
100
+ def options_from_hash(args)
101
+ args.last.is_a?(Hash) ? args.pop : {}
102
+ end
103
+
104
+ def concat(tag)
105
+ @template.concat(tag)
106
+ end
107
+
108
+ def content_tag(tag, content, *args)
109
+ options = options_from_hash(args)
110
+ @template.content_tag(tag, content, options)
111
+ 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
+ end
125
+ end
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{table_builder}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Petrik de Heus"]
12
+ s.date = %q{2010-06-29}
13
+ s.description = %q{Rails builder for creating tables and calendars inspired by ActionView's FormBuilder.}
14
+ s.email = %q{}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".autotest",
20
+ ".gitignore",
21
+ "MIT-LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "init.rb",
26
+ "lib/table_builder.rb",
27
+ "lib/table_builder/calendar_helper.rb",
28
+ "lib/table_builder/table_builder.rb",
29
+ "table_builder.gemspec",
30
+ "test/calendar_helper_test.rb",
31
+ "test/table_builder_test.rb",
32
+ "test/test_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/maca/table_builder}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.6}
38
+ s.summary = %q{Rails builder for creating tables and calendars inspired by ActionView's FormBuilder.}
39
+ s.test_files = [
40
+ "test/calendar_helper_test.rb",
41
+ "test/table_builder_test.rb",
42
+ "test/test_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ else
51
+ end
52
+ else
53
+ end
54
+ end
55
+
@@ -0,0 +1,162 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class CalendarHelperTest < Test::Unit::TestCase
4
+ include ActionView::Helpers::TextHelper
5
+ include ActionView::Helpers::TagHelper
6
+ include ActionController::TestCase::Assertions
7
+ include CalendarHelper
8
+ attr_accessor :output_buffer
9
+
10
+ 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
22
+ end
23
+
24
+ def test_calendar_for_without_an_array
25
+ self.output_buffer = ''
26
+ assert_raises(ArgumentError) do
27
+ calendar_for('a') {|t| }
28
+ end
29
+ end
30
+
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
57
+ 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)
75
+ end
76
+ 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
95
+ 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
+
110
+ class CalendarHelperTest < Test::Unit::TestCase
111
+
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
116
+
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)
123
+ end
124
+
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
131
+
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
138
+
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
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
150
+
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
+ 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
+ end
161
+
162
+ class Event < Struct.new(:id, :name, :date); end
@@ -0,0 +1,185 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class TableBuilderTest < Test::Unit::TestCase
4
+ include ActionView::Helpers::TextHelper
5
+ include ActionView::Helpers::TagHelper
6
+ include ActionController::TestCase::Assertions
7
+ include TableHelper
8
+ attr_accessor :output_buffer
9
+
10
+ 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 = ''
16
+ end
17
+
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
24
+ end
25
+
26
+ def test_table_for_without_an_array_raises_error
27
+ assert_raises(ArgumentError) do
28
+ table_for('a') {|t| }
29
+ 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
40
+ 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
58
+ 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')
73
+ 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
+
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
93
+ 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
+
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
115
+ 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
+
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
163
+ end
164
+
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
181
+ end
182
+
183
+ end
184
+
185
+ class Drummer < Struct.new(:id, :name); end
@@ -0,0 +1,10 @@
1
+ require 'test/unit'
2
+
3
+ require "rubygems"
4
+ require 'active_support'
5
+ require 'action_pack'
6
+ require 'action_controller'
7
+ require 'action_view'
8
+ require 'action_controller/test_process'
9
+
10
+ require(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'table_builder')))
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: table_builder
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Petrik de Heus
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-29 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Rails builder for creating tables and calendars inspired by ActionView's FormBuilder.
22
+ email: ""
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.rdoc
29
+ files:
30
+ - .autotest
31
+ - .gitignore
32
+ - MIT-LICENSE
33
+ - README.rdoc
34
+ - Rakefile
35
+ - VERSION
36
+ - init.rb
37
+ - lib/table_builder.rb
38
+ - lib/table_builder/calendar_helper.rb
39
+ - lib/table_builder/table_builder.rb
40
+ - table_builder.gemspec
41
+ - test/calendar_helper_test.rb
42
+ - test/table_builder_test.rb
43
+ - test/test_helper.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/maca/table_builder
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.6
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Rails builder for creating tables and calendars inspired by ActionView's FormBuilder.
74
+ test_files:
75
+ - test/calendar_helper_test.rb
76
+ - test/table_builder_test.rb
77
+ - test/test_helper.rb