tablematic 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c7e31de7aad761a0668517eb443ffd019b4251e
4
- data.tar.gz: e0fc8ad8f5c9a0bf563f291bfca9b0a0c9866c11
3
+ metadata.gz: ff87b7cf2c25b91af52a594265c19461c8338a6f
4
+ data.tar.gz: e31f2f45bd8a39448b0f1e6122b80d60d3a4af0f
5
5
  SHA512:
6
- metadata.gz: bd67f2907fc0d539fdfd52a666a921497f6b6b20b64cc01ff9f504c622c016a393ff6b9d134a2f1f6f3de5fe4093c75bd8b35aad5e9abc1efc1fe9388a3e4a47
7
- data.tar.gz: c9e993d9ade0999ea19be71cbd790f6a64411f1134723a2e2ecc09f6ac2c35d7a1f5f239235066bec4b89f3eb7291a59596a40fca7078fe02d2c207ae6e3a5d1
6
+ metadata.gz: e37ca023feeeb9f78f3e6427bf2fbd4ee1aad20c53336437f08525e29572296807b027fb05e69d28ee32a9d218841048b20e7fcc41fae40de9ed712d90264937
7
+ data.tar.gz: 31381754bc0407801b5ded6f17302b2185e409edbb27ebad509ab6fff33875a2ec40dc93659d2b03fbcaa6260a468acd5c1a099ae5b40ca6a0d982282700c63b
@@ -1,5 +1,8 @@
1
1
  module TableHelper
2
2
  class TableBuilder
3
+ extend Forwardable
4
+ def_delegators :@view_context, :concat, :content_tag, :capture
5
+
3
6
  def initialize(records, attr, opts={})
4
7
  @records = records
5
8
  @attr = attr
@@ -10,14 +13,11 @@ module TableHelper
10
13
  end
11
14
 
12
15
  def build(view_context, &block)
13
- result = "<table class='#{table_classes}'>"
14
- result += emit_html_heading
15
- result += emit_html_rows(view_context, &block)
16
- result += "</table>"
17
- result.html_safe
18
- end
19
-
20
- def output_buffer=(other)
16
+ @view_context = view_context
17
+ content_tag(:table, class: table_classes) do
18
+ emit_html_heading +
19
+ emit_html_rows(&block)
20
+ end.html_safe
21
21
  end
22
22
 
23
23
  protected
@@ -28,28 +28,31 @@ module TableHelper
28
28
  end
29
29
 
30
30
  def emit_html_heading
31
- result = "<thead><tr>"
32
- @attr.each do |attribute|
33
- result += "<th class='#{attribute_css_class(attribute)} #{attribute_type_css_class(attribute)}'>#{column_heading_for(attribute)}</th>"
31
+ content_tag(:thead) do
32
+ content_tag(:tr) do
33
+ @attr.each do |attribute|
34
+ css_classes = "#{attribute_css_class(attribute)} #{attribute_type_css_class(attribute)}"
35
+ concat(content_tag(:th, column_heading_for(attribute), class: css_classes))
36
+ end
37
+ end
34
38
  end
35
- result += "</tr></thead>"
36
39
  end
37
40
 
38
- def emit_html_rows(view_context, &block)
39
- result = "<tbody>"
40
- @records.each_with_index do |record,i|
41
- odd_even = i % 2 == 0 ? "#{@namespace}even" : "#{@namespace}odd"
42
- result += "<tr class='#{odd_even}'>"
43
- append_to_rows = block_given? ? view_context.capture(record, &block) : ''
44
- @attr.each do |attribute|
45
- message = attribute.is_a?(Hash) ? attribute.keys.first.to_sym : attribute.to_sym
46
- value = record.send(message).to_s
47
- result += "<td class='#{attribute_css_class(attribute)} #{attribute_type_css_class(attribute)}'>#{value}</td>"
41
+ def emit_html_rows(&block)
42
+ content_tag(:tbody) do
43
+ @records.each_with_index do |record,i|
44
+ columns = ''
45
+ @attr.each do |attribute|
46
+ message = attribute.is_a?(Hash) ? attribute.keys.first.to_sym : attribute.to_sym
47
+ value = record.send(message).to_s
48
+ columns << content_tag(:td, value, class: "#{attribute_css_class(attribute)} #{attribute_type_css_class(attribute)}")
49
+ end
50
+ columns += capture(record, &block) if block_given?
51
+
52
+ odd_even = i % 2 == 0 ? "#{@namespace}even" : "#{@namespace}odd"
53
+ concat(content_tag(:tr, columns.html_safe, class: odd_even))
48
54
  end
49
- result += append_to_rows
50
- result += "</tr>"
51
55
  end
52
- result += "</tbody>"
53
56
  end
54
57
 
55
58
  def attribute_css_class(attribute_name)
