tabula_rasa 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85b6138d31f80d0ef643af81a02b77bcea0fc4b2
4
- data.tar.gz: 787437faf4bd0631fd23cc2ec3e64e8e48c2079a
3
+ metadata.gz: e1d7fbe18b2b3b9d85b7a5cd7677e246ad3333fd
4
+ data.tar.gz: 5902b713adcff3c7b639eaf75fc95a3e5ba757e6
5
5
  SHA512:
6
- metadata.gz: 0647a914557c4b474c43013f1693e3454826feed995410445d39165f3dc270f07aba2805b9addf321aacfe634f57bc4a81dce0ac421d38348b6352ed12940c20
7
- data.tar.gz: 86b608afad417372b5eb3610823e6625e7cc8c6c42ae2a087e02ce6caa1ac72df3c4bd88397f4baf3b56c7130cc2251390337bf26f0461e2b8815aac9bc84bc3
6
+ metadata.gz: f1b741f7851cf06aac649649c46c39f8b91ba3ff4cc5ec73978633214def1848af0d7914785403a492805ccbec114396c8ba40f10c3974916a96856e5edd70b7
7
+ data.tar.gz: 41c3aaf92b875314a8069e3fdf5a03d4a9bba30e5753e4fff58bbb73bb5c5b7814a78794c2a4be69a77b44a7093039556d7505269e4ad33771fa1c389568b643
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  An opinionated yet simple table generator for Rails<sup>1</sup>.
5
5
 
6
- TabulaRasa is designed to leverage ActiveRecord::Relation instances and conventions<sup>2</sup> to make table generation a snap but still readable and editable.
6
+ TabulaRasa is designed to leverage ActiveRecord::Relation instances and conventions to make table generation a snap but still readable and editable.
7
7
 
8
8
  ## Installation
9
9
 
@@ -21,7 +21,98 @@ Or install it yourself as:
21
21
 
22
22
  ## Usage
23
23
 
24
- TODO: Write usage instructions here
24
+ TabulaRasa is designed to make it easy to make valid HTML[5] tables for ActiveRecord::Relations. Here are examples on how to set attributes:
25
+
26
+ ```ruby
27
+ # TABLE
28
+ # NOTE: special options here:
29
+ # * zebra: Accepts boolean or array of string values for ActionView's cycle helper. Defaults to true, creating zebra-striped tables (using 'even' and 'odd' values) out of the box.
30
+ # * dom_id: Accepts boolean, for setting TBODY TR id attribute to dom_id for the row's instance. Defaults to false.
31
+ # * dom_class: Accepts boolean, for adding dom_class for the row's instance to the TBODY TR class attribute. Defaults to false.
32
+ tabula_rasa @collection id: 'foo' do |table|
33
+ t.column :foo
34
+ end
35
+
36
+ # THEAD
37
+ tabula_rasa @collection, head: {id: 'foo'} do |table|
38
+ t.column :foo
39
+ end
40
+
41
+ # THEAD TR not supported. There's only one row. Use the THEAD TR selector instead.
42
+
43
+ # TH
44
+ tabula_rasa @collection do |table|
45
+ t.column :foo, head: {id: 'foo'}
46
+ # You can also set both TH and TD attributes with the same values
47
+ t.column :bar, shared: {class: 'bar'}
48
+ # NOTE: block column arguments are not supported for TH element
49
+ end
50
+
51
+ # TBODY
52
+ tabula_rasa @collection, body: {id: 'foo'} do |table|
53
+ t.column :foo
54
+ end
55
+
56
+ # TBODY TR
57
+ tabula_rasa @collection do |table|
58
+ t.column :foo # Just here for validity
59
+ # This is the code that actually does the work for the TR
60
+ t.row class: 'foobar'
61
+ # OR you can use a block argument. Do note though...
62
+ # Defining the row twice in real code will raise ArgumentError.
63
+ t.row do |row|
64
+ row.id do |instance|
65
+ dom_id instance
66
+ end
67
+ # Sets attribute 'data-foo'
68
+ row.data :foo do |instance|
69
+ "foo-#{instance.id}"
70
+ end
71
+ end
72
+ end
73
+
74
+ # TD
75
+ tabula_rasa @collection do |table|
76
+ t.column :foo, body: {class: 'foo'}
77
+ t.column :bar do |column|
78
+ column.class do |instance|
79
+ instance.bar? ? 'bar' : 'unbar'
80
+ end
81
+ # Just an example of another attribute where setting by string might make sense. Use CSS for this really!
82
+ column.attribute :width, '100'
83
+ end
84
+ # You can also set both TH and TD attributes with the same values
85
+ t.column :baz, shared: {class: 'baz'}
86
+ end
87
+ ```
88
+
89
+ Cell values default to the attribute name [for the TH element] and value [for the TD element]. You can easily override this by using the :value key for the :head or :body options for the column method as seen below:
90
+
91
+ ```ruby
92
+ tabula_rasa @collection do |table|
93
+ t.column :foo, head: {value: 'Foo Header'}, body: {value: 'Foo Body Content'}
94
+ end
95
+ ```
96
+
97
+ Most use cases for overriding the body value though would be better served by using a block which provides access to the collection member for that row.
98
+
99
+ ```ruby
100
+ tabula_rasa @collection do |table|
101
+ t.column :foo do |column|
102
+ column.value do |instance|
103
+ "A massaged value for instance #{instance.id}"
104
+ end
105
+ end
106
+ end
107
+ ```
108
+
109
+ A lot of the time though you should just be able to get by with a simple symbol argument, since you aren't limited to simple attributes but can use methods that massage attribute data.
110
+
111
+ ```ruby
112
+ tabula_rasa @collection do |table|
113
+ t.column :foo
114
+ end
115
+ ```
25
116
 
