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.
@@ -2,7 +2,8 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class HeaderByDefaultTest < Test::Unit::TestCase
4
4
  def setup
5
- @header = TableHelper::Header.new([])
5
+ @table = TableHelper::CollectionTable.new([])
6
+ @header = TableHelper::Header.new(@table)
6
7
  end
7
8
 
8
9
  def test_should_hide_when_empty
@@ -10,158 +11,149 @@ class HeaderByDefaultTest < Test::Unit::TestCase
10
11
  end
11
12
 
12
13
  def test_should_have_no_columns
14
+ expected = {}
15
+ assert_equal expected, @header.columns
16
+ end
17
+
18
+ def test_should_have_no_column_names
13
19
  assert_equal [], @header.column_names
14
20
  end
21
+
22
+ def test_should_be_empty
23
+ assert @header.empty?
24
+ end
25
+
26
+ def test_should_have_a_row
27
+ assert_not_nil @header.row
28
+ end
29
+
30
+ def test_should_have_a_table
31
+ assert_equal @table, @header.table
32
+ end
15
33
  end
16
34
 
17
- class HeaderTest < Test::Unit::TestCase
35
+ class HeaderWithColumnDetectionTest < Test::Unit::TestCase
18
36
  class Post
19
37
  def self.column_names
20
38
  ['title', 'author_name']
21
39
  end
22
40
  end
23
41
 
24
- class Reflection
25
- def klass
26
- Post
27
- end
42
+ def test_should_have_columns_if_class_has_column_names
43
+ table = TableHelper::CollectionTable.new([], Post)
44
+ header = TableHelper::Header.new(table)
45
+
46
+ assert_equal ['title', 'author_name'], header.column_names
28
47
  end
29
48
 
30
- class PostCollection < Array
31
- def proxy_reflection
32
- Reflection.new
49
+ def test_should_not_have_columns_if_class_has_no_column_names
50
+ table = TableHelper::CollectionTable.new([], Array)
51
+ header = TableHelper::Header.new(table)
52
+
53
+ assert header.columns.empty?
54
+ end
55
+ end
56
+
57
+ class HeaderWithAutomaticColumnsTest < Test::Unit::TestCase
58
+ class Post
59
+ def self.column_names
60
+ ['title', 'author_name']
33
61
  end
34
62
  end
35
63
 
36
- def test_should_use_class_columns_if_class_has_column_names
37
- header = TableHelper::Header.new([], Post)
38
- assert_equal ['title', 'author_name'], header.column_names
64
+ def setup
65
+ table = TableHelper::CollectionTable.new([Post.new], Post)
66
+ @header = TableHelper::Header.new(table)
39
67
  end
40
68
 
41
- def test_should_have_no_column_names_if_class_has_no_column_names
42
- header = TableHelper::Header.new([], Array)
43
- assert_equal [], header.column_names
69
+ def test_should_use_titleized_name_for_content
70
+ assert_equal 'Title', @header.columns['title'].content
71
+ assert_equal 'Author Name', @header.columns['author_name'].content
44
72
  end
45
73
 
46
- def test_should_use_class_columns_if_collection_has_objects
47
- header = TableHelper::Header.new([Post.new])
48
- assert_equal ['title', 'author_name'], header.column_names
74
+ def test_should_namespace_html_classes
75
+ assert_equal 'post-title', @header.columns['title'][:class]
76
+ assert_equal 'post-author_name', @header.columns['author_name'][:class]
49
77
  end
50
78
 
51
- def test_should_use_class_columns_if_collection_is_proxy
52
- header = TableHelper::Header.new(PostCollection.new)
53
- assert_equal ['title', 'author_name'], header.column_names
79
+ def test_should_not_be_empty
80
+ assert !@header.empty?
54
81
  end
55
82
 
