table_helper 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- 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
data/test/unit/body_test.rb
CHANGED
@@ -2,12 +2,16 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
|
2
2
|
|
3
3
|
class BodyByDefaultTest < Test::Unit::TestCase
|
4
4
|
def setup
|
5
|
-
|
6
|
-
@body = TableHelper::Body.new(
|
5
|
+
@table = TableHelper::CollectionTable.new([])
|
6
|
+
@body = TableHelper::Body.new(@table)
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
|
9
|
+
def test_should_have_a_table
|
10
|
+
assert_equal @table, @body.table
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_should_not_alternate
|
14
|
+
assert_nil @body.alternate
|
11
15
|
end
|
12
16
|
|
13
17
|
def test_should_have_an_empty_caption
|
@@ -16,50 +20,108 @@ class BodyByDefaultTest < Test::Unit::TestCase
|
|
16
20
|
end
|
17
21
|
|
18
22
|
class BodyTest < Test::Unit::TestCase
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
def setup
|
24
|
+
@table = TableHelper::CollectionTable.new([])
|
25
|
+
@body = TableHelper::Body.new(@table)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_should_raise_exception_if_invalid_alternate_specified
|
29
|
+
assert_raise(ArgumentError) {@body.alternate = :invalid}
|
25
30
|
end
|
26
31
|
|
32
|
+
def test_should_not_raise_exception_for_odd_alternate
|
33
|
+
assert_nothing_raised {@body.alternate = :odd}
|
34
|
+
assert_equal :odd, @body.alternate
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_should_not_raise_exception_for_even_alternate
|
38
|
+
assert_nothing_raised {@body.alternate = :even}
|
39
|
+
assert_equal :even, @body.alternate
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class BodyWithEmptyCollectionTest < Test::Unit::TestCase
|
27
44
|
def setup
|
28
|
-
@
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
@
|
34
|
-
|
45
|
+
@table = TableHelper::CollectionTable.new([])
|
46
|
+
@body = TableHelper::Body.new(@table)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_should_have_no_content_if_no_empty_caption
|
50
|
+
@body.empty_caption = nil
|
51
|
+
assert_html_equal '<tbody></tbody>', @body.html
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_should_show_content_if_empty_caption
|
55
|
+
expected = <<-end_str
|
56
|
+
<tbody>
|
57
|
+
<tr class="ui-collection-empty">
|
58
|
+
<td>No matches found.</td>
|
59
|
+
</tr>
|
60
|
+
</tbody>
|
61
|
+
end_str
|
62
|
+
assert_html_equal expected, @body.html
|
35
63
|
end
|
36
64
|
|
37
|
-
def
|
38
|
-
|
65
|
+
def test_should_use_custom_empty_caption_class_if_specified
|
66
|
+
original_empty_caption_class = TableHelper::Body.empty_caption_class
|
67
|
+
TableHelper::Body.empty_caption_class = 'ui-collection-empty_caption'
|
68
|
+
|
69
|
+
expected = <<-end_str
|
70
|
+
<tbody>
|
71
|
+
<tr class="ui-collection-empty_caption">
|
72
|
+
<td>No matches found.</td>
|
73
|
+
</tr>
|
74
|
+
</tbody>
|
75
|
+
end_str
|
76
|
+
assert_html_equal expected, @body.html
|
77
|
+
ensure
|
78
|
+
TableHelper::Body.empty_caption_class = original_empty_caption_class
|
39
79
|
end
|
40
80
|
|
41
|
-
def
|
42
|
-
|
43
|
-
|
81
|
+
def test_should_set_colspan_if_header_has_multiple_columns
|
82
|
+
@table.header :title, :author_name
|
83
|
+
|
84
|
+
expected = <<-end_str
|
85
|
+
<tbody>
|
86
|
+
<tr class="ui-collection-empty">
|
87
|
+
<td colspan="2">No matches found.</td>
|
88
|
+
</tr>
|
89
|
+
</tbody>
|
90
|
+
end_str
|
91
|
+
assert_html_equal expected, @body.html
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
class BodyWithCollectionTest < Test::Unit::TestCase
|
96
|
+
class Post
|
97
|
+
attr_accessor :title
|
98
|
+
|
99
|
+
def initialize(title)
|
100
|
+
@title = title
|
101
|
+
end
|
44
102
|
end
|
45
103
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
104
|
+
def setup
|
105
|
+
@collection = [Post.new('first'), Post.new('second'), Post.new('last')]
|
106
|
+
@table = TableHelper::CollectionTable.new(@collection)
|
107
|
+
@table.header :title
|
108
|
+
|
109
|
+
@body = TableHelper::Body.new(@table)
|
49
110
|
end
|
50
111
|
|
51
112
|
def test_should_build_row_using_object_location_for_default_index
|
52
|
-
|
113
|
+
build_post = nil
|
114
|
+
index = nil
|
115
|
+
@body.each {|row, build_post, index|}
|
53
116
|
|
54
117
|
@collection.each do |post|
|
55
|
-
html = @body.build_row(post)
|
56
|
-
|
57
|
-
|
58
|
-
end
|
118
|
+
html = @body.build_row(post)
|
119
|
+
assert_equal post, build_post
|
120
|
+
assert_equal @collection.index(post), index
|
59
121
|
|
60
122
|
expected = <<-end_str
|
61
|
-
<tr class="
|
62
|
-
<td class="title">#{post.title}</td>
|
123
|
+
<tr class="post ui-collection-result">
|
124
|
+
<td class="post-title">#{post.title}</td>
|
63
125
|
</tr>
|
64
126
|
end_str
|
65
127
|
assert_html_equal expected, html
|
@@ -67,65 +129,45 @@ class BodyTest < Test::Unit::TestCase
|
|
67
129
|
end
|
68
130
|
|
69
131
|
def test_should_build_row_using_custom_value_for_index
|
70
|
-
|
132
|
+
post = nil
|
133
|
+
index = nil
|
134
|
+
@body.each {|row, post, index|}
|
71
135
|
|
72
|
-
html = @body.build_row(@collection.first, 1)
|
73
|
-
|
74
|
-
|
75
|
-
end
|
136
|
+
html = @body.build_row(@collection.first, 1)
|
137
|
+
assert_equal @collection.first, post
|
138
|
+
assert_equal 1, index
|
76
139
|
|
77
140
|
expected = <<-end_str
|
78
|
-
<tr class="
|
79
|
-
<td class="title">first</td>
|
141
|
+
<tr class="post ui-collection-result">
|
142
|
+
<td class="post-title">first</td>
|
80
143
|
</tr>
|
81
144
|
end_str
|
82
145
|
assert_html_equal expected, html
|
83
146
|
end
|
84
147
|
|
85
148
|
def test_should_build_row_with_missing_cells
|
86
|
-
@header
|
87
|
-
@header.column :author_name
|
149
|
+
header = @table.header :author_name
|
88
150
|
|
89
151
|
expected = <<-end_str
|
90
|
-
<tr class="
|
91
|
-
<td class="title">first</td>
|
92
|
-
<td class="author_name empty"></td>
|
152
|
+
<tr class="post ui-collection-result">
|
153
|
+
<td class="post-title">first</td>
|
154
|
+
<td class="post-author_name ui-state-empty"></td>
|
93
155
|
</tr>
|
94
156
|
end_str
|
95
157
|
assert_html_equal expected, @body.build_row(@collection.first)
|
96
158
|
end
|
97
159
|
|
98
|
-
def
|
99
|
-
@header.column :title
|
100
|
-
|
101
|
-
expected = <<-end_str
|
102
|
-
<tr class="row">
|
103
|
-
<td class="title">first</td>
|
104
|
-
</tr>
|
105
|
-
<tr class="row">
|
106
|
-
<td class="title">second</td>
|
107
|
-
</tr>
|
108
|
-
<tr class="row">
|
109
|
-
<td class="title">last</td>
|
110
|
-
</tr>
|
111
|
-
end_str
|
112
|
-
assert_html_equal expected, @body.build
|
113
|
-
end
|
114
|
-
|
115
|
-
def test_html_should_use_body_tag
|
116
|
-
@header.column :title
|
117
|
-
@body.build
|
118
|
-
|
160
|
+
def test_should_build_html
|
119
161
|
expected = <<-end_str
|
120
162
|
<tbody>
|
121
|
-
<tr class="
|
122
|
-
<td class="title">first</td>
|
163
|
+
<tr class="post ui-collection-result">
|
164
|
+
<td class="post-title">first</td>
|
123
165
|
</tr>
|
124
|
-
<tr class="
|
125
|
-
<td class="title">second</td>
|
166
|
+
<tr class="post ui-collection-result">
|
167
|
+
<td class="post-title">second</td>
|
126
168
|
</tr>
|
127
|
-
<tr class="
|
128
|
-
<td class="title">last</td>
|
169
|
+
<tr class="post ui-collection-result">
|
170
|
+
<td class="post-title">last</td>
|
129
171
|
</tr>
|
130
172
|
</tbody>
|
131
173
|
end_str
|
@@ -133,20 +175,18 @@ class BodyTest < Test::Unit::TestCase
|
|
133
175
|
end
|
134
176
|
|
135
177
|
def test_should_include_custom_attributes_in_body_tag
|
136
|
-
@header.column :title
|
137
178
|
@body[:class] = 'pretty'
|
138
|
-
@body.build
|
139
179
|
|
140
180
|
expected = <<-end_str
|
141
181
|
<tbody class="pretty">
|
142
|
-
<tr class="
|
143
|
-
<td class="title">first</td>
|
182
|
+
<tr class="post ui-collection-result">
|
183
|
+
<td class="post-title">first</td>
|
144
184
|
</tr>
|
145
|
-
<tr class="
|
146
|
-
<td class="title">second</td>
|
185
|
+
<tr class="post ui-collection-result">
|
186
|
+
<td class="post-title">second</td>
|
147
187
|
</tr>
|
148
|
-
<tr class="
|
149
|
-
<td class="title">last</td>
|
188
|
+
<tr class="post ui-collection-result">
|
189
|
+
<td class="post-title">last</td>
|
150
190
|
</tr>
|
151
191
|
</tbody>
|
152
192
|
end_str
|
@@ -154,37 +194,33 @@ class BodyTest < Test::Unit::TestCase
|
|
154
194
|
end
|
155
195
|
end
|
156
196
|
|
157
|
-
class
|
197
|
+
class BodyWithCustomBuilderTest < Test::Unit::TestCase
|
158
198
|
def setup
|
159
|
-
@collection = []
|
160
|
-
@
|
161
|
-
@
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
<td>No matches found.</td>
|
168
|
-
</tr>
|
169
|
-
end_str
|
170
|
-
assert_html_equal expected, @body.build
|
171
|
-
end
|
172
|
-
|
173
|
-
def test_should_be_empty_if_no_empty_caption
|
174
|
-
@body.empty_caption = nil
|
175
|
-
assert_html_equal '', @body.build
|
199
|
+
@collection = [Object.new, Object.new, Object.new]
|
200
|
+
@table = TableHelper::CollectionTable.new(@collection)
|
201
|
+
@table.header :index
|
202
|
+
|
203
|
+
@body = TableHelper::Body.new(@table)
|
204
|
+
@body.each do |row, object, index|
|
205
|
+
row.index index.to_s
|
206
|
+
end
|
176
207
|
end
|
177
208
|
|
178
|
-
def
|
179
|
-
@header.column :title
|
180
|
-
@header.column :author_name
|
181
|
-
|
209
|
+
def test_should_use_custom_builder
|
182
210
|
expected = <<-end_str
|
183
|
-
<
|
184
|
-
<
|
185
|
-
|
211
|
+
<tbody>
|
212
|
+
<tr class="object ui-collection-result">
|
213
|
+
<td class="object-index">0</td>
|
214
|
+
</tr>
|
215
|
+
<tr class="object ui-collection-result">
|
216
|
+
<td class="object-index">1</td>
|
217
|
+
</tr>
|
218
|
+
<tr class="object ui-collection-result">
|
219
|
+
<td class="object-index">2</td>
|
220
|
+
</tr>
|
221
|
+
</tbody>
|
186
222
|
end_str
|
187
|
-
assert_html_equal expected, @body.
|
223
|
+
assert_html_equal expected, @body.html
|
188
224
|
end
|
189
225
|
end
|
190
226
|
|
@@ -198,22 +234,18 @@ class BodyWithAlternatingEvenRowsTest < Test::Unit::TestCase
|
|
198
234
|
end
|
199
235
|
|
200
236
|
def setup
|
201
|
-
@collection = [
|
202
|
-
|
203
|
-
|
204
|
-
Post.new('last')
|
205
|
-
]
|
206
|
-
@header = TableHelper::Header.new(@collection)
|
207
|
-
@header.column :title
|
237
|
+
@collection = [Post.new('first'), Post.new('second'), Post.new('last')]
|
238
|
+
table = TableHelper::CollectionTable.new(@collection)
|
239
|
+
table.header :title
|
208
240
|
|
209
|
-
@body = TableHelper::Body.new(
|
210
|
-
@body.
|
241
|
+
@body = TableHelper::Body.new(table)
|
242
|
+
@body.alternate = :even
|
211
243
|
end
|
212
244
|
|
213
245
|
def test_should_alternate_even_row
|
214
246
|
expected = <<-end_str
|
215
|
-
<tr class="
|
216
|
-
<td class="title">first</td>
|
247
|
+
<tr class="post ui-collection-result ui-state-alternate">
|
248
|
+
<td class="post-title">first</td>
|
217
249
|
</tr>
|
218
250
|
end_str
|
219
251
|
assert_html_equal expected, @body.build_row(@collection.first)
|
@@ -221,8 +253,8 @@ class BodyWithAlternatingEvenRowsTest < Test::Unit::TestCase
|
|
221
253
|
|
222
254
|
def test_should_not_alternate_odd_row
|
223
255
|
expected = <<-end_str
|
224
|
-
<tr class="
|
225
|
-
<td class="title">second</td>
|
256
|
+
<tr class="post ui-collection-result">
|
257
|
+
<td class="post-title">second</td>
|
226
258
|
</tr>
|
227
259
|
end_str
|
228
260
|
assert_html_equal expected, @body.build_row(@collection[1])
|
@@ -239,22 +271,18 @@ class BodyWithAlternatingOddRowsTest < Test::Unit::TestCase
|
|
239
271
|
end
|
240
272
|
|
241
273
|
def setup
|
242
|
-
@collection = [
|
243
|
-
|
244
|
-
|
245
|
-
Post.new('last')
|
246
|
-
]
|
247
|
-
@header = TableHelper::Header.new(@collection)
|
248
|
-
@header.column :title
|
274
|
+
@collection = [Post.new('first'), Post.new('second'), Post.new('last')]
|
275
|
+
table = TableHelper::CollectionTable.new(@collection)
|
276
|
+
table.header :title
|
249
277
|
|
250
|
-
@body = TableHelper::Body.new(
|
251
|
-
@body.
|
278
|
+
@body = TableHelper::Body.new(table)
|
279
|
+
@body.alternate = :odd
|
252
280
|
end
|
253
281
|
|
254
282
|
def test_should_alternate_odd_row
|
255
283
|
expected = <<-end_str
|
256
|
-
<tr class="
|
257
|
-
<td class="title">second</td>
|
284
|
+
<tr class="post ui-collection-result ui-state-alternate">
|
285
|
+
<td class="post-title">second</td>
|
258
286
|
</tr>
|
259
287
|
end_str
|
260
288
|
assert_html_equal expected, @body.build_row(@collection[1])
|
@@ -262,8 +290,8 @@ class BodyWithAlternatingOddRowsTest < Test::Unit::TestCase
|
|
262
290
|
|
263
291
|
def test_should_not_alternate_even_row
|
264
292
|
expected = <<-end_str
|
265
|
-
<tr class="
|
266
|
-
<td class="title">first</td>
|
293
|
+
<tr class="post ui-collection-result">
|
294
|
+
<td class="post-title">first</td>
|
267
295
|
</tr>
|
268
296
|
end_str
|
269
297
|
assert_html_equal expected, @body.build_row(@collection.first)
|
data/test/unit/cell_test.rb
CHANGED
@@ -5,38 +5,108 @@ class CellByDefaultTest < Test::Unit::TestCase
|
|
5
5
|
@cell = TableHelper::Cell.new(:name)
|
6
6
|
end
|
7
7
|
|
8
|
+
def test_should_have_content
|
9
|
+
assert_equal 'Name', @cell.content
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_should_have_a_content_type
|
13
|
+
assert_equal :data, @cell.content_type
|
14
|
+
end
|
15
|
+
|
8
16
|
def test_should_have_a_class_name
|
9
17
|
assert_equal 'name', @cell[:class]
|
10
18
|
end
|
11
19
|
|
12
|
-
def
|
20
|
+
def test_should_build_html
|
13
21
|
assert_equal '<td class="name">Name</td>', @cell.html
|
14
22
|
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class CellTest < Test::Unit::TestCase
|
26
|
+
def test_should_raise_exception_if_content_type_is_invalid
|
27
|
+
assert_raise(ArgumentError) {TableHelper::Cell.new(:name).content_type = :invalid}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class CellWithCustomContentTest < Test::Unit::TestCase
|
32
|
+
def setup
|
33
|
+
@cell = TableHelper::Cell.new(:name, 'John Smith')
|
34
|
+
end
|
15
35
|
|
16
|
-
def
|
17
|
-
assert_equal '
|
36
|
+
def test_should_use_custom_content
|
37
|
+
assert_equal 'John Smith', @cell.content
|
18
38
|
end
|
19
39
|
end
|
20
40
|
|
21
|
-
class
|
22
|
-
def
|
23
|
-
cell = TableHelper::Cell.new(:name,
|
24
|
-
|
41
|
+
class CellWithEmptyContentTest < Test::Unit::TestCase
|
42
|
+
def setup
|
43
|
+
@cell = TableHelper::Cell.new(:name, nil)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_should_include_empty_class
|
47
|
+
assert_equal 'name ui-state-empty', @cell[:class]
|
25
48
|
end
|
26
49
|
|
27
|
-
def
|
28
|
-
|
29
|
-
|
50
|
+
def test_should_use_custom_empty_class_if_specified
|
51
|
+
original_empty_class = TableHelper::Cell.empty_class
|
52
|
+
TableHelper::Cell.empty_class = 'ui-state-blank'
|
53
|
+
|
54
|
+
cell = TableHelper::Cell.new(:name, nil)
|
55
|
+
assert_equal 'name ui-state-blank', cell[:class]
|
56
|
+
ensure
|
57
|
+
TableHelper::Cell.empty_class = original_empty_class
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_should_build_html
|
61
|
+
assert_equal '<td class="name ui-state-empty"></td>', @cell.html
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class CellWithCustomHtmlOptionsTest < Test::Unit::TestCase
|
66
|
+
def setup
|
67
|
+
@cell = TableHelper::Cell.new(:name, :style => 'float: left;')
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_should_include_custom_options
|
71
|
+
assert_equal 'float: left;', @cell[:style]
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_should_build_html
|
75
|
+
assert_equal '<td class="name" style="float: left;">Name</td>', @cell.html
|
30
76
|
end
|
31
77
|
|
32
78
|
def test_should_append_automated_class_name_if_class_already_specified
|
33
|
-
cell = TableHelper::Cell.new(:name,
|
34
|
-
assert_equal 'name
|
35
|
-
assert_equal '<td class="name
|
79
|
+
cell = TableHelper::Cell.new(:name, :class => 'selected')
|
80
|
+
assert_equal 'selected name', cell[:class]
|
81
|
+
assert_equal '<td class="selected name">Name</td>', cell.html
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class CellWithCustomContentAndHtmlOptionsTest < Test::Unit::TestCase
|
86
|
+
def setup
|
87
|
+
@cell = TableHelper::Cell.new(:name, 'John Smith', :style => 'float: left;')
|
36
88
|
end
|
37
89
|
|
38
|
-
def
|
39
|
-
|
90
|
+
def test_should_use_custom_content
|
91
|
+
assert_equal 'John Smith', @cell.content
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_should_include_custom_options
|
95
|
+
assert_equal 'float: left;', @cell[:style]
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_should_build_html
|
99
|
+
assert_equal '<td class="name" style="float: left;">John Smith</td>', @cell.html
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class CellWithNamespaceTest < Test::Unit::TestCase
|
104
|
+
def setup
|
105
|
+
@cell = TableHelper::Cell.new(:name, :namespace => 'post')
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_should_namespace_class
|
109
|
+
assert_equal 'post-name', @cell[:class]
|
40
110
|
end
|
41
111
|
end
|
42
112
|
|
@@ -46,7 +116,11 @@ class CellWithHeaderContentType < Test::Unit::TestCase
|
|
46
116
|
@cell.content_type = :header
|
47
117
|
end
|
48
118
|
|
49
|
-
def
|
119
|
+
def test_should_have_a_content_type
|
120
|
+
assert_equal :header, @cell.content_type
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_should_build_a_header_cell
|
50
124
|
assert_equal '<th class="name">Name</th>', @cell.html
|
51
125
|
end
|
52
126
|
end
|