table_helper 0.0.5 → 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.
@@ -1,46 +1,44 @@
1
- module PluginAWeek #:nodoc:
2
- module TableHelper
3
- # Represents an HTML element
4
- #
5
- # == Modifying HTML options
6
- #
7
- # HTML options can normally be specified when creating the element.
8
- # However, if they need to be modified after the element has been created,
9
- # you can access the properties like so:
10
- #
11
- # r = Row.new
12
- # r[:style] = 'display: none;'
13
- #
14
- # or for a cell:
15
- #
16
- # c = Cell.new
17
- # c[:style] = 'display: none;'
18
- class HtmlElement
19
- include ActionView::Helpers::TagHelper
20
-
21
- delegate :[],
22
- :[]=,
23
- :to => '@html_options'
24
-
25
- def initialize(html_options = {}) #:nodoc:
26
- @html_options = html_options.symbolize_keys
1
+ module TableHelper
2
+ # Represents an HTML element
3
+ #
4
+ # == Modifying HTML options
5
+ #
6
+ # HTML options can normally be specified when creating the element.
7
+ # However, if they need to be modified after the element has been created,
8
+ # you can access the properties like so:
9
+ #
10
+ # r = Row.new
11
+ # r[:style] = 'display: none;'
12
+ #
13
+ # or for a cell:
14
+ #
15
+ # c = Cell.new
16
+ # c[:style] = 'display: none;'
17
+ class HtmlElement
18
+ include ActionView::Helpers::TagHelper
19
+
20
+ delegate :[],
21
+ :[]=,
22
+ :to => '@html_options'
23
+
24
+ def initialize(html_options = {}) #:nodoc:
25
+ @html_options = html_options.symbolize_keys
26
+ end
27
+
28
+ # Generates the html representing this element
29
+ def html
30
+ content_tag(tag_name, content, @html_options)
31
+ end
32
+
33
+ private
34
+ # The name of the element tag to use (e.g. td, th, tr, etc.)
35
+ def tag_name
36
+ ''
27
37
  end
28
38
 
29
- # Generates the html representing this element
30
- def html
31
- content_tag(tag_name, content, @html_options)
39
+ # The content that will be displayed inside of the tag
40
+ def content
41
+ ''
32
42
  end
33
-
34
- private
35
- # The name of the element tag to use (e.g. td, th, tr, etc.)
36
- def tag_name
37
- ''
38
- end
39
-
40
- # The content that will be displayed inside of the tag
41
- def content
42
- ''
43
- end
44
- end
45
43
  end
46
44
  end
@@ -1,103 +1,101 @@
1
1
  require 'table_helper/cell'
2
2
 
3
- module PluginAWeek #:nodoc:
4
- module TableHelper
5
- # Provides a blank class that can be used to build the cells for a row
6
- class RowBuilder < BlankSlate
7
- reveal :respond_to?
8
-
9
- attr_reader :row
10
-
11
- # Creates a builder for the given row
12
- def initialize(row)
13
- @row = row
14
- end
15
-
16
- # Proxies all missed methods to the row
17
- def method_missing(*args)
18
- row.send(*args)
19
- end
3
+ module TableHelper
4
+ # Provides a blank class that can be used to build the cells for a row
5
+ class RowBuilder < BlankSlate #:nodoc:
6
+ reveal :respond_to?
7
+
8
+ attr_reader :row
9
+
10
+ # Creates a builder for the given row
11
+ def initialize(row)
12
+ @row = row
13
+ end
14
+
15
+ # Proxies all missed methods to the row
16
+ def method_missing(*args)
17
+ row.send(*args)
18
+ end
19
+
20
+ # Defines the builder method for the given cell name. For example, if
21
+ # a cell with the name :title was defined, then the cell would be able
22
+ # to be read and written like so:
23
+ #
24
+ # row.title #=> Accesses the title
25
+ # row.title "Page Title" #=> Creates a new cell with "Page Title" as the content
26
+ def define_cell(name)
27
+ method_name = name.gsub('-', '_')
20
28
 