26
117
  ## Contributing
27
118
 
@@ -33,12 +124,8 @@ TODO: Write usage instructions here
33
124
 
34
125
  ## TODO
35
126
 
36
- * Write usage instructions
37
- * Support for 'zebra-striping'
38
- * TD dom_id and dom_class
127
+ * Discover more things to do
39
128
 
40
129
  ## Footnotes
41
130
 
42
131
  <sup>1</sup> TabulaRasa really only relies on ActiveRecord and ActionView/ActionPack so you could use in a non-Rails stack that had those two elements but it seemed silly to differentiate this in the summary sentence.
43
-
44
- <sup>2</sup> These conventions may only be my own. Time will tell.
@@ -13,18 +13,24 @@ module TabulaRasa
13
13
  @options = options
14
14
  @view = view
15
15
  @klass = collection.klass
16
- @columns = []
16
+ @column_collection = []
17
17
  yield self if block_given?
18
18
  ensure_row
19
19
  end
20
20
 
21
21
  def render
22
- return if columns.empty?
22
+ return if column_collection.empty?
23
23
  content_tag :table, content, table_options
24
24
  end
25
25
 
26
26
  def column(*args, &block)
27
- @columns << Column.new(self, *args, &block)
27
+ @column_collection << Column.new(self, *args, &block)
28
+ end
29
+
30
+ def columns(*args)
31
+ args.each do |arg|
32
+ @column_collection << Column.new(self, arg)
33
+ end
28
34
  end
29
35
 
30
36
  def row(options = {}, &block)
@@ -34,7 +40,7 @@ module TabulaRasa
34
40
 
35
41
  private
36
42
 
37
- attr_reader :view, :klass, :columns
43
+ attr_reader :view, :klass, :column_collection
38
44
 
39
45
  def content
40
46
  safe_join [thead, tbody]
@@ -43,7 +49,7 @@ module TabulaRasa
43
49
  def thead
44
50
  content_tag :thead, options[:head] do
45
51
  content_tag :tr do
46
- safe_join columns.map(&:head_content)
52
+ safe_join column_collection.map(&:head_content)
47
53
  end
48
54
  end
49
55
  end
@@ -57,7 +63,7 @@ module TabulaRasa
57
63
  def collection_body
58
64
  rows = collection.map do |member|
59
65
  content_tag :tr, @row.options_for(member) do
60
- cells = columns.map do |column|
66
+ cells = column_collection.map do |column|
61
67
  column.body_content_for member
62
68
  end
63
69
  safe_join cells
@@ -68,7 +74,7 @@ module TabulaRasa
68
74
 
69
75
  def empty_body
70
76
  content_tag :tr, class: 'empty' do
71
- content_tag :td, colspan: columns.size do
77
+ content_tag :td, colspan: column_collection.size do
72
78
  "No #{klass.table_name.downcase} present"
73
79
  end
74
80
  end
@@ -1,3 +1,3 @@
1
1
  module TabulaRasa
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -335,5 +335,22 @@ describe TabulaRasa::Helpers, 'Head Specs' do
335
335
  cell.children.first.must_be :text?
336
336
  cell.text.must_equal 'No survivors present'
337
337
  end
338
+
339
+ it 'can use multiple single arguments via internal columns method to set multiple columns' do
340
+ captured = capture do
341
+ tabula_rasa @survivors do |t|
342
+ t.columns :first_name, :last_name
343
+ end
344
+ end
345
+ rows = extract_all('table tbody tr', captured)
346
+
347
+ rows.each_with_index do |row, n|
348
+ survivor = @survivors[n]
349
+ cells = row.search('td')
350
+ cells.size.must_equal 2
351
+ cells[0].text.must_equal survivor.first_name
352
+ cells[1].text.must_equal survivor.last_name
353
+ end
354
+ end
338
355
  end
339
356
  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.3
4
+ version: 0.0.4
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-01 00:00:00.000000000 Z
11
+ date: 2013-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler