tubular 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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +141 -0
- data/Rakefile +1 -0
- data/lib/tubular/builder.rb +66 -0
- data/lib/tubular/column.rb +53 -0
- data/lib/tubular/helper.rb +12 -0
- data/lib/tubular/railtie.rb +7 -0
- data/lib/tubular/version.rb +3 -0
- data/lib/tubular.rb +6 -0
- data/tubular.gemspec +19 -0
- metadata +14 -3
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ryan Closner
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
# Tubular
|
2
|
+
|
3
|
+
Tubular helps you quickly and easily generate HTML tables.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'tubular'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install tubular
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Tubular was designed with collections in mind.
|
22
|
+
|
23
|
+
Given a model like the following:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class User < ActiveRecord::Base
|
27
|
+
attr_accesible :first_name, :last_name, :email
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
And a collection populated with instances of the given model:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
@users = [
|
35
|
+
User.new(:first_name => "John", :last_name => "Wayne", :email => "john@wayne.com"),
|
36
|
+
User.new(:first_name => "Air", :last_name => "Jordan", :email => "air@jordan.com"),
|
37
|
+
User.new(:first_name => "Charles", :last_name => "Bukowski", :email => "c@bukowski.com")
|
38
|
+
]
|
39
|
+
```
|
40
|
+
|
41
|
+
One can easily generate an HTML table in the following manner:
|
42
|
+
|
43
|
+
```erb
|
44
|
+
<%= table_for(@users) do %>
|
45
|
+
<% column :first_name %>
|
46
|
+
<% column :last_name %>
|
47
|
+
<% column :email %>
|
48
|
+
<% end %>
|
49
|
+
```
|
50
|
+
|
51
|
+
This ERB template would generate the following HTML:
|
52
|
+
|
53
|
+
```html
|
54
|
+
<table>
|
55
|
+
<thead>
|
56
|
+
<tr>
|
57
|
+
<th>First Name</th>
|
58
|
+
<th>Last Name</th>
|
59
|
+
<th>Email</th>
|
60
|
+
<tr>
|
61
|
+
</thead>
|
62
|
+
<tbody>
|
63
|
+
<tr>
|
64
|
+
<td>John</td>
|
65
|
+
<td>Wayne</td>
|
66
|
+
<td>john@wayne.com</td>
|
67
|
+
</tr>
|
68
|
+
<tr>
|
69
|
+
<td>Air</td>
|
70
|
+
<td>Jordan</td>
|
71
|
+
<td>air@jordan.com</td>
|
72
|
+
</tr>
|
73
|
+
<tr>
|
74
|
+
<td>Charles</td>
|
75
|
+
<td>Bukowski</td>
|
76
|
+
<td>c@bukowski.com</td>
|
77
|
+
</tr>
|
78
|
+
</tbody>
|
79
|
+
</table>
|
80
|
+
```
|
81
|
+
|
82
|
+
# API
|
83
|
+
|
84
|
+
In order to customize the output of the generated HTML, one can pass the following
|
85
|
+
options:
|
86
|
+
|
87
|
+
```erb
|
88
|
+
<%= table_for(@users, :table_html => { :class => "index_table" }) do %>
|
89
|
+
<% column :first_name, :header_html => { :class => "sortable" } %>
|
90
|
+
<% column :last_name, :cell_html => { :class => "featured" } %>
|
91
|
+
<% column :email, :header_content => proc { "Email #{1 + 1}" } %>
|
92
|
+
<% column :full_name, :header_content => "Full Name", :cell_content => proc {|user| "#{ user.first_name } #{ user.last_name }" } %>
|
93
|
+
<% column :actions, :header_content => "", :cell_content => "Some action." %>
|
94
|
+
<% end %>
|
95
|
+
```
|
96
|
+
This ERB template would generate the following HTML:
|
97
|
+
|
98
|
+
```html
|
99
|
+
<table class="index_table">
|
100
|
+
<thead>
|
101
|
+
<tr>
|
102
|
+
<th class="sortable">First Name</th>
|
103
|
+
<th>Last Name</th>
|
104
|
+
<th>Email 2</th>
|
105
|
+
<th>Full Name</th>
|
106
|
+
<th></th>
|
107
|
+
<tr>
|
108
|
+
</thead>
|
109
|
+
<tbody>
|
110
|
+
<tr>
|
111
|
+
<td>John</td>
|
112
|
+
<td class="featured">Wayne</td>
|
113
|
+
<td>john@wayne.com</td>
|
114
|
+
<td>John Wayne</td>
|
115
|
+
<td>Some action.</td>
|
116
|
+
</tr>
|
117
|
+
<tr>
|
118
|
+
<td>Air</td>
|
119
|
+
<td class="featured">Jordan</td>
|
120
|
+
<td>air@jordan.com</td>
|
121
|
+
<td>Air Jordan</td>
|
122
|
+
<td>Some action.</td>
|
123
|
+
</tr>
|
124
|
+
<tr>
|
125
|
+
<td>Charles</td>
|
126
|
+
<td class="featured">Bukowski</td>
|
127
|
+
<td>c@bukowski.com</td>
|
128
|
+
<td>Charles Bukowski</td>
|
129
|
+
<td>Some action.</td>
|
130
|
+
</tr>
|
131
|
+
</tbody>
|
132
|
+
</table>
|
133
|
+
```
|
134
|
+
|
135
|
+
## Contributing
|
136
|
+
|
137
|
+
1. Fork it
|
138
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
139
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
140
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
141
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Tubular
|
2
|
+
class Builder
|
3
|
+
|
4
|
+
delegate :content_tag, :to => :context
|
5
|
+
|
6
|
+
def self.render(*args, &block)
|
7
|
+
new(*args, &block).render
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(attributes, &block)
|
11
|
+
@context = attributes.fetch(:context)
|
12
|
+
@collection = attributes.fetch(:collection, [])
|
13
|
+
@table_html = attributes.fetch(:table_html, {})
|
14
|
+
@columns = []
|
15
|
+
|
16
|
+
if block_given?
|
17
|
+
instance_eval(&block)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def render
|
22
|
+
content_tag(:table, table_html) do
|
23
|
+
(render_table_head + render_table_body) if columns.size > 0
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def column(*args, &block)
|
28
|
+
columns << build_column(*args, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
attr_reader :context, :collection, :columns, :table_html
|
34
|
+
|
35
|
+
def build_column(*args, &block)
|
36
|
+
Column.new(context, *args, &block)
|
37
|
+
end
|
38
|
+
|
39
|
+
def render_table_head
|
40
|
+
content_tag(:thead) do
|
41
|
+
content_tag(:tr) do
|
42
|
+
columns.map do |column|
|
43
|
+
content_tag(:th, column.header_html) do
|
44
|
+
column.render_header_content
|
45
|
+
end
|
46
|
+
end.join.html_safe
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def render_table_body
|
52
|
+
content_tag(:tbody) do
|
53
|
+
collection.map do |record|
|
54
|
+
content_tag(:tr) do
|
55
|
+
columns.map do |column|
|
56
|
+
content_tag(:td, column.cell_html) do
|
57
|
+
column.render_body_content(record)
|
58
|
+
end
|
59
|
+
end.join.html_safe
|
60
|
+
end
|
61
|
+
end.join.html_safe
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end # class Builder
|
66
|
+
end # module Tubular
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Tubular
|
2
|
+
class Column
|
3
|
+
|
4
|
+
attr_reader :context, :method_name, :header_html,
|
5
|
+
:header_content, :cell_html, :cell_content
|
6
|
+
|
7
|
+
delegate :t, :to => :context
|
8
|
+
|
9
|
+
def initialize(context, *args, &block)
|
10
|
+
attributes = args.extract_options!
|
11
|
+
|
12
|
+
@context = context
|
13
|
+
@method_name = attributes.fetch(:method_name, args.shift)
|
14
|
+
@header_html = attributes.fetch(:header_html, {})
|
15
|
+
@header_content = attributes.fetch(:header_content, nil)
|
16
|
+
@cell_html = attributes.fetch(:cell_html, {})
|
17
|
+
@cell_content = attributes.fetch(:cell_content, nil)
|
18
|
+
|
19
|
+
if block_given?
|
20
|
+
@cell_content = block
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def render_header_content
|
25
|
+
case header_content
|
26
|
+
when Proc
|
27
|
+
header_content.call
|
28
|
+
when String, Symbol
|
29
|
+
t(header_content)
|
30
|
+
when NilClass
|
31
|
+
method_name ? t(method_name) : nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def render_body_content(record)
|
36
|
+
case cell_content
|
37
|
+
when Proc
|
38
|
+
call_content_proc(cell_content, record)
|
39
|
+
when NilClass
|
40
|
+
method_name && record.respond_to?(method_name) ? record.send(method_name) : nil
|
41
|
+
else
|
42
|
+
cell_content.to_s
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
def call_content_proc(content, record = nil)
|
49
|
+
content.arity > 0 ? content.call(record) : content.call
|
50
|
+
end
|
51
|
+
|
52
|
+
end # class Column
|
53
|
+
end # module Tubular
|
data/lib/tubular.rb
ADDED
data/tubular.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tubular/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "tubular"
|
8
|
+
gem.version = Tubular::VERSION
|
9
|
+
gem.authors = ["Ryan Closner"]
|
10
|
+
gem.email = ["ryan@ryanclosner.com"]
|
11
|
+
gem.description = %q{Helper to quickly and easily generate HTML tables}
|
12
|
+
gem.summary = %q{Helper to quickly and easily generate HTML tables}
|
13
|
+
gem.homepage = "http://github.com/rclosner/tubular"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tubular
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -17,7 +17,19 @@ email:
|
|
17
17
|
executables: []
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
|
-
files:
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/tubular.rb
|
27
|
+
- lib/tubular/builder.rb
|
28
|
+
- lib/tubular/column.rb
|
29
|
+
- lib/tubular/helper.rb
|
30
|
+
- lib/tubular/railtie.rb
|
31
|
+
- lib/tubular/version.rb
|
32
|
+
- tubular.gemspec
|
21
33
|
homepage: http://github.com/rclosner/tubular
|
22
34
|
licenses: []
|
23
35
|
post_install_message:
|
@@ -43,4 +55,3 @@ signing_key:
|
|
43
55
|
specification_version: 3
|
44
56
|
summary: Helper to quickly and easily generate HTML tables
|
45
57
|
test_files: []
|
46
|
-
has_rdoc:
|