tabula_rasa 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tabula_rasa/base.rb +24 -3
- data/lib/tabula_rasa/column.rb +4 -4
- data/lib/tabula_rasa/row.rb +78 -0
- data/lib/tabula_rasa/version.rb +1 -1
- data/spec/config/spec.rb +1 -0
- data/spec/tabula_rasa/helper_body_spec.rb +172 -6
- data/spec/tabula_rasa/helper_head_spec.rb +3 -2
- data/spec/tabula_rasa/helper_integrated_spec.rb +2 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85b6138d31f80d0ef643af81a02b77bcea0fc4b2
|
4
|
+
data.tar.gz: 787437faf4bd0631fd23cc2ec3e64e8e48c2079a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0647a914557c4b474c43013f1693e3454826feed995410445d39165f3dc270f07aba2805b9addf321aacfe634f57bc4a81dce0ac421d38348b6352ed12940c20
|
7
|
+
data.tar.gz: 86b608afad417372b5eb3610823e6625e7cc8c6c42ae2a087e02ce6caa1ac72df3c4bd88397f4baf3b56c7130cc2251390337bf26f0461e2b8815aac9bc84bc3
|
data/lib/tabula_rasa/base.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
+
require 'tabula_rasa/row'
|
1
2
|
require 'tabula_rasa/column'
|
2
3
|
|
3
4
|
module TabulaRasa
|
4
5
|
class Base
|
5
|
-
attr_reader :collection, :
|
6
|
+
attr_reader :collection, :options
|
6
7
|
|
7
|
-
delegate :
|
8
|
+
delegate :content_tag, :cycle, :dom_id, :dom_class, :safe_join, to: :view
|
8
9
|
|
9
10
|
def initialize(collection, view, options = {}, &block)
|
10
11
|
raise ArgumentError, 'TabulaRasa only works on ActiveRecord Relation instances' unless collection.is_a?(ActiveRecord::Relation)
|
@@ -14,6 +15,7 @@ module TabulaRasa
|
|
14
15
|
@klass = collection.klass
|
15
16
|
@columns = []
|
16
17
|
yield self if block_given?
|
18
|
+
ensure_row
|
17
19
|
end
|
18
20
|
|
19
21
|
def render
|
@@ -25,8 +27,15 @@ module TabulaRasa
|
|
25
27
|
@columns << Column.new(self, *args, &block)
|
26
28
|
end
|
27
29
|
|
30
|
+
def row(options = {}, &block)
|
31
|
+
raise ArgumentError, 'Row definition cannot be called more than once' if @row.present?
|
32
|
+
@row = Row.new(self, options, &block)
|
33
|
+
end
|
34
|
+
|
28
35
|
private
|
29
36
|
|
37
|
+
attr_reader :view, :klass, :columns
|
38
|
+
|
30
39
|
def content
|
31
40
|
safe_join [thead, tbody]
|
32
41
|
end
|
@@ -47,7 +56,7 @@ module TabulaRasa
|
|
47
56
|
|
48
57
|
def collection_body
|
49
58
|
rows = collection.map do |member|
|
50
|
-
content_tag :tr do
|
59
|
+
content_tag :tr, @row.options_for(member) do
|
51
60
|
cells = columns.map do |column|
|
52
61
|
column.body_content_for member
|
53
62
|
end
|
@@ -57,8 +66,20 @@ module TabulaRasa
|
|
57
66
|
safe_join rows
|
58
67
|
end
|
59
68
|
|
69
|
+
def empty_body
|
70
|
+
content_tag :tr, class: 'empty' do
|
71
|
+
content_tag :td, colspan: columns.size do
|
72
|
+
"No #{klass.table_name.downcase} present"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
60
77
|
def table_options
|
61
78
|
options.except :head, :body
|
62
79
|
end
|
80
|
+
|
81
|
+
def ensure_row
|
82
|
+
@row ||= Row.new(self)
|
83
|
+
end
|
63
84
|
end
|
64
85
|
end
|
data/lib/tabula_rasa/column.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module TabulaRasa
|
2
2
|
class Column
|
3
|
-
delegate :
|
3
|
+
delegate :content_tag, to: :base
|
4
4
|
|
5
5
|
def initialize(base, *args, &block)
|
6
6
|
@base = base
|
@@ -23,7 +23,7 @@ module TabulaRasa
|
|
23
23
|
content_tag :td, body_value_for(instance), options[:body]
|
24
24
|
end
|
25
25
|
|
26
|
-
def
|
26
|
+
def value(&block)
|
27
27
|
@body_value_option = block if block_given?
|
28
28
|
end
|
29
29
|
|
@@ -61,9 +61,9 @@ module TabulaRasa
|
|
61
61
|
def body_value_for(instance)
|
62
62
|
value = body_value_option || instance.send(attribute)
|
63
63
|
if value.respond_to?(:call)
|
64
|
-
value.call
|
64
|
+
value.call instance
|
65
65
|
else
|
66
|
-
|
66
|
+
value.to_s
|
67
67
|
end
|
68
68
|
end
|
69
69
|
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module TabulaRasa
|
2
|
+
class Row
|
3
|
+
# NOTE: Need to handle ALL valid HTML5 attributes
|
4
|
+
ATTRIBUTES = [:id, :class]
|
5
|
+
|
6
|
+
delegate :cycle, :dom_id, :dom_class, to: :base
|
7
|
+
|
8
|
+
def initialize(base, options = {}, &block)
|
9
|
+
@base = base
|
10
|
+
@options = options
|
11
|
+
yield self if block_given?
|
12
|
+
end
|
13
|
+
|
14
|
+
def options_for(instance)
|
15
|
+
{
|
16
|
+
id: [id_value_for(instance), dom_id_for(instance)].compact.join(' ').squish,
|
17
|
+
class: [class_value_for(instance), zebrastripe, dom_class_for(instance)].compact.join(' ').squish
|
18
|
+
}.merge data_values_for(instance)
|
19
|
+
end
|
20
|
+
|
21
|
+
def data(key, &block)
|
22
|
+
options[:data] ||= {}
|
23
|
+
options[:data][key] = block
|
24
|
+
end
|
25
|
+
|
26
|
+
ATTRIBUTES.each do |attribute|
|
27
|
+
define_method attribute do |&block|
|
28
|
+
options[attribute] = block
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
attr_reader :base, :options
|
35
|
+
|
36
|
+
def zebrastripe
|
37
|
+
case base.options[:zebra]
|
38
|
+
when false
|
39
|
+
# Nothing
|
40
|
+
when nil
|
41
|
+
cycle *%w{even odd}
|
42
|
+
when Array
|
43
|
+
cycle *base.options[:zebra]
|
44
|
+
else
|
45
|
+
raise ArgumentError, "Invalid value for options[:zebra]: #{base.options[:zebra]}. Should be array."
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def dom_id_for(instance)
|
50
|
+
return unless base.options[:dom_id] == true
|
51
|
+
dom_id(instance) unless id_value_for(instance).present?
|
52
|
+
end
|
53
|
+
|
54
|
+
def dom_class_for(instance)
|
55
|
+
dom_class(instance) if base.options[:dom_class] == true
|
56
|
+
end
|
57
|
+
|
58
|
+
def data_values_for(instance)
|
59
|
+
return {} if options[:data].blank?
|
60
|
+
options[:data].inject({}) do |m, x|
|
61
|
+
key, value = x
|
62
|
+
m["data-#{key}"] = value.call(instance)
|
63
|
+
m
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
ATTRIBUTES.each do |attribute|
|
68
|
+
define_method "#{attribute}_value_for" do |instance|
|
69
|
+
value = options[attribute]
|
70
|
+
if value.respond_to?(:call)
|
71
|
+
value.call instance
|
72
|
+
else
|
73
|
+
value.to_s
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/lib/tabula_rasa/version.rb
CHANGED
data/spec/config/spec.rb
CHANGED
@@ -3,6 +3,7 @@ class TabulaRasa::Spec < MiniTest::Spec
|
|
3
3
|
include ActionView::Helpers::TagHelper
|
4
4
|
include ActionView::Helpers::TextHelper
|
5
5
|
include ActionView::Helpers::OutputSafetyHelper
|
6
|
+
include ActionView::RecordIdentifier
|
6
7
|
include TabulaRasa::Helpers
|
7
8
|
|
8
9
|
attr_accessor :output_buffer
|
@@ -35,8 +35,156 @@ describe TabulaRasa::Helpers, 'Head Specs' do
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
|
39
|
-
|
38
|
+
describe 'TR attributes' do
|
39
|
+
it 'zebrastripes by default' do
|
40
|
+
captured = capture do
|
41
|
+
tabula_rasa @survivors do |t|
|
42
|
+
t.column :first_name
|
43
|
+
end
|
44
|
+
end
|
45
|
+
rows = extract_all('table tbody tr', captured)
|
46
|
+
|
47
|
+
rows[0].attribute('class').value.must_equal 'even'
|
48
|
+
rows[1].attribute('class').value.must_equal 'odd'
|
49
|
+
rows[2].attribute('class').value.must_equal 'even'
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'can disable zebrastripes' do
|
53
|
+
captured = capture do
|
54
|
+
tabula_rasa @survivors, zebra: false do |t|
|
55
|
+
t.column :first_name
|
56
|
+
end
|
57
|
+
end
|
58
|
+
rows = extract_all('table tbody tr', captured)
|
59
|
+
|
60
|
+
rows.each do |row|
|
61
|
+
row.attribute('class').value.must_be_empty
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'can customize zebrastripes' do
|
66
|
+
captured = capture do
|
67
|
+
tabula_rasa @survivors, zebra: %w{foo bar} do |t|
|
68
|
+
t.column :first_name
|
69
|
+
end
|
70
|
+
end
|
71
|
+
rows = extract_all('table tbody tr', captured)
|
72
|
+
|
73
|
+
rows[0].attribute('class').value.must_match /foo/
|
74
|
+
rows[1].attribute('class').value.must_match /bar/
|
75
|
+
rows[2].attribute('class').value.must_match /foo/
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'raises ArgumentError when invalid zebra option given' do
|
79
|
+
proc {
|
80
|
+
tabula_rasa @survivors, zebra: 'string' do |t|
|
81
|
+
t.column :first_name
|
82
|
+
end
|
83
|
+
}.must_raise ArgumentError
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'can set dom_id via option on the main method' do
|
87
|
+
captured = capture do
|
88
|
+
tabula_rasa @survivors, dom_id: true do |t|
|
89
|
+
t.column :first_name
|
90
|
+
end
|
91
|
+
end
|
92
|
+
rows = extract_all('table tbody tr', captured)
|
93
|
+
|
94
|
+
rows.each_with_index do |row, n|
|
95
|
+
row.attribute('id').value.must_equal "survivor_#{@survivors[n].id}"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'overrides dom_id option when id is explicitly set on row' do
|
100
|
+
captured = capture do
|
101
|
+
tabula_rasa @survivors, dom_id: true do |t|
|
102
|
+
t.column :first_name
|
103
|
+
t.row do |r|
|
104
|
+
r.id do |instance|
|
105
|
+
"manual_#{instance.id}"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
rows = extract_all('table tbody tr', captured)
|
111
|
+
|
112
|
+
rows.each_with_index do |row, n|
|
113
|
+
row.attribute('id').value.must_equal "manual_#{@survivors[n].id}"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'can set dom_class via option on the main method' do
|
118
|
+
captured = capture do
|
119
|
+
tabula_rasa @survivors, dom_class: true do |t|
|
120
|
+
t.column :first_name
|
121
|
+
end
|
122
|
+
end
|
123
|
+
rows = extract_all('table tbody tr', captured)
|
124
|
+
|
125
|
+
rows.each_with_index do |row, n|
|
126
|
+
row.attribute('class').value.must_match dom_class(@survivors[n])
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'can set attributes via options on internal row method' do
|
131
|
+
captured = capture do
|
132
|
+
tabula_rasa @survivors do |t|
|
133
|
+
t.column :first_name
|
134
|
+
t.row class: 'useless_row_class_for_testing'
|
135
|
+
end
|
136
|
+
end
|
137
|
+
rows = extract_all('table tbody tr', captured)
|
138
|
+
rows.each do |row|
|
139
|
+
row.attribute('class').value.must_match /useless_row_class_for_testing/
|
140
|
+
end
|
141
|
+
|
142
|
+
# Zebrastripes should still work as well
|
143
|
+
rows[0].attribute('class').value.must_match /even/
|
144
|
+
rows[1].attribute('class').value.must_match /odd/
|
145
|
+
rows[2].attribute('class').value.must_match /even/
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'can set attributes via block on internal row method' do
|
149
|
+
captured = capture do
|
150
|
+
tabula_rasa @survivors do |t|
|
151
|
+
t.column :first_name
|
152
|
+
t.row do |r|
|
153
|
+
r.id do |instance|
|
154
|
+
"survivor_id_#{instance.id}"
|
155
|
+
end
|
156
|
+
r.class do |instance|
|
157
|
+
"survivor_class_#{instance.id}"
|
158
|
+
end
|
159
|
+
r.data :foo do |instance|
|
160
|
+
"foo_#{instance.id}"
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
rows = extract_all('table tbody tr', captured)
|
166
|
+
|
167
|
+
rows.each_with_index do |row, n|
|
168
|
+
row.attribute('id').value.must_match "survivor_id_#{@survivors[n].id}"
|
169
|
+
row.attribute('class').value.must_match "survivor_class_#{@survivors[n].id}"
|
170
|
+
row.attribute('data-foo').value.must_match "foo_#{@survivors[n].id}"
|
171
|
+
end
|
172
|
+
|
173
|
+
# Zebrastripes should still work as well
|
174
|
+
rows[0].attribute('class').value.must_match /even/
|
175
|
+
rows[1].attribute('class').value.must_match /odd/
|
176
|
+
rows[2].attribute('class').value.must_match /even/
|
177
|
+
end
|
178
|
+
|
179
|
+
it 'raises ArgumentError if row is defined more than once' do
|
180
|
+
proc {
|
181
|
+
tabula_rasa @survivors do |t|
|
182
|
+
t.row class: 'foo'
|
183
|
+
t.row class: 'bar'
|
184
|
+
end
|
185
|
+
}.must_raise(ArgumentError)
|
186
|
+
end
|
187
|
+
end
|
40
188
|
|
41
189
|
describe 'TD attributes' do
|
42
190
|
it 'sets no attributes by default' do
|
@@ -124,11 +272,11 @@ describe TabulaRasa::Helpers, 'Head Specs' do
|
|
124
272
|
body.attributes.must_be_empty
|
125
273
|
end
|
126
274
|
|
127
|
-
it 'can override first argument via a
|
275
|
+
it 'can override first argument via a block for internal column method' do
|
128
276
|
captured = capture do
|
129
277
|
tabula_rasa @survivors do |t|
|
130
278
|
t.column :whole_name do |c|
|
131
|
-
c.
|
279
|
+
c.value do |instance|
|
132
280
|
"#{instance.first_name} #{instance.last_name}"
|
133
281
|
end
|
134
282
|
end
|
@@ -142,11 +290,11 @@ describe TabulaRasa::Helpers, 'Head Specs' do
|
|
142
290
|
end
|
143
291
|
end
|
144
292
|
|
145
|
-
it 'can override first argument via a
|
293
|
+
it 'can override first argument via a block for internal column method while setting attributes via options[:body]' do
|
146
294
|
captured = capture do
|
147
295
|
tabula_rasa @survivors do |t|
|
148
296
|
t.column :whole_name, body: {id: 'id_value', class: 'class_value', arbitrary: 'arbitrary_value', value: 'Overridden Value'} do |c|
|
149
|
-
c.
|
297
|
+
c.value do |instance|
|
150
298
|
"#{instance.first_name} #{instance.last_name}"
|
151
299
|
end
|
152
300
|
end
|
@@ -169,5 +317,23 @@ describe TabulaRasa::Helpers, 'Head Specs' do
|
|
169
317
|
table.attributes.must_be_empty
|
170
318
|
body.attributes.must_be_empty
|
171
319
|
end
|
320
|
+
|
321
|
+
it 'indicates the table is empty when collection is empty' do
|
322
|
+
@survivors = Survivor.none
|
323
|
+
captured = capture do
|
324
|
+
tabula_rasa @survivors do |t|
|
325
|
+
t.column :first_name
|
326
|
+
end
|
327
|
+
end
|
328
|
+
table = extract_first('table', captured)
|
329
|
+
body = extract_first('table tbody', captured)
|
330
|
+
cells = extract_all('table tbody td', captured)
|
331
|
+
|
332
|
+
cells.size.must_equal 1
|
333
|
+
cell = cells.first
|
334
|
+
cell.children.size.must_equal 1
|
335
|
+
cell.children.first.must_be :text?
|
336
|
+
cell.text.must_equal 'No survivors present'
|
337
|
+
end
|
172
338
|
end
|
173
339
|
end
|
@@ -35,8 +35,9 @@ describe TabulaRasa::Helpers, 'Head Specs' do
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
# NOTE: For now, I don't see the point of adding attributes on TR element.
|
38
|
+
# NOTE: For now, I don't see the point of adding attributes on TR element on THEAD.
|
39
39
|
# I've had varying success styling on that and tend to style on the TH elements instead.
|
40
|
+
# Also, unlike TBODY, there's only one row so... it's THEAD TR. Done!
|
40
41
|
|
41
42
|
describe 'TH attributes' do
|
42
43
|
it 'sets no attributes by default' do
|
@@ -115,7 +116,7 @@ describe TabulaRasa::Helpers, 'Head Specs' do
|
|
115
116
|
head.attributes.must_be_empty
|
116
117
|
end
|
117
118
|
|
118
|
-
it 'raises ArgumentError if attempting to override content via a
|
119
|
+
it 'raises ArgumentError if attempting to override content via a block [as body can]' do
|
119
120
|
proc {
|
120
121
|
tabula_rasa @survivors do |t|
|
121
122
|
t.column :first_name do |c|
|
@@ -26,11 +26,11 @@ describe TabulaRasa::Helpers, 'Integrated [Head and Body] Specs' do
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
it 'can override body values via
|
29
|
+
it 'can override body values via a block while using attribute argument for head value' do
|
30
30
|
captured = capture do
|
31
31
|
tabula_rasa @survivors do |t|
|
32
32
|
t.column :whole_name do |c|
|
33
|
-
c.
|
33
|
+
c.value do |instance|
|
34
34
|
"#{instance.first_name} #{instance.last_name}"
|
35
35
|
end
|
36
36
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tabula_rasa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- RSL
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -125,6 +125,7 @@ files:
|
|
125
125
|
- lib/tabula_rasa/base.rb
|
126
126
|
- lib/tabula_rasa/column.rb
|
127
127
|
- lib/tabula_rasa/helpers.rb
|
128
|
+
- lib/tabula_rasa/row.rb
|
128
129
|
- lib/tabula_rasa/version.rb
|
129
130
|
- spec/config/active_record.rb
|
130
131
|
- spec/config/spec.rb
|