56
- def test_should_create_column_readers_if_column_names_found
57
- header = TableHelper::Header.new([], Post)
58
-
59
- assert_nothing_raised {header.builder.title}
60
- assert_instance_of TableHelper::Cell, header.builder.title
61
-
62
- assert_nothing_raised {header.builder.title}
63
- assert_instance_of TableHelper::Cell, header.builder.author_name
83
+ def test_should_build_html
84
+ expected = <<-end_str
85
+ <thead>
86
+ <tr>
87
+ <th class="post-title" scope="col">Title</th>
88
+ <th class="post-author_name" scope="col">Author Name</th>
89
+ </tr>
90
+ </thead>
91
+ end_str
92
+ assert_html_equal expected, @header.html
64
93
  end
65
94
 
66
- def test_should_create_column_reader_when_column_is_created
67
- header = TableHelper::Header.new([])
68
- header.column :title
95
+ def test_should_clear_existing_columns_when_first_column_is_created
96
+ cell = @header.column :created_on
69
97
 
70
- assert_equal ['title'], header.column_names
71
- assert_instance_of TableHelper::Cell, header.builder.title
72
- end
73
-
74
- def test_should_set_column_scope
75
- header = TableHelper::Header.new([])
76
- header.column :title
77
- assert_equal 'col', header.columns['title'][:scope]
98
+ assert_raise(NoMethodError) {@header.builder.title}
99
+ assert_raise(NoMethodError) {@header.builder.author_name}
100
+
101
+ expected = {'created_on' => cell}
102
+ assert_equal expected, @header.columns
78
103
  end
79
-
80
- def test_should_allow_html_options_to_be_specified_for_new_columns
81
- header = TableHelper::Header.new([])
82
- header.column :title, 'Title', :class => 'pretty'
83
- assert_equal 'title pretty', header.columns['title'][:class]
104
+ end
105
+
106
+ class HeaderWithCustomColumnsTest < Test::Unit::TestCase
107
+ def setup
108
+ table = TableHelper::CollectionTable.new([])
109
+ @header = TableHelper::Header.new(table)
110
+ @title = @header.column :title
84
111
  end
85
112
 
86
- def test_should_use_column_name_for_default_content
87
- header = TableHelper::Header.new([])
88
- header.column :title
89
- assert_equal '<th class="title" scope="col">Title</th>', header.columns['title'].html
113
+ def test_should_set_scope
114
+ assert_equal 'col', @title[:scope]
90
115
  end
91
116
 
92
- def test_should_sanitize_column_names
93
- header = TableHelper::Header.new([])
94
- header.column 'the-title'
95
-
96
- assert_nothing_raised {header.builder.the_title}
97
- assert_instance_of TableHelper::Cell, header.builder.the_title
117
+ def test_should_use_name_for_default_content
118
+ assert_equal 'Title', @title.content
98
119
  end
99
120
 
100
- def test_should_clear_existing_columns_when_first_column_is_created
101
- header = TableHelper::Header.new([], Post)
102
- assert_equal ['title', 'author_name'], header.column_names
103
-
104
- header.column :created_on
105
-
106
- assert_raise(NoMethodError) {header.builder.title}
107
- assert_raise(NoMethodError) {header.builder.author_name}
108
- assert_equal ['created_on'], header.column_names
121
+ def test_should_allow_content_to_be_customized
122
+ title = @header.column :title, 'The Title'
123
+ assert_equal 'The Title', title.content
109
124
  end
110
125
 
111
- def test_should_include_html_options
112
- header = TableHelper::Header.new([Post.new])
113
- header[:class] = 'pretty'
114
-
115
- expected = <<-end_str
116
- <thead class="pretty">
117
- <tr>
118
- <th class="title" scope="col">Title</th>
119
- <th class="author_name" scope="col">Author Name</th>
120
- </tr>
121
- </thead>
122
- end_str
123
- assert_html_equal expected, header.html
126
+ def test_should_allow_html_options_to_be_customized
127
+ title = @header.column :title, :class => 'pretty'
128
+ assert_equal 'pretty title', title[:class]
124
129
  end
