table_helper 0.1.0 → 0.2.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.
- data/CHANGELOG.rdoc +10 -0
- data/LICENSE +1 -1
- data/README.rdoc +78 -67
- data/Rakefile +1 -1
- data/lib/table_helper.rb +200 -169
- data/lib/table_helper/body.rb +46 -49
- data/lib/table_helper/body_row.rb +18 -14
- data/lib/table_helper/cell.rb +33 -22
- data/lib/table_helper/collection_table.rb +111 -37
- data/lib/table_helper/footer.rb +9 -7
- data/lib/table_helper/header.rb +31 -93
- data/lib/table_helper/html_element.rb +1 -3
- data/lib/table_helper/row.rb +16 -7
- data/test/helpers/table_helper_test.rb +20 -12
- data/test/unit/body_row_test.rb +66 -16
- data/test/unit/body_test.rb +159 -131
- data/test/unit/cell_test.rb +90 -16
- data/test/unit/collection_table_test.rb +166 -192
- data/test/unit/footer_test.rb +33 -8
- data/test/unit/header_test.rb +136 -114
- data/test/unit/row_builder_test.rb +16 -8
- data/test/unit/row_test.rb +106 -45
- metadata +3 -5
- data/test/unit/header_builder_test.rb +0 -48
@@ -2,7 +2,8 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
|
2
2
|
|
3
3
|
class CollectionTableByDefaultTest < Test::Unit::TestCase
|
4
4
|
def setup
|
5
|
-
@
|
5
|
+
@collection = []
|
6
|
+
@table = TableHelper::CollectionTable.new(@collection)
|
6
7
|
end
|
7
8
|
|
8
9
|
def test_cellspacing_should_be_zero
|
@@ -13,242 +14,215 @@ class CollectionTableByDefaultTest < Test::Unit::TestCase
|
|
13
14
|
assert_equal '0', @table[:cellpadding]
|
14
15
|
end
|
15
16
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
def test_should_have_a_css_class
|
18
|
+
assert_equal 'ui-collection', @table[:class]
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_should_use_custom_collection_css_class_if_specified
|
22
|
+
original_collection_class = TableHelper::CollectionTable.collection_class
|
23
|
+
TableHelper::CollectionTable.collection_class = 'ui-records'
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
25
|
+
table = TableHelper::CollectionTable.new(@collection)
|
26
|
+
assert_equal 'ui-records', table[:class]
|
27
|
+
ensure
|
28
|
+
TableHelper::CollectionTable.collection_class = original_collection_class
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_should_not_have_a_klass
|
32
|
+
assert_nil @table.klass
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_should_not_have_an_object_name
|
36
|
+
assert_nil @table.object_name
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_should_have_a_collection
|
40
|
+
assert_equal @collection, @table.collection
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_should_have_a_header
|
44
|
+
assert_not_nil @table.header
|
45
|
+
assert_instance_of TableHelper::Header, @table.header
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_should_have_a_body
|
49
|
+
assert_not_nil @table.rows
|
50
|
+
assert_instance_of TableHelper::Body, @table.rows
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_should_have_a_footer
|
54
|
+
assert_not_nil @table.footer
|
55
|
+
assert_instance_of TableHelper::Footer, @table.footer
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_should_be_empty
|
59
|
+
assert @table.empty?
|
33
60
|
end
|
34
61
|
end
|
35
62
|
|
36
63
|
class CollectionTableTest < Test::Unit::TestCase
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
attr_accessor :title
|
64
|
+
def test_should_accept_block_with_self_as_argument
|
65
|
+
args = nil
|
66
|
+
table = TableHelper::CollectionTable.new([]) {|*args|}
|
43
67
|
|
44
|
-
|
45
|
-
@title = title
|
46
|
-
end
|
68
|
+
assert_equal [table], args
|
47
69
|
end
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class CollectionTableWithClassDetectionTest < Test::Unit::TestCase
|
73
|
+
class Post
|
53
74
|
end
|
54
75
|
|
55
76
|
class Reflection
|
56
77
|
def klass
|
57
|
-
|
78
|
+
Post
|
58
79
|
end
|
59
80
|
end
|
60
81
|
|
61
|
-
class
|
82
|
+
class PostCollection < Array
|
62
83
|
def proxy_reflection
|
63
84
|
Reflection.new
|
64
85
|
end
|
65
86
|
end
|
66
87
|
|
88
|
+
def test_should_not_detect_class_if_collection_is_empty_vanilla_array
|
89
|
+
table = TableHelper::CollectionTable.new([])
|
90
|
+
assert_nil table.klass
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_should_detect_class_if_collection_has_objects
|
94
|
+
table = TableHelper::CollectionTable.new([Post.new])
|
95
|
+
assert_equal Post, table.klass
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_should_detect_class_if_collection_is_model_proxy
|
99
|
+
table = TableHelper::CollectionTable.new(PostCollection.new)
|
100
|
+
assert_equal Post, table.klass
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
class CollectionTableWithClassTest < Test::Unit::TestCase
|
105
|
+
class Post
|
106
|
+
end
|
107
|
+
|
67
108
|
def setup
|
68
|
-
@
|
69
|
-
Post.new('first'),
|
70
|
-
Post.new('second'),
|
71
|
-
Post.new('last')
|
72
|
-
]
|
109
|
+
@table = TableHelper::CollectionTable.new([], Post)
|
73
110
|
end
|
74
111
|
|
75
|
-
def
|
76
|
-
|
112
|
+
def test_should_have_klass
|
113
|
+
assert_equal Post, @table.klass
|
77
114
|
end
|
78
115
|
|
79
|
-
def
|
80
|
-
|
81
|
-
html = table.build do |body|
|
82
|
-
assert_instance_of TableHelper::Body, body
|
83
|
-
end
|
84
|
-
|
85
|
-
expected = <<-end_str
|
86
|
-
<tbody>
|
87
|
-
<tr class="row">
|
88
|
-
<td class="title">first</td>
|
89
|
-
</tr>
|
90
|
-
<tr class="row">
|
91
|
-
<td class="title">second</td>
|
92
|
-
</tr>
|
93
|
-
<tr class="row">
|
94
|
-
<td class="title">last</td>
|
95
|
-
</tr>
|
96
|
-
</tbody>
|
97
|
-
end_str
|
98
|
-
assert_html_equal expected, html
|
116
|
+
def test_should_have_object_name
|
117
|
+
assert_equal 'post', @table.object_name
|
99
118
|
end
|
100
119
|
|
101
|
-
def
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
120
|
+
def test_should_include_pluralized_object_name_in_css_class
|
121
|
+
assert_equal 'posts ui-collection', @table[:class]
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
class CollectionTableWithEmptyCollectionTest < Test::Unit::TestCase
|
126
|
+
def setup
|
127
|
+
@table = TableHelper::CollectionTable.new([])
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_should_be_empty
|
131
|
+
assert @table.empty?
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_should_build_html
|
108
135
|
expected = <<-end_str
|
109
|
-
<
|
110
|
-
<
|
111
|
-
<
|
112
|
-
</
|
113
|
-
|
114
|
-
<td class="title">second</td>
|
115
|
-
</tr>
|
116
|
-
<tr class="row">
|
117
|
-
<td class="title">last</td>
|
118
|
-
</tr>
|
119
|
-
</tbody>
|
120
|
-
<tfoot>
|
121
|
-
<tr>
|
122
|
-
</tr>
|
123
|
-
</tfoot>
|
136
|
+
<table cellpadding="0" cellspacing="0" class="ui-collection">
|
137
|
+
<tbody>
|
138
|
+
<tr class="ui-collection-empty"><td>No matches found.</td></tr>
|
139
|
+
</tbody>
|
140
|
+
</table>
|
124
141
|
end_str
|
125
|
-
assert_html_equal expected, html
|
142
|
+
assert_html_equal expected, @table.html
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
class CollectionTableWithObjectsTest < Test::Unit::TestCase
|
147
|
+
def setup
|
148
|
+
@table = TableHelper::CollectionTable.new([Object.new])
|
126
149
|
end
|
127
150
|
|
128
|
-
def
|
129
|
-
table
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
151
|
+
def test_should_not_be_empty
|
152
|
+
assert !@table.empty?
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
class CollectionTableHeaderTest < Test::Unit::TestCase
|
157
|
+
def setup
|
158
|
+
@table = TableHelper::CollectionTable.new([Object.new])
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_should_not_include_in_html_if_none_specified
|
136
162
|
expected = <<-end_str
|
137
|
-
<
|
138
|
-
<
|
139
|
-
<
|
140
|
-
</
|
141
|
-
</
|
142
|
-
<tbody>
|
143
|
-
<tr class="row">
|
144
|
-
<td class="title">first</td>
|
145
|
-
</tr>
|
146
|
-
<tr class="row">
|
147
|
-
<td class="title">second</td>
|
148
|
-
</tr>
|
149
|
-
<tr class="row">
|
150
|
-
<td class="title">last</td>
|
151
|
-
</tr>
|
152
|
-
</tbody>
|
153
|
-
<tfoot>
|
154
|
-
<tr>
|
155
|
-
</tr>
|
156
|
-
</tfoot>
|
163
|
+
<table cellpadding="0" cellspacing="0" class="objects ui-collection">
|
164
|
+
<tbody>
|
165
|
+
<tr class="object ui-collection-result"></tr>
|
166
|
+
</tbody>
|
167
|
+
</table>
|
157
168
|
end_str
|
158
|
-
assert_html_equal expected, html
|
169
|
+
assert_html_equal expected, @table.html
|
159
170
|
end
|
160
171
|
|
161
|
-
def
|
162
|
-
table
|
163
|
-
html = table.build
|
172
|
+
def test_should_include_in_html_if_specified
|
173
|
+
@table.header :name, :title
|
164
174
|
|
165
175
|
expected = <<-end_str
|
166
|
-
<
|
167
|
-
<
|
168
|
-
<
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
<
|
174
|
-
<
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
</tr>
|
181
|
-
<tr class="row">
|
182
|
-
<td class="title">last</td>
|
183
|
-
<td class="author_name empty"></td>
|
184
|
-
</tr>
|
185
|
-
</tbody>
|
176
|
+
<table cellpadding="0" cellspacing="0" class="objects ui-collection">
|
177
|
+
<thead>
|
178
|
+
<tr>
|
179
|
+
<th class="object-name" scope="col">Name</th>
|
180
|
+
<th class="object-title" scope="col">Title</th>
|
181
|
+
</tr>
|
182
|
+
</thead>
|
183
|
+
<tbody>
|
184
|
+
<tr class="object ui-collection-result">
|
185
|
+
<td class="object-name ui-state-empty"></td>
|
186
|
+
<td class="object-title ui-state-empty"></td>
|
187
|
+
</tr>
|
188
|
+
</tbody>
|
189
|
+
</table>
|
186
190
|
end_str
|
187
|
-
assert_html_equal expected, html
|
191
|
+
assert_html_equal expected, @table.html
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
class CollectionTableFooterTest < Test::Unit::TestCase
|
196
|
+
def setup
|
197
|
+
@table = TableHelper::CollectionTable.new([Object.new])
|
188
198
|
end
|
189
199
|
|
190
|
-
def
|
191
|
-
@collection.insert(0, Note.new('zeroth'))
|
192
|
-
table = TableHelper::CollectionTable.new(@collection)
|
193
|
-
html = table.build
|
194
|
-
|
200
|
+
def test_should_not_include_in_html_if_none_specified
|
195
201
|
expected = <<-end_str
|
196
|
-
<
|
197
|
-
<
|
198
|
-
<
|
199
|
-
|
200
|
-
|
201
|
-
</thead>
|
202
|
-
<tbody>
|
203
|
-
<tr class="row">
|
204
|
-
<td class="title">zeroth</td>
|
205
|
-
<td class="author_name empty"></td>
|
206
|
-
</tr>
|
207
|
-
<tr class="row">
|
208
|
-
<td class="title">first</td>
|
209
|
-
<td class="author_name empty"></td>
|
210
|
-
</tr>
|
211
|
-
<tr class="row">
|
212
|
-
<td class="title">second</td>
|
213
|
-
<td class="author_name empty"></td>
|
214
|
-
</tr>
|
215
|
-
<tr class="row">
|
216
|
-
<td class="title">last</td>
|
217
|
-
<td class="author_name empty"></td>
|
218
|
-
</tr>
|
219
|
-
</tbody>
|
202
|
+
<table cellpadding="0" cellspacing="0" class="objects ui-collection">
|
203
|
+
<tbody>
|
204
|
+
<tr class="object ui-collection-result"></tr>
|
205
|
+
</tbody>
|
206
|
+
</table>
|
220
207
|
end_str
|
221
|
-
assert_html_equal expected, html
|
208
|
+
assert_html_equal expected, @table.html
|
222
209
|
end
|
223
210
|
|
224
|
-
def
|
225
|
-
|
226
|
-
collection.concat(@collection)
|
227
|
-
table = TableHelper::CollectionTable.new(collection)
|
228
|
-
html = table.build
|
211
|
+
def test_should_include_in_html_if_specified
|
212
|
+
@table.footer :total, 1
|
229
213
|
|
230
214
|
expected = <<-end_str
|
231
|
-
<
|
232
|
-
<
|
233
|
-
<
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
</tr>
|
242
|
-
<tr class="row">
|
243
|
-
<td class="title">second</td>
|
244
|
-
<td class="author_name empty"></td>
|
245
|
-
</tr>
|
246
|
-
<tr class="row">
|
247
|
-
<td class="title">last</td>
|
248
|
-
<td class="author_name empty"></td>
|
249
|
-
</tr>
|
250
|
-
</tbody>
|
215
|
+
<table cellpadding="0" cellspacing="0" class="objects ui-collection">
|
216
|
+
<tbody>
|
217
|
+
<tr class="object ui-collection-result"></tr>
|
218
|
+
</tbody>
|
219
|
+
<tfoot>
|
220
|
+
<tr>
|
221
|
+
<td class="object-total">1</td>
|
222
|
+
</tr>
|
223
|
+
</tfoot>
|
224
|
+
</table>
|
251
225
|
end_str
|
252
|
-
assert_html_equal expected, html
|
226
|
+
assert_html_equal expected, @table.html
|
253
227
|
end
|
254
228
|
end
|
data/test/unit/footer_test.rb
CHANGED
@@ -2,17 +2,31 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
|
2
2
|
|
3
3
|
class FooterByDefaultTest < Test::Unit::TestCase
|
4
4
|
def setup
|
5
|
-
@
|
5
|
+
@table = TableHelper::CollectionTable.new([])
|
6
|
+
@footer = TableHelper::Footer.new(@table)
|
6
7
|
end
|
7
8
|
|
8
|
-
def
|
9
|
+
def test_should_hide_when_empty
|
9
10
|
assert @footer.hide_when_empty
|
10
11
|
end
|
12
|
+
|
13
|
+
def test_should_be_empty
|
14
|
+
assert @footer.empty?
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_should_have_a_row
|
18
|
+
assert_not_nil @footer.row
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_should_have_a_table
|
22
|
+
assert_equal @table, @footer.table
|
23
|
+
end
|
11
24
|
end
|
12
25
|
|
13
26
|
class FooterTest < Test::Unit::TestCase
|
14
27
|
def setup
|
15
|
-
|
28
|
+
table = TableHelper::CollectionTable.new([Object.new])
|
29
|
+
@footer = TableHelper::Footer.new(table)
|
16
30
|
end
|
17
31
|
|
18
32
|
def test_should_include_custom_attributes
|
@@ -26,14 +40,24 @@ class FooterTest < Test::Unit::TestCase
|
|
26
40
|
end_str
|
27
41
|
assert_html_equal expected, @footer.html
|
28
42
|
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class FooterWithCellsTest < Test::Unit::TestCase
|
46
|
+
def setup
|
47
|
+
table = TableHelper::CollectionTable.new([Object.new])
|
48
|
+
@footer = TableHelper::Footer.new(table)
|
49
|
+
@cell = @footer.cell :total, 20
|
50
|
+
end
|
29
51
|
|
30
|
-
def
|
31
|
-
|
32
|
-
|
52
|
+
def test_should_namespace_cell_classes
|
53
|
+
assert_equal 'object-total', @cell[:class]
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_should_build_html
|
33
57
|
expected = <<-end_str
|
34
58
|
<tfoot>
|
35
59
|
<tr>
|
36
|
-
<td class="total">20</td>
|
60
|
+
<td class="object-total">20</td>
|
37
61
|
</tr>
|
38
62
|
</tfoot>
|
39
63
|
end_str
|
@@ -43,7 +67,8 @@ end
|
|
43
67
|
|
44
68
|
class FooterWithEmptyCollectionTest < Test::Unit::TestCase
|
45
69
|
def setup
|
46
|
-
|
70
|
+
table = TableHelper::CollectionTable.new([])
|
71
|
+
@footer = TableHelper::Footer.new(table)
|
47
72
|
end
|
48
73
|
|
49
74
|
def test_should_not_display_if_hide_when_empty
|