table_for 0.0.1
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/README.rdoc +42 -0
- data/lib/table_for.rb +4 -0
- data/lib/table_for/helper.rb +94 -0
- data/lib/table_for/version.rb +3 -0
- metadata +88 -0
data/README.rdoc
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
== TableFor
|
2
|
+
|
3
|
+
Renders a table for your model collection.
|
4
|
+
Like show_for, but for collections...
|
5
|
+
|
6
|
+
== Disclaimer
|
7
|
+
|
8
|
+
Should not be considered stable at all this point.
|
9
|
+
(The gem have no specs! OMG!)
|
10
|
+
|
11
|
+
== Installation
|
12
|
+
|
13
|
+
gem install table_for
|
14
|
+
|
15
|
+
== Basic Usage
|
16
|
+
|
17
|
+
# In your erb views:
|
18
|
+
<%= table_for Product, @products do |table| %>
|
19
|
+
<%= table.head :name, :size, :description, :price %>
|
20
|
+
<%= table.body do |row| %>
|
21
|
+
<%= row.cell :name %>
|
22
|
+
<%= row.cells :size, :description %>
|
23
|
+
<%= row.cell number_to_currency(row.record.price) %>
|
24
|
+
<% end %>
|
25
|
+
<%= table.foot do %>
|
26
|
+
<%= link_to "Add product", new_product_path %>
|
27
|
+
<% end %>
|
28
|
+
<% end %>
|
29
|
+
|
30
|
+
# Or:
|
31
|
+
<%= table_for Product, @products do |table| %>
|
32
|
+
<%= table.columns :name, :size, :description, :price %>
|
33
|
+
<%= table.foot do %>
|
34
|
+
<%= link_to "Add product", new_product_path %>
|
35
|
+
<% end %>
|
36
|
+
<% end %>
|
37
|
+
|
38
|
+
== Credits and contributors
|
39
|
+
|
40
|
+
* Jonas Nicklas (jnickals) who actually wrote mote of the code while we pair-programmed.
|
41
|
+
* Elabs for sponsoring with development time
|
42
|
+
* The authors of show_for for providing the inspiration.
|
data/lib/table_for.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
module TableFor
|
2
|
+
module Helper
|
3
|
+
# Create a table for records, using model class for naming things.
|
4
|
+
#
|
5
|
+
# Example:
|
6
|
+
#
|
7
|
+
# table_for User @users do |table|
|
8
|
+
# table.columns :first_name, :last_name
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
def table_for(model_class, records, &block)
|
12
|
+
Table.new(self, model_class, records, block).render
|
13
|
+
end
|
14
|
+
|
15
|
+
class Table < Struct.new(:template, :model_class, :records, :block)
|
16
|
+
attr_accessor :columns, :rows
|
17
|
+
delegate :capture, :content_tag, :to => :template
|
18
|
+
|
19
|
+
def human_association_name
|
20
|
+
model_class.model_name.human.pluralize
|
21
|
+
end
|
22
|
+
|
23
|
+
def human_column_names
|
24
|
+
columns.map { |column| model_class.human_attribute_name(column) }
|
25
|
+
end
|
26
|
+
|
27
|
+
def row_builders
|
28
|
+
records.map { |record| RowBuilder.new(self, record) }
|
29
|
+
end
|
30
|
+
|
31
|
+
def render
|
32
|
+
content_tag(:table, :class => 'list') do
|
33
|
+
content_tag(:caption, human_association_name) + capture(TableBuilder.new(self), &block)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class TableBuilder < Struct.new(:table)
|
39
|
+
delegate :template, :human_column_names, :to => :table
|
40
|
+
delegate :capture, :content_tag, :admin?, :to => :template
|
41
|
+
|
42
|
+
def columns(*columns)
|
43
|
+
head(*columns) + body(*columns)
|
44
|
+
end
|
45
|
+
|
46
|
+
def head(*columns)
|
47
|
+
table.columns = columns
|
48
|
+
|
49
|
+
content_tag(:thead) do
|
50
|
+
content_tag(:tr) do
|
51
|
+
human_column_names.map { |column| content_tag(:th, column) }.join
|
52
|
+
end
|
53
|
+
end.html_safe
|
54
|
+
end
|
55
|
+
|
56
|
+
def body(*columns, &block)
|
57
|
+
content_tag(:tbody) do
|
58
|
+
table.row_builders.map do |builder|
|
59
|
+
content_tag(:tr) do
|
60
|
+
if block then capture(builder, &block) else builder.cells(*columns) end
|
61
|
+
end
|
62
|
+
end.join.html_safe
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def foot(&block)
|
67
|
+
if admin?
|
68
|
+
content_tag(:tfoot) do
|
69
|
+
content_tag(:tr) do
|
70
|
+
content_tag(:td, capture(&block), :colspan => table.columns.size)
|
71
|
+
end
|
72
|
+
end.html_safe
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class RowBuilder < Struct.new(:table, :record)
|
78
|
+
delegate :template, :to => :table
|
79
|
+
delegate :content_tag, :to => :template
|
80
|
+
|
81
|
+
def cells(*values)
|
82
|
+
values.map { |v| cell(v) }.join.html_safe
|
83
|
+
end
|
84
|
+
|
85
|
+
def cell(value)
|
86
|
+
content_tag(:td) do
|
87
|
+
if value.is_a?(Symbol) then record.send(value).to_s else value.to_s end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
ActionView::Base.send :include, TableFor::Helper
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: table_for
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "Nicklas Ramh\xC3\xB6j"
|
14
|
+
- Jonas Nicklas
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-10-05 00:00:00 +02:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: rails
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 7
|
31
|
+
segments:
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
version: "3.0"
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Renders a table for your model collection
|
38
|
+
email:
|
39
|
+
- ramhoj@gmail.com
|
40
|
+
- jnicklas@gmail.com
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
extra_rdoc_files:
|
46
|
+
- README.rdoc
|
47
|
+
files:
|
48
|
+
- lib/table_for/helper.rb
|
49
|
+
- lib/table_for/version.rb
|
50
|
+
- lib/table_for.rb
|
51
|
+
- README.rdoc
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/ramhoj/table_for
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options:
|
58
|
+
- --main
|
59
|
+
- README.rdoc
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project: table_for
|
83
|
+
rubygems_version: 1.3.7
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Renders a table for your model collection
|
87
|
+
test_files: []
|
88
|
+
|