21
- # Defines the builder method for the given cell name. For example, if
22
- # a cell with the name :title was defined, then the cell would be able
23
- # to be read and written like so:
24
- #
25
- # row.title #=> Accesses the title
26
- # row.title "Page Title" #=> Creates a new cell with "Page Title" as the content
27
- def define_cell(name)
28
- method_name = name.gsub('-', '_')
29
-
30
- klass = class << self; self; end
31
- klass.class_eval do
32
- define_method(method_name) do |*args|
33
- if args.empty?
34
- row.cells[name]
35
- else
36
- row.cell(name, *args)
37
- end
29
+ klass = class << self; self; end
30
+ klass.class_eval do
31
+ define_method(method_name) do |*args|
32
+ if args.empty?
33
+ row.cells[name]
34
+ else
35
+ row.cell(name, *args)
38
36
  end
39
- end unless klass.method_defined?(method_name)
40
- end
41
-
42
- # Removes the definition for the given cell
43
- def undef_cell(name)
44
- method_name = name.gsub('-', '_')
45
-
46
- klass = class << self; self; end
47
- klass.class_eval do
48
- remove_method(method_name)
49
37
  end
50
- end
38
+ end unless klass.method_defined?(method_name)
51
39
  end
52
40
 
53
- # Represents a single row within a table. A row can consist of either
54
- # data cells or header cells.
55
- class Row < HtmlElement
56
- # The proxy class used externally to build the actual cells
57
- attr_reader :builder
58
-
59
- # The current cells in this row, in the order in which they will be built
60
- attr_reader :cells
41
+ # Removes the definition for the given cell
42
+ def undef_cell(name)
43
+ method_name = name.gsub('-', '_')
61
44
 
62
- def initialize #:nodoc:
63
- super
64
-
65
- @cells = ActiveSupport::OrderedHash.new
66
- @builder = RowBuilder.new(self)
45
+ klass = class << self; self; end
46
+ klass.class_eval do
47
+ remove_method(method_name)
67
48
  end
49
+ end
50
+ end
51
+
52
+ # Represents a single row within a table. A row can consist of either
53
+ # data cells or header cells.
54
+ class Row < HtmlElement
55
+ # The proxy class used externally to build the actual cells
56
+ attr_reader :builder
57
+
58
+ # The current cells in this row, in the order in which they will be built
59
+ attr_reader :cells
60
+
61
+ def initialize #:nodoc:
62
+ super
68
63
 
69
- # Creates a new cell with the given name and generates shortcut
70
- # accessors for the method.
71
- def cell(name, *args)
72
- name = name.to_s if name
73
-
74
- cell = Cell.new(name, *args)
75
- cells[name] = cell
76
- builder.define_cell(name) if name
77
-
78
- cell
79
- end
64
+ @cells = ActiveSupport::OrderedHash.new
65
+ @builder = RowBuilder.new(self)
66
+ end
67
+
68
+ # Creates a new cell with the given name and generates shortcut
69
+ # accessors for the method.
70
+ def cell(name, *args)
71
+ name = name.to_s if name
80
72
 
81
- # The names of all cells in this row
82
- def cell_names
83
- cells.keys
84
- end
73
+ cell = Cell.new(name, *args)
74
+ cells[name] = cell
75
+ builder.define_cell(name) if name
85
76
 
86
- # Clears all of the current cells from the row
87
- def clear
88
- # Remove all of the shortcut methods
89
- cell_names.each {|name| builder.undef_cell(name)}
90
- cells.clear
77
+ cell
78
+ end
79
+
80
+ # The names of all cells in this row
81
+ def cell_names
82
+ cells.keys
83
+ end
84
+
85
+ # Clears all of the current cells from the row
86
+ def clear
87
+ # Remove all of the shortcut methods
88
+ cell_names.each {|name| builder.undef_cell(name)}
89
+ cells.clear
90
+ end
91
+
92
+ private
93
+ def tag_name
94
+ 'tr'
91
95
  end
92
96
 
93
- private
94
- def tag_name
95
- 'tr'
96
- end
97
-
98
- def content
99
- cells.values.map(&:html).join
100
- end
101
- end
97
+ def content
98
+ cells.values.map(&:html).join
99
+ end
102
100
  end
103
101
  end
@@ -1,7 +1,7 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
- class TableHelperTest < Test::Unit::TestCase
4
- include PluginAWeek::TableHelper
3
+ class TableHelperTest < ActionView::TestCase
4
+ tests TableHelper
5
5
 
6
6
  def test_should_build_collection_table
7
7
  html = collection_table(['first', 'second', 'last']) do |header, body|
