tablematic 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c7e31de7aad761a0668517eb443ffd019b4251e
4
+ data.tar.gz: e0fc8ad8f5c9a0bf563f291bfca9b0a0c9866c11
5
+ SHA512:
6
+ metadata.gz: bd67f2907fc0d539fdfd52a666a921497f6b6b20b64cc01ff9f504c622c016a393ff6b9d134a2f1f6f3de5fe4093c75bd8b35aad5e9abc1efc1fe9388a3e4a47
7
+ data.tar.gz: c9e993d9ade0999ea19be71cbd790f6a64411f1134723a2e2ecc09f6ac2c35d7a1f5f239235066bec4b89f3eb7291a59596a40fca7078fe02d2c207ae6e3a5d1
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,193 @@
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
+
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Tablematic'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
@@ -0,0 +1,69 @@
1
+ module TableHelper
2
+ class TableBuilder
3
+ def initialize(records, attr, opts={})
4
+ @records = records
5
+ @attr = attr
6
+ @model = @records.first.class
7
+ @namespace = opts.fetch(:namespace,'')
8
+ @config = Tablematic.configuration
9
+ @opts = opts
10
+ end
11
+
12
+ 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)
21
+ end
22
+
23
+ protected
24
+
25
+ def column_heading_for(attribute)
26
+ return attribute.values.first if attribute.is_a?(Hash)
27
+ return attribute.to_s.humanize
28
+ end
29
+
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>"
34
+ end
35
+ result += "</tr></thead>"
36
+ end
37
+
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>"
48
+ end
49
+ result += append_to_rows
50
+ result += "</tr>"
51
+ end
52
+ result += "</tbody>"
53
+ end
54
+
55
+ def attribute_css_class(attribute_name)
56
+ "#{@namespace}#{attribute_name.to_s.parameterize.underscore}"
57
+ end
58
+
59
+ def attribute_type_css_class(attribute_name)
60
+ column = @model.columns_hash[attribute_name.to_s]
61
+ "#{@namespace}#{column.type.to_s.parameterize.underscore}" if column
62
+ end
63
+
64
+ def table_classes
65
+ css_classes = @opts.fetch(:table_class, @config.table_class)
66
+ ([] << css_classes << 'tablematic').flatten.join(' ')
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,8 @@
1
+ class Configuration
2
+ attr_accessor :exclude, :table_class
3
+
4
+ def initialize
5
+ @exclude = []
6
+ @table_class = ''
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ require 'action_view'
2
+
3
+ module TableHelper
4
+ def table_for(records, options={}, &block)
5
+ attr = options[:attributes] || records.first.attribute_names
6
+ concat TableBuilder.new(records, attr, options).build(self, &block)
7
+ end
8
+ end
9
+
10
+ ActionView::Base.class_eval do
11
+ include TableHelper
12
+ end
@@ -0,0 +1,3 @@
1
+ module Tablematic
2
+ VERSION = "0.0.1"
3
+ end
data/lib/tablematic.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'tablematic/helper'
2
+ require 'tablematic/builder'
3
+ require 'tablematic/configuration'
4
+
5
+ module Tablematic
6
+ def self.configure
7
+ # raise Error::AlreadyConfigured unless @configuration.nil?
8
+
9
+ @configuration = Configuration.new
10
+ yield(@configuration)
11
+ end
12
+
13
+ def self.configuration
14
+ return @configuration || Configuration.new
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :tablematic do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tablematic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Desjardins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: database_cleaner
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: with_model
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Generates tables from AR collections.
84
+ email:
85
+ - mike.desjardins@cereslogic.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - MIT-LICENSE
91
+ - README.rdoc
92
+ - Rakefile
93
+ - lib/tablematic.rb
94
+ - lib/tablematic/builder.rb
95
+ - lib/tablematic/configuration.rb
96
+ - lib/tablematic/helper.rb
97
+ - lib/tablematic/version.rb
98
+ - lib/tasks/tablematic_tasks.rake
99
+ homepage: http://www.github.com/mdesjardins/tablematic
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.2.2
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Generates tables from AR collections.
123
+ test_files: []