table_for 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/README.rdoc CHANGED
@@ -1,20 +1,22 @@
1
- == TableFor
1
+ = TableFor
2
2
 
3
- Renders a table for your model collection.
4
- Like show_for, but for collections...
3
+ Renders a table for your model collection.
4
+ Like show_for, but for collections...
5
5
 
6
6
  == Disclaimer
7
7
 
8
- Should not be considered stable at all this point.
9
- (The gem have no specs! OMG!)
8
+ Should not be considered stable.
9
+ Briefly tested under rails 3 running ruby 1.8.7 and 1.9.2
10
+ Test coverage not yet up to speed.
10
11
 
11
12
  == Installation
12
13
 
13
- gem install table_for
14
+ Add the below line to your gem file and run bundle install
15
+ gem 'table_for'
14
16
 
15
17
  == Basic Usage
16
18
 
17
- # In your erb views:
19
+ In your erb views:
18
20
  <%= table_for Product, @products do |table| %>
19
21
  <%= table.head :name, :size, :description, :price %>
20
22
  <%= table.body do |row| %>
@@ -27,7 +29,7 @@
27
29
  <% end %>
28
30
  <% end %>
29
31
 
30
- # Or:
32
+ Or:
31
33
  <%= table_for Product, @products do |table| %>
32
34
  <%= table.columns :name, :size, :description, :price %>
33
35
  <%= table.foot do %>
@@ -35,8 +37,10 @@
35
37
  <% end %>
36
38
  <% end %>
37
39
 
40
+ The footer if not shown if there is the view helper "admin?" is defined and returns false.
41
+
38
42
  == Credits and contributors
39
43
 
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.
44
+ * Jonas Nicklas (jnicklas) who actually wrote most of the code while we pair-programmed.
45
+ * Elabs for sponsoring with development time.
46
+ * The authors of the show_for gem for providing the inspiration.
data/lib/table_for.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  require 'table_for/helper'
2
2
 
3
3
  module TableFor
4
+ autoload :Table, 'table_for/table'
5
+ autoload :TableBuilder, 'table_for/table_builder'
6
+ autoload :RowBuilder, 'table_for/row_builder'
4
7
  end
@@ -1,93 +1,37 @@
1
1
  module TableFor
2
2
  module Helper
3
- # Create a table for records, using model class for naming things.
3
+ # Create a html table for records, using model class for naming things.
4
4
  #
5
- # Example:
5
+ # Examples:
6
6
  #
7
- # table_for User @users do |table|
8
- # table.columns :first_name, :last_name
9
- # end
7
+ # <tt>table_for Product, @products do |table|
8
+ # table.head :name, :size, :description, :price
9
+ # table.body do |row|
10
+ # row.cell :name
11
+ # row.cells :size, :description
12
+ # row.cell number_to_currency(row.record.price)
13
+ # end
14
+ # table.foot do
15
+ # link_to "Add product", new_product_path
16
+ # end
17
+ # end</tt>
10
18
  #
19
+ # <tt>table_for Product, @products do |table|
20
+ # table.columns :name, :size, :description, :price
21
+ # table.foot do
22
+ # link_to "Add product", new_product_path
23
+ # end
24
+ # end</tt>
25
+ #
26
+ # Returns:
27
+ #
28
+ # A string containing the html table
29
+ #
30
+ # (Call this method from your erb templates by wrapping each line in <%= %> or <% %>)
31
+
11
32
  def table_for(model_class, records, &block)
12
33
  Table.new(self, model_class, records, block).render
13
34
  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
35
  end
92
36
  end
93
37
 
@@ -0,0 +1,16 @@
1
+ module TableFor
2
+ class RowBuilder < Struct.new(:table, :record)
3
+ delegate :template, :to => :table
4
+ delegate :content_tag, :to => :template
5
+
6
+ def cells(*values)
7
+ values.map { |v| cell(v) }.join.html_safe
8
+ end
9
+
10
+ def cell(value)
11
+ content_tag(:td) do
12
+ if value.is_a?(Symbol) then record.send(value).to_s else value.to_s end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ module TableFor
2
+ class Table < Struct.new(:template, :model_class, :records, :block)
3
+ attr_accessor :columns, :rows
4
+ delegate :capture, :content_tag, :to => :template
5
+
6
+ def human_column_names
7
+ columns.map { |column| model_class.human_attribute_name(column) }
8
+ end
9
+
10
+ def row_builders
11
+ records.map { |record| RowBuilder.new(self, record) }
12
+ end
13
+
14
+ def render
15
+ content_tag(:table, :class => 'list') do
16
+ caption + capture(TableBuilder.new(self), &block)
17
+ end
18
+ end
19
+
20
+ protected
21
+
22
+ def caption
23
+ content_tag(:caption, human_association_name)
24
+ end
25
+
26
+ def human_association_name
27
+ model_class.model_name.human.pluralize
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,45 @@
1
+ module TableFor
2
+ class TableBuilder < Struct.new(:table)
3
+ delegate :template, :human_column_names, :to => :table
4
+ delegate :capture, :content_tag, :to => :template
5
+
6
+ def columns(*columns)
7
+ head(*columns) + body(*columns)
8
+ end
9
+
10
+ def head(*columns)
11
+ table.columns = columns
12
+
13
+ content_tag(:thead) do
14
+ content_tag(:tr) do
15
+ human_column_names.map { |column| content_tag(:th, column) }.join
16
+ end
17
+ end.html_safe
18
+ end
19
+
20
+ def body(*columns, &block)
21
+ content_tag(:tbody) do
22
+ table.row_builders.map do |builder|
23
+ content_tag(:tr) do
24
+ if block then capture(builder, &block) else builder.cells(*columns) end
25
+ end
26
+ end.join.html_safe
27
+ end
28
+ end
29
+
30
+ def foot(&block)
31
+ if admin?
32
+ content_tag(:tfoot) do
33
+ content_tag(:tr) do
34
+ content_tag(:td, capture(&block), :colspan => table.columns.size)
35
+ end
36
+ end.html_safe
37
+ end
38
+ end
39
+
40
+ def admin?
41
+ return true unless template.respond_to?(:admin?)
42
+ template.admin?
43
+ end
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  module TableFor
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,11 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+
3
+ $:.unshift File.expand_path('../lib', __FILE__)
4
+
5
+ require 'rails'
6
+ require 'rubygems'
7
+ require 'bundler/setup'
8
+ require "rails/test_help"
9
+
10
+ Dir["#{File.expand_path(File.dirname(__FILE__))}/../lib/table_for/*.rb"].each {|f| require(f) unless f =~ /version\.rb$/ }
11
+ Dir["#{File.expand_path(File.dirname(__FILE__))}/support/*.rb"].each {|f| require(f) }
@@ -0,0 +1,17 @@
1
+ module TestTableFor
2
+ class User < Struct.new(:first_name, :last_name)
3
+ class << self
4
+ def human_attribute_name(attribute)
5
+ attribute.to_s.humanize
6
+ end
7
+
8
+ def model_name
9
+ self
10
+ end
11
+
12
+ def human
13
+ "User"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe TableFor::Table do
4
+ let(:template) { ActionView::Base.new }
5
+ let(:users) do
6
+ [TestTableFor::User.new(:first_name => "Nicklas", :last_name => "Ramhöj"),
7
+ TestTableFor::User.new(:first_name => "Jonas", :last_name => "Nicklas")]
8
+ end
9
+ subject { TableFor::Table.new(template, TestTableFor::User, users, lambda { "content" }) }
10
+ before { subject.stub(:columns).and_return([:first_name, :last_name]) }
11
+
12
+ describe "#human_column_names" do
13
+ it "returns a list with huminzed column names" do
14
+ subject.human_column_names.should == ["First name", "Last name"]
15
+ end
16
+ end
17
+
18
+ describe "#row_builders" do
19
+ it "returns a row builder for each record" do
20
+ subject.row_builders.map(&:class).should == [TableFor::RowBuilder, TableFor::RowBuilder]
21
+ end
22
+ end
23
+
24
+ describe "#render" do
25
+ it "wrapps the caption, header, body and footer in a table tag" do
26
+ subject.render.should include(%Q{<table class="list">})
27
+ subject.render.should include(%Q{<caption>Users</caption>})
28
+ subject.render.should include(%Q{</table>})
29
+ end
30
+ end
31
+
32
+ describe "#human_association_name" do
33
+ it "returns the collection name" do
34
+ subject.send(:human_association_name).should == "Users"
35
+ end
36
+ end
37
+
38
+ describe "#caption" do
39
+ it "wraps the human_association_name in a caption tag" do
40
+ subject.send(:caption).should == "<caption>Users</caption>"
41
+ end
42
+ end
43
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_for
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Nicklas Ramh\xC3\xB6j"
@@ -16,13 +16,31 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-10-05 00:00:00 +02:00
19
+ date: 2010-10-16 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
23
- name: rails
23
+ name: rspec
24
24
  prerelease: false
25
25
  requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 62196427
31
+ segments:
32
+ - 2
33
+ - 0
34
+ - 0
35
+ - beta
36
+ - 20
37
+ version: 2.0.0.beta.20
38
+ type: :development
39
+ version_requirements: *id001
40
+ - !ruby/object:Gem::Dependency
41
+ name: rails
42
+ prerelease: false
43
+ requirement: &id002 !ruby/object:Gem::Requirement
26
44
  none: false
27
45
  requirements:
28
46
  - - ~>
@@ -33,7 +51,7 @@ dependencies:
33
51
  - 0
34
52
  version: "3.0"
35
53
  type: :runtime
36
- version_requirements: *id001
54
+ version_requirements: *id002
37
55
  description: Renders a table for your model collection
38
56
  email:
39
57
  - ramhoj@gmail.com
@@ -46,8 +64,14 @@ extra_rdoc_files:
46
64
  - README.rdoc
47
65
  files:
48
66
  - lib/table_for/helper.rb
67
+ - lib/table_for/row_builder.rb
68
+ - lib/table_for/table.rb
69
+ - lib/table_for/table_builder.rb
49
70
  - lib/table_for/version.rb
50
71
  - lib/table_for.rb
72
+ - spec/spec_helper.rb
73
+ - spec/support/user.rb
74
+ - spec/table_for/table_spec.rb
51
75
  - README.rdoc
52
76
  has_rdoc: true
53
77
  homepage: http://github.com/ramhoj/table_for