@@ -12,7 +12,7 @@ class TableHelperTest < Test::Unit::TestCase
12
12
  end
13
13
  end
14
14
 
15
- expected = <<-end_eval
15
+ expected = <<-end_str
16
16
  <table cellpadding="0" cellspacing="0">
17
17
  <thead>
18
18
  <tr>
@@ -31,7 +31,7 @@ class TableHelperTest < Test::Unit::TestCase
31
31
  </tr>
32
32
  </tbody>
33
33
  </table>
34
- end_eval
34
+ end_str
35
35
  assert_html_equal expected, html
36
36
  end
37
37
  end
@@ -1,12 +1,9 @@
1
- require 'test/unit'
1
+ # Load the plugin testing framework
2
+ $:.unshift("#{File.dirname(__FILE__)}/../../plugin_test_helper/lib")
2
3
  require 'rubygems'
3
- require 'action_controller'
4
- require 'action_view'
4
+ require 'plugin_test_helper'
5
5
 
6
- $:.unshift(File.dirname(__FILE__) + '/../lib')
7
- require File.dirname(__FILE__) + '/../init'
8
-
9
- class Test::Unit::TestCase #:nodoc:
6
+ Test::Unit::TestCase.class_eval do
10
7
  private
11
8
  def assert_html_equal(expected, actual)
12
9
  assert_equal expected.strip.gsub(/\n\s*/, ''), actual
@@ -1,9 +1,9 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class BodyRowByDefaultTest < Test::Unit::TestCase
4
4
  def setup
5
- header = PluginAWeek::TableHelper::Header.new([])
6
- @row = PluginAWeek::TableHelper::BodyRow.new(Object.new, header)
5
+ header = TableHelper::Header.new([])
6
+ @row = TableHelper::BodyRow.new(Object.new, header)
7
7
  end
8
8
 
9
9
  def test_should_not_alternate
@@ -23,9 +23,9 @@ class BodyRowTest < Test::Unit::TestCase
23
23
  end
24
24
 
25
25
  def setup
26
- header = PluginAWeek::TableHelper::Header.new([])
26
+ header = TableHelper::Header.new([])
27
27
  header.column :title
28
- @row = PluginAWeek::TableHelper::BodyRow.new(Post.new, header)
28
+ @row = TableHelper::BodyRow.new(Post.new, header)
29
29
  end
30
30
 
31
31
  def test_should_generate_cell_accessors
@@ -40,8 +40,8 @@ end
40
40
 
41
41
  class BodyRowWithNoColumnsTest < Test::Unit::TestCase
42
42
  def setup
43
- header = PluginAWeek::TableHelper::Header.new([])
44
- @row = PluginAWeek::TableHelper::BodyRow.new(Object.new, header)
43
+ header = TableHelper::Header.new([])
44
+ @row = TableHelper::BodyRow.new(Object.new, header)
45
45
  end
46
46
 
47
47
  def test_should_not_build_cells
@@ -57,10 +57,10 @@ class BodyRowWithCustomAttributeTest < Test::Unit::TestCase
57
57
  end
58
58
 
59
59
  def setup
60
- header = PluginAWeek::TableHelper::Header.new([])
60
+ header = TableHelper::Header.new([])
61
61
  header.column :title
62
62
  header.column :author_name
63
- @row = PluginAWeek::TableHelper::BodyRow.new(Post.new, header)
63
+ @row = TableHelper::BodyRow.new(Post.new, header)
64
64
  end
65
65
 
66
66
  def test_should_use_attribute_values_as_cell_content
@@ -71,10 +71,10 @@ end
71
71
 
72
72
  class BodyRowWithMissingCellsTest < Test::Unit::TestCase
73
73
  def setup
74
- header = PluginAWeek::TableHelper::Header.new([])
74
+ header = TableHelper::Header.new([])
75
75
  header.column :title
76
76
  header.column :author_name
77
- @row = PluginAWeek::TableHelper::BodyRow.new(Object.new, header)
77
+ @row = TableHelper::BodyRow.new(Object.new, header)
78
78
  end
79
79
 
80
80
  def test_should_build_missing_cells_if_cells_not_specified
@@ -94,8 +94,8 @@ end
94
94
 
95
95
  class BodyRowAlternatingTest < Test::Unit::TestCase
96
96
  def setup