125
130
 
126
- def test_should_include_html_options_for_header_row
127
- header = TableHelper::Header.new([Post.new])
128
- header.row[:class] = 'pretty'
129
-
130
- expected = <<-end_str
131
- <thead>
132
- <tr class="pretty">
133
- <th class="title" scope="col">Title</th>
134
- <th class="author_name" scope="col">Author Name</th>
135
- </tr>
136
- </thead>
137
- end_str
138
- assert_html_equal expected, header.html
131
+ def test_should_not_be_empty
132
+ assert !@header.empty?
139
133
  end
140
134
  end
141
135
 
142
- class HeaderWithConflictingColumnNamesTest < Test::Unit::TestCase
136
+ class HeaderWithMultipleColumnsTest < Test::Unit::TestCase
143
137
  def setup
144
- @header = TableHelper::Header.new([])
145
- @header.column 'id'
146
- end
147
-
148
- def test_should_be_able_to_read_cell
149
- assert_instance_of TableHelper::Cell, @header.builder.id
138
+ table = TableHelper::CollectionTable.new([])
139
+ @header = TableHelper::Header.new(table)
140
+ @title, @author_name = @header.column :title, :author_name, :class => 'pretty'
150
141
  end
151
142
 
152
- def test_should_be_able_to_write_to_cell
153
- @header.builder.id '1'
154
- assert_instance_of TableHelper::Cell, @header.builder.id
143
+ def test_should_use_default_content_for_each
144
+ assert_equal 'Title', @title.content
145
+ assert_equal 'Author Name', @author_name.content
155
146
  end
156
147
 
157
- def test_should_be_able_to_clear
158
- assert_nothing_raised {@header.clear}
148
+ def test_should_share_html_options
149
+ assert_equal 'pretty title', @title[:class]
159
150
  end
160
151
  end
161
152
 
162
153
  class HeaderWithEmptyCollectionTest < Test::Unit::TestCase
163
154
  def setup
164
- @header = TableHelper::Header.new([])
155
+ table = TableHelper::CollectionTable.new([])
156
+ @header = TableHelper::Header.new(table)
165
157
  end
166
158
 
167
159
  def test_should_not_display_if_hide_when_empty
@@ -190,14 +182,10 @@ class HeaderWithEmptyCollectionTest < Test::Unit::TestCase
190
182
  end
191
183
 
192
184
  class HeaderWithCollectionTest < Test::Unit::TestCase
193
- class Post
194
- def self.column_names
195
- ['title', 'author_name']
196
- end
197
- end
198
-
199
185
  def setup
200
- @header = TableHelper::Header.new([Post.new])
186
+ table = TableHelper::CollectionTable.new([Object.new])
187
+ @header = TableHelper::Header.new(table)
188
+ @header.column :title, :author_name
201
189
  end
202
190
 
203
191
  def test_should_display_if_hide_when_empty
@@ -206,8 +194,8 @@ class HeaderWithCollectionTest < Test::Unit::TestCase
206
194
  expected = <<-end_str
207
195
  <thead>
208
196
  <tr>
209
- <th class="title" scope="col">Title</th>
210
- <th class="author_name" scope="col">Author Name</th>
197
+ <th class="object-title" scope="col">Title</th>
198
+ <th class="object-author_name" scope="col">Author Name</th>
211
199
  </tr>
212
200
  </thead>
213
201
  end_str
@@ -220,8 +208,42 @@ class HeaderWithCollectionTest < Test::Unit::TestCase
220
208
  expected = <<-end_str
221
209
  <thead>
222
210
  <tr>