@@ -1,3 +1,3 @@
1
1
  module Tablematic
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tablematic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Desjardins
@@ -88,7 +88,6 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - MIT-LICENSE
91
- - README.rdoc
92
91
  - Rakefile
93
92
  - lib/tablematic.rb
94
93
  - lib/tablematic/builder.rb
data/README.rdoc DELETED
@@ -1,193 +0,0 @@
1
- # Tablematic
2
-
3
- ## Introduction
4
- There are a bunch of other Rails table generators out there. This is mine.
5
-
6
- Tabletastic is a gem to help quickly emit tabular data in a Rails web
7
- view. I created it because 1) I found myself doing this all the time, and
8
- it seemed like I could make it generic and package it as a gem, and 2)
9
- companies always want to see open source code samples when you apply
10
- for jobs, so it made sense to create some!
11
-
12
- ## Installation
13
- (Eventually, when I put this on rubygems) in your Rails project, Gemfile:
14
- gem "tablematic"
15
-
16
- ## Usage
17
- The simplest use of tablematic is to invoke the helper method with a
18
- variable that contains a collection of ActiveRecord objects, thusly:
19
-
20
- ```erb
21
- <%= table_for(@posts) %>
22
- ```
23
-
24
- Let's say you have a table named posts, and it contains the following
25
- data:
26
-
27
-
28
- |id|created_at|updated_at|added_by |title |content|
29
- |--|----------|----------|-------------|---------------|-------|
30
- | 1|2015-08-06|2015-08-06|Lisa Marie |Elvis had a Twin|On January 8, 1935, Elvis Aron (later spelled Aaron) Presley was born at his parents' two-room house in East Tupelo, Mississippi, about 35 minutes after his identical twin brother, Jesse Garon, who was stillborn.|
31
- | 2|2015-08-06|2015-08-06|Priscilla |Graceland |In 1957, when he was just 22, Elvis shelled out $102,500 for Graceland, the Memphis mansion that served as his home base for two decades.|
32
- | 3|2015-08-06|2015-08-06|Elvis Himself|Col. Tom Barker |Elvis's future manager immigrated illegally to America as a young man, where he reinvented himself as Tom Parker and claimed to be from West Virginia. He worked as a pitchman for traveling carnivals, followed by stints as dog catcher and pet cemetery founder, among other occupations, then managed the careers of several country music singers.|
33
-
34
- _Note: Check the unit tests for more Elvis Facts(tm)_
35
-
36
- Calling table_for on the table above would generate a table that looks like this:
37
-
38
- ```html
39
- <table class=' tablematic'>
40
- <thead>
41
- <tr>
42
- <th class='id integer'>Id</th>
43
- <th class='created_at datetime'>Created at</th>
44
- <th class='updated_at datetime'>Updated at</th>
45
- <th class='added_by string'>Added by</th>
46
- <th class='title string'>Title</th>
47
- <th class='content text'>Content</th>
48
- </tr>
49
- </thead>
50
- <tbody>
51
- <tr class='even'>
52
- <td class='id integer'>1</td>
53
- <td class='created_at datetime'>2015-08-06 12:04:05 UTC</td>
54
- <td class='updated_at datetime'>2015-08-06 12:04:05 UTC</td>
55
- <td class='added_by string'>Lisa Marie</td>
56
- <td class='title string'>Elvis had a Twin</td>
57
- <td class='content text'>On January 8, 1935, Elvis Aron (later spelled Aaron) Presley was born at his parents’ two-room house in East Tupelo, Mississippi, about 35 minutes after his identical twin brother, Jesse Garon, who was stillborn.</td>
58
- </tr>
59
- <tr class='odd'>
60
- <td class='id integer'>2</td>
61
- <td class='created_at datetime'>2015-08-06 12:04:05 UTC</td>
62
- <td class='updated_at datetime'>2015-08-06 12:04:05 UTC</td>
63
- <td class='added_by string'>Priscilla</td>
64
- <td class='title string'>Graceland</td>
65
- <td class='content text'>In 1957, when he was just 22, Elvis shelled out $102,500 for Graceland, the Memphis mansion that served as his home base for two decades.</td>
66
- </tr>
67
- <tr class='even'>
68
- <td class='id integer'>3</td>
69
- <td class='created_at datetime'>2015-08-06 12:04:05 UTC</td>
70
- <td class='updated_at datetime'>2015-08-06 12:04:05 UTC</td>
71
- <td class='added_by string'>Elvis Himself</td>
72
- <td class='title string'>Col. Tom Barker</td>
73
- <td class='content text'>Elvis’s future manager immigrated illegally to America as a young man, where he reinvented himself as Tom Parker and claimed to be from West Virginia. He worked as a pitchman for traveling carnivals, followed by stints as dog catcher and pet cemetery founder, among other occupations, then managed the careers of several country music singers.</td>
74
- </tr>
75
- </tbody>
76
- </table>
77
- ```
78
-
79
- Interesting stuff above:
80
- * It auto-generates odd/even classes on the rows in case you want to use them for styling
81
- * It also includes the parameter name and datatype on each column in case that's useful for styling (e.g., you might want to make integers align right).
82
- * Tables have the tablematic class by default.
83
-
84
- ### Customizing output
85
- The defaults are probably not exactly what you need, so you can tweak things as needed by
86
- supplying options in an hash as the second parameter to table_for. Here are some of the
87
- supported options:
88
-
89
- #### table_class:
90
- A string or array of strings to use as class name(s) on the table. This is handy if you're
91
- using a CSS framework like bootstrap:
92
-
93
- ```erb
94
- <%= table_for(@posts, table_class: 'table table-condensed') %>
95
- ```
96
-
97
- yields
98
-
99
- ```html
100
- <table class="tablematic table table-condensed">
101
- ...
102
- ```
103
-
104
- #### attributes:
105
- Limits the output to only those attributes included in the array. E.g.,
106
-
107
- ```erb
108
- <%= table_for(@posts, attributes: [:title, :content]) %>
109
- ```
110
-
111
- You can also specify the column header names this way by making the
112
- array elements into hashes where the key is the attribute name and
113
- the value is the title:
114
-
115
- ```erb
116
- <%= table_for(@posts, attributes: [{title: 'A cool title'}, {content: 'Awesome content'}]) %>
117
- ```
118
-
119
- ### Code Blocks for additional per-row output
120
- Let's say you want to include links to Edit or Delete actions on each row along
121
- with the table values. Anything that is supplied in a block to the table_for
122
- method will be appended to the end of each row that is generated. For example,
123
-
124
- ```erb
125
- <% table_for(@posts) do |post| %>
126
- <td><%= link_to "Edit", edit_post_path(post) %></td>
127
- <% end %>
128
- ```
129
-
130
- will generate the following:
131
-
132
- ```html
133
- <table class=' tablematic'>
134
- <thead>
135
- <tr>
136
- <th class='id integer'>Id</th>
137
- <th class='created_at datetime'>Created at</th>
138
- <th class='updated_at datetime'>Updated at</th>
139
- <th class='added_by string'>Added by</th>
140
- <th class='title string'>Title</th>
141
- <th class='content text'>Content</th>
142
- <th></th>
143
- </tr>
144
- </thead>
145
- <tbody>
146
- <tr class='even'>
147
- <td class='id integer'>1</td>
148
- <td class='created_at datetime'>2015-08-06 12:04:05 UTC</td>
149
- <td class='updated_at datetime'>2015-08-06 12:04:05 UTC</td>
150
- <td class='added_by string'>Lisa Marie</td>
151
- <td class='title string'>Elvis had a Twin</td>
152
- <td class='content text'>On January 8, 1935, Elvis Aron (later spelled Aaron) Presley was born at his parents’ two-room house in East Tupelo, Mississippi, about 35 minutes after his identical twin brother, Jesse Garon, who was stillborn.</td>
153
- <td><a href="/posts/1/edit">Edit</a></td>
154
- </tr>
155
- ...
156
- ```
157
-
158
- ### Global Configuration
159
- Right now there's only one option you can configure globally - I'm working
160
- on that. Here's how you do it - in an initializer, plop the following code:
161
-
162
- ```ruby
163
- Tablematic.configure do |config|
164
- config.table_class = 'table table-condensed'
165
- end
166
- ```
167
-
168
- This will make the default CSS classes on every generated table include the
169
- table and table-condensed classes, so you don't have to specify it explicitly
170
- everywhere you use the helper.
171
-
172
- ## TODO
173
- * Upload to rubygems and become FOSS famous.
174
- * Setup TravisCI
175
- * Change string additions to use << instead because string addition is expensive (or better yet use content_tag).
176
- * An exclude option to easily exclude columns, including at the global level.
177
- * Integration specs for better testing w/ Rails.
178
- * Maybe it'd be cool to have optional footers with totals? Might be wonky w/ pagination.
179
- * A way to specify column headings when you're including additional stuff in a block.
180
- * A way to specify that generate class names should use hyphens instead of underscores
181
- * Configurable class namespaces, some of that stuff is already there just haven't finished it.
182
- * Make it work w/ regular old arrays of hashes as well as AR.
183
-
184
- ## Note on Patches/Pull Requests
185
- * Fork the project.
186
- * Make your feature addition or bug fix.
187
- * Add specs for it.
188
- * Commit.
189
- * Send me a pull request. Bonus points for topic branches.
190
-
191
- ## Copyright
192
- Copyright 2015 Mike Desjardins. See MIT-LICENSE for details.
193
-