97
- header = PluginAWeek::TableHelper::Header.new([])
98
- @row = PluginAWeek::TableHelper::BodyRow.new(Object.new, header)
97
+ header = TableHelper::Header.new([])
98
+ @row = TableHelper::BodyRow.new(Object.new, header)
99
99
  @row.alternate = true
100
100
  end
101
101
 
@@ -1,9 +1,9 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class BodyByDefaultTest < Test::Unit::TestCase
4
4
  def setup
5
- header = PluginAWeek::TableHelper::Header.new([])
6
- @body = PluginAWeek::TableHelper::Body.new([], header)
5
+ header = TableHelper::Header.new([])
6
+ @body = TableHelper::Body.new([], header)
7
7
  end
8
8
 
9
9
  def test_should_not_alternate_rows
@@ -30,8 +30,8 @@ class BodyTest < Test::Unit::TestCase
30
30
  Post.new('second'),
31
31
  Post.new('last')
32
32
  ]
33
- @header = PluginAWeek::TableHelper::Header.new(@collection)
34
- @body = PluginAWeek::TableHelper::Body.new(@collection, @header)
33
+ @header = TableHelper::Header.new(@collection)
34
+ @body = TableHelper::Body.new(@collection, @header)
35
35
  end
36
36
 
37
37
  def test_should_raise_exception_if_invalid_alternate_rows_specified
@@ -57,11 +57,11 @@ class BodyTest < Test::Unit::TestCase
57
57
  assert_equal @collection.index(post), index
58
58
  end
59
59
 
60
- expected = <<-end_eval
60
+ expected = <<-end_str
61
61
  <tr class="row">
62
62
  <td class="title">#{post.title}</td>
63
63
  </tr>
64
- end_eval
64
+ end_str
65
65
  assert_html_equal expected, html
66
66
  end
67
67
  end
@@ -74,11 +74,11 @@ class BodyTest < Test::Unit::TestCase
74
74
  assert_equal 1, index
75
75
  end
76
76
 
77
- expected = <<-end_eval
77
+ expected = <<-end_str
78
78
  <tr class="row">
79
79
  <td class="title">first</td>
80
80
  </tr>
81
- end_eval
81
+ end_str
82
82
  assert_html_equal expected, html
83
83
  end
84
84
 
@@ -86,19 +86,19 @@ class BodyTest < Test::Unit::TestCase
86
86
  @header.column :title
87
87
  @header.column :author_name
88
88
 
89
- expected = <<-end_eval
89
+ expected = <<-end_str
90
90
  <tr class="row">
91
91
  <td class="title">first</td>
92
92
  <td class="author_name empty"></td>
93
93
  </tr>
94
- end_eval
94
+ end_str
95
95
  assert_html_equal expected, @body.build_row(@collection.first)
96
96
  end
97
97
 
98
98
  def test_should_build_all_rows
99
99
  @header.column :title
100
100
 
101
- expected = <<-end_eval
101
+ expected = <<-end_str
102
102
  <tr class="row">
103
103
  <td class="title">first</td>
104
104
  </tr>
@@ -108,7 +108,7 @@ class BodyTest < Test::Unit::TestCase
108
108
  <tr class="row">
109
109
  <td class="title">last</td>
110
110
  </tr>
111
- end_eval
111
+ end_str
112
112
  assert_html_equal expected, @body.build
113
113
  end
114
114
 
@@ -116,7 +116,7 @@ class BodyTest < Test::Unit::TestCase
116
116
  @header.column :title
117
117
  @body.build
118
118
 
119
- expected = <<-end_eval
119
+ expected = <<-end_str
120
120
  <tbody>
121
121
  <tr class="row">
122
122
  <td class="title">first</td>
@@ -128,7 +128,7 @@ class BodyTest < Test::Unit::TestCase
128
128
  <td class="title">last</td>
129
129
  </tr>
130
130
  </tbody>
131
- end_eval
131
+ end_str
132
132
  assert_html_equal expected, @body.html
133
133
  end
134
134
 
@@ -137,7 +137,7 @@ class BodyTest < Test::Unit::TestCase
137
137
  @body[:class] = 'pretty'
138
138
  @body.build
139
139
 
140
- expected = <<-end_eval
140
+ expected = <<-end_str
141
141
  <tbody class="pretty">
142
142
  <tr class="row">
143
143
  <td class="title">first</td>