223
- <th class="title" scope="col">Title</th>
224
- <th class="author_name" scope="col">Author Name</th>
211
+ <th class="object-title" scope="col">Title</th>
212
+ <th class="object-author_name" scope="col">Author Name</th>
213
+ </tr>
214
+ </thead>
215
+ end_str
216
+ assert_html_equal expected, @header.html
217
+ end
218
+ end
219
+
220
+ class HeaderWithCustomHtmlOptionsTest < Test::Unit::TestCase
221
+ def setup
222
+ table = TableHelper::CollectionTable.new([Object.new])
223
+ @header = TableHelper::Header.new(table)
224
+ @header.column :title
225
+ end
226
+
227
+ def test_should_include_html_options
228
+ @header[:class] = 'pretty'
229
+
230
+ expected = <<-end_str
231
+ <thead class="pretty">
232
+ <tr>
233
+ <th class="object-title" scope="col">Title</th>
234
+ </tr>
235
+ </thead>
236
+ end_str
237
+ assert_html_equal expected, @header.html
238
+ end
239
+
240
+ def test_should_include_html_options_for_header_row
241
+ @header.row[:class] = 'pretty'
242
+
243
+ expected = <<-end_str
244
+ <thead>
245
+ <tr class="pretty">
246
+ <th class="object-title" scope="col">Title</th>
225
247
  </tr>
226
248
  </thead>
227
249
  end_str
@@ -1,8 +1,16 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
+ class RowBuilderParent < TableHelper::HtmlElement
4
+ attr_reader :table
5
+
6
+ def initialize(table = TableHelper::CollectionTable.new([]))
7
+ @table = table
8
+ end
9
+ end
10
+
3
11
  class RowBuilderByDefaultTest < Test::Unit::TestCase
4
12
  def setup
5
- @row = TableHelper::Row.new
13
+ @row = TableHelper::Row.new(RowBuilderParent.new)
6
14
  @builder = TableHelper::RowBuilder.new(@row)
7
15
  end
8
16
 
@@ -13,29 +21,29 @@ end
13
21
 
14
22
  class RowBuilderWithCellsTest < Test::Unit::TestCase
15
23
  def setup
16
- @row = TableHelper::Row.new
24
+ @row = TableHelper::Row.new(RowBuilderParent.new)
17
25
  @builder = TableHelper::RowBuilder.new(@row)
18
26
  @builder.define_cell('first-name')
19
27
  end
20
28
 
21
29
  def test_should_create_cell_reader
22
- assert_nothing_raised {@builder.first_name}
30
+ assert @builder.respond_to?(:first_name)
23
31
  end
24
32
 
25
33
  def test_should_read_cell_without_arguments
26
- @row.cells['first-name'] = TableHelper::Cell.new('first-name')
27
- assert_instance_of TableHelper::Cell, @builder.first_name
34
+ cell = @row.cells['first-name'] = TableHelper::Cell.new('first-name')
35
+ assert_equal cell, @builder.first_name
28
36
  end
29
37
 
30
38
  def test_should_write_cell_with_arguments
31
- @builder.first_name 'Your Name'
32
- assert_equal '<td class="first-name">Your Name</td>', @row.cells['first-name'].html
39
+ cell = @builder.first_name 'Your Name'
40
+ assert_equal 'Your Name', cell.content
33
41
  end
34
42
  end
35
43
 
36
44
  class RowBuilderAfterUndefiningACellTest < Test::Unit::TestCase
37
45
  def setup
38
- @row = TableHelper::Row.new
46
+ @row = TableHelper::Row.new(RowBuilderParent.new)
39
47
  @builder = TableHelper::RowBuilder.new(@row)
40
48
  @builder.define_cell('first-name')
41
49
  @builder.undef_cell('first-name')
@@ -1,11 +1,20 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
+ class RowParent < TableHelper::HtmlElement
4
+ attr_reader :table
5
+
6
+ def initialize(table = TableHelper::CollectionTable.new([]))
7
+ @table = table
8
+ end
9
+ end
10
+
3
11
  class RowByDefaultTest < Test::Unit::TestCase
4
12
  def setup
5
- @row = TableHelper::Row.new
13
+ @parent = RowParent.new
14
+ @row = TableHelper::Row.new(@parent)
6
15
  end
7
16
 
8
- def test_should_not_set_any_html_options
17
+ def test_should_not_have_class
9
18
  assert_nil @row[:class]
10
19
  end
11
20
 
@@ -20,48 +29,23 @@ class RowByDefaultTest < Test::Unit::TestCase
20
29
  def test_should_have_a_builder
21
30
  assert_not_nil @row.builder
22
31
  end
23
- end
24
-
25
- class RowTest < Test::Unit::TestCase
26
- def setup
27
- @row = TableHelper::Row.new
28
- end
29
-
30
- def test_should_create_cell_reader_after_building_cell
31
- @row.cell :name
32
- assert_nothing_raised {@row.builder.name}
33
- assert_instance_of TableHelper::Cell, @row.builder.name
34
- end
35
32
 
36
- def test_should_use_cell_name_for_class_name
37
- @row.cell :name
38
- assert_equal 'name', @row.cells['name'][:class]
33
+ def test_should_have_a_parent
34
+ assert_equal @parent, @row.parent
39
35
  end
40
36
 
41
- def test_should_sanitize_cell_name_for_reader_method
42
- @row.cell 'the-name'
43
-
44
- @row.builder.the_name
45
- assert_nothing_raised {@row.builder.the_name}
46
- assert_instance_of TableHelper::Cell, @row.builder.the_name
37
+ def test_should_have_a_table
38
+ assert_equal @parent.table, @row.table
47
39
  end
48
40
 
49
- def test_should_use_unsanitized_cell_name_for_cell_class
50
- @row.cell 'the-name'
51
- assert_equal ['the-name'], @row.cell_names
52
- assert_equal 'the-name', @row.cells['the-name'][:class]
53
- end
54
-
55
- def test_should_allow_html_options_when_building_cell
56
- @row.cell :name, 'Name', :class => 'pretty'
57
-
58
- assert_equal 'name pretty', @row.cells['name'][:class]
41
+ def test_should_be_empty
42
+ assert @row.empty?
59
43
  end
60
44
  end
61
45
 
62
46
  class RowWithoutCellsTest < Test::Unit::TestCase
63
47
  def setup
64
- @row = TableHelper::Row.new
48
+ @row = TableHelper::Row.new(RowParent.new)
65
49
  end
66
50
 
67
51
  def test_should_be_able_to_clear_cells
@@ -69,6 +53,10 @@ class RowWithoutCellsTest < Test::Unit::TestCase
69
53
  assert_equal [], @row.cell_names
70
54
  end
71
55
 
56
+ def test_should_be_empty
57
+ assert @row.empty?
58
+ end
59
+
72
60
  def test_should_build_html
73
61
  assert_equal '<tr></tr>', @row.html
74
62
  end
@@ -76,14 +64,32 @@ end
76
64
 
77
65
  class RowWithCellsTest < Test::Unit::TestCase
78
66
  def setup
79
- @row = TableHelper::Row.new
80
- @row.cell :name
67
+ @row = TableHelper::Row.new(RowParent.new)
68
+ @cell = @row.cell :name
69
+ end
70
+
71
+ def test_should_create_cell_reader
72
+ assert_nothing_raised {@row.builder.name}
73
+ assert_equal @cell, @row.builder.name
74
+ end
75
+
76
+ def test_should_use_cell_name_for_class_name
77
+ assert_equal 'name', @cell[:class]
78
+ end
79
+
80
+ def test_should_have_cells
81
+ expected = {'name' => @cell}
82
+ assert_equal expected, @row.cells
81
83
  end
82
84
 
83
85
  def test_should_have_cell_names
84
86
  assert_equal ['name'], @row.cell_names
85
87
  end
86
88
 
89
+ def test_should_not_be_empty
90
+ assert !@row.empty?
91
+ end
92
+
87
93
  def test_should_be_able_to_clear_existing_cells
88
94
  @row.clear
89
95
 
@@ -95,26 +101,40 @@ class RowWithCellsTest < Test::Unit::TestCase
95
101
  end
96
102
 
97
103
  def test_should_create_new_cell_if_cell_already_exists
98
- old_cell = @row.cells['name']
99
-
100
- @row.cell :name
101
- assert_not_same old_cell, @row.cells['name']
104
+ new_cell = @row.cell :name
105
+ assert_not_same new_cell, @cell
102
106
  end
103
107
 
104
- def test_should_be_able_to_read_cell_after_clearing
108
+ def test_should_be_able_to_recreate_cell_after_clearing
105
109
  @row.clear
106
110
  @row.cell :name
107
111
 
108
112
  assert_equal ['name'], @row.cell_names
109
113
  assert_nothing_raised {@row.builder.name}
110
114
  end
115
+
116
+ def test_should_allow_html_options
117
+ @row.clear
118
+ cell = @row.cell :name, 'Name', :class => 'pretty'
119
+
120
+ assert_equal 'pretty name', cell[:class]
121
+ end
111
122
  end
112
123
 
113
124
  class RowWithMultipleCellsTest < Test::Unit::TestCase
114
125
  def setup
115
- @row = TableHelper::Row.new
116
- @row.cell :name
117
- @row.cell :location
126
+ @row = TableHelper::Row.new(RowParent.new)
127
+ @name = @row.cell :name
128
+ @location = @row.cell :location
129
+ end
130
+
131
+ def test_should_have_cells
132
+ expected = {'name' => @name, 'location' => @location}
133
+ assert_equal expected, @row.cells
134
+ end
135
+
136
+ def test_should_have_cell_names
137
+ assert_equal ['location', 'name'], @row.cell_names.sort
118
138
  end
119
139
 
120
140
  def test_should_build_html
@@ -122,9 +142,34 @@ class RowWithMultipleCellsTest < Test::Unit::TestCase
122
142
  end
123
143
  end
124
144
 
145
+ class RowWithUnconventionalCellNamesTest < Test::Unit::TestCase
146
+ def setup
147
+ @row = TableHelper::Row.new(RowParent.new)
148
+ @cell = @row.cell 'the-name'
149
+ end
150
+
151
+ def test_should_have_cells
152
+ expected = {'the-name' => @cell}
153
+ assert_equal expected, @row.cells
154
+ end
155
+
156
+ def test_should_have_cell_names
157
+ assert_equal ['the-name'], @row.cell_names
158
+ end
159
+
160
+ def test_should_use_sanitized_name_for_reader_method
161
+ assert_nothing_raised {@row.builder.the_name}
162
+ assert_equal @cell, @row.builder.the_name
163
+ end
164
+
165
+ def test_should_use_unsanitized_name_for_class
166
+ assert_equal 'the-name', @cell[:class]
167
+ end
168
+ end
169
+
125
170
  class RowWithConflictingCellNamesTest < Test::Unit::TestCase
126
171
  def setup
127
- @row = TableHelper::Row.new
172
+ @row = TableHelper::Row.new(RowParent.new)
128
173
  @row.cell :id
129
174
  end
130
175
 
@@ -141,3 +186,19 @@ class RowWithConflictingCellNamesTest < Test::Unit::TestCase
141
186
  assert_nothing_raised {@row.clear}
142
187
  end
143
188
  end
189
+
190
+ class RowWithTableObjectNameTest < Test::Unit::TestCase
191
+ def setup
192
+ table = TableHelper::CollectionTable.new([], Object)
193
+ @row = TableHelper::Row.new(RowParent.new(table))
194
+ @cell = @row.cell :id
195
+ end
196
+
197
+ def test_should_not_have_class
198
+ assert_nil @row[:class]
199
+ end
200
+
201
+ def test_should_namespace_cell
202
+ assert_equal 'object-id', @cell[:class]
203
+ end
204
+ end