@@ -149,7 +149,7 @@ class BodyTest < Test::Unit::TestCase
149
149
  <td class="title">last</td>
150
150
  </tr>
151
151
  </tbody>
152
- end_eval
152
+ end_str
153
153
  assert_html_equal expected, @body.html
154
154
  end
155
155
  end
@@ -157,16 +157,16 @@ end
157
157
  class BodyWithEmptyCollectionTest < Test::Unit::TestCase
158
158
  def setup
159
159
  @collection = []
160
- @header = PluginAWeek::TableHelper::Header.new(@collection)
161
- @body = PluginAWeek::TableHelper::Body.new(@collection, @header)
160
+ @header = TableHelper::Header.new(@collection)
161
+ @body = TableHelper::Body.new(@collection, @header)
162
162
  end
163
163
 
164
164
  def test_should_show_no_content
165
- expected = <<-end_eval
165
+ expected = <<-end_str
166
166
  <tr class="no_content">
167
167
  <td>No matches found.</td>
168
168
  </tr>
169
- end_eval
169
+ end_str
170
170
  assert_html_equal expected, @body.build
171
171
  end
172
172
 
@@ -179,11 +179,11 @@ class BodyWithEmptyCollectionTest < Test::Unit::TestCase
179
179
  @header.column :title
180
180
  @header.column :author_name
181
181
 
182
- expected = <<-end_eval
182
+ expected = <<-end_str
183
183
  <tr class="no_content">
184
184
  <td colspan="2">No matches found.</td>
185
185
  </tr>
186
- end_eval
186
+ end_str
187
187
  assert_html_equal expected, @body.build
188
188
  end
189
189
  end
@@ -203,28 +203,28 @@ class BodyWithAlternatingEvenRowsTest < Test::Unit::TestCase
203
203
  Post.new('second'),
204
204
  Post.new('last')
205
205
  ]
206
- @header = PluginAWeek::TableHelper::Header.new(@collection)
206
+ @header = TableHelper::Header.new(@collection)
207
207
  @header.column :title
208
208
 
209
- @body = PluginAWeek::TableHelper::Body.new(@collection, @header)
209
+ @body = TableHelper::Body.new(@collection, @header)
210
210
  @body.alternate_rows = :even
211
211
  end
212
212
 
213
213
  def test_should_alternate_even_row
214
- expected = <<-end_eval
214
+ expected = <<-end_str
215
215
  <tr class="row alternate">
216
216
  <td class="title">first</td>
217
217
  </tr>
218
- end_eval
218
+ end_str
219
219
  assert_html_equal expected, @body.build_row(@collection.first)
220
220
  end
221
221
 
222
222
  def test_should_not_alternate_odd_row
223
- expected = <<-end_eval
223
+ expected = <<-end_str
224
224
  <tr class="row">
225
225
  <td class="title">second</td>
226
226
  </tr>
227
- end_eval
227
+ end_str
228
228
  assert_html_equal expected, @body.build_row(@collection[1])
229
229
  end
230
230
  end
@@ -244,28 +244,28 @@ class BodyWithAlternatingOddRowsTest < Test::Unit::TestCase
244
244
  Post.new('second'),
245
245
  Post.new('last')
246
246
  ]
247
- @header = PluginAWeek::TableHelper::Header.new(@collection)
247
+ @header = TableHelper::Header.new(@collection)
248
248
  @header.column :title
249
249
 
250
- @body = PluginAWeek::TableHelper::Body.new(@collection, @header)
250
+ @body = TableHelper::Body.new(@collection, @header)
251
251
  @body.alternate_rows = :odd
252
252
  end
253
253
 
254
254
  def test_should_alternate_odd_row
255
- expected = <<-end_eval
255
+ expected = <<-end_str
256
256
  <tr class="row alternate">
257
257
  <td class="title">second</td>
258
258
  </tr>
259
- end_eval
259
+ end_str
260
260
  assert_html_equal expected, @body.build_row(@collection[1])
261
261
  end
262
262
 
263
263
  def test_should_not_alternate_even_row
264
- expected = <<-end_eval
264
+ expected = <<-end_str
265
265
  <tr class="row">
266
266
  <td class="title">first</td>
267
267
  </tr>
268
- end_eval
268
+ end_str
269
269
  assert_html_equal expected, @body.build_row(@collection.first)
270
270
  end
271
271
  end