table_for_collection 1.0.3 → 1.0.4

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/Changelog CHANGED
@@ -1,3 +1,9 @@
1
+ == master
2
+
3
+ Add option time_format for simple column
4
+ Add shortcut for column width
5
+ Translate column name if possible
6
+
1
7
  == v.1.0.3
2
8
 
3
9
  Callback column for given attribute
data/Gemfile CHANGED
@@ -3,14 +3,8 @@ source :gemcutter
3
3
  gem "actionpack"
4
4
 
5
5
  group :development, :test do
6
- gem "rspec", ">=2.0.0"
7
- gem "autotest"
6
+ gem "rspec"
8
7
  gem "webrat"
9
-
10
- case RUBY_VERSION
11
- when /^1\.9/
12
- gem 'ruby-debug19'
13
- when /^1\.8/
14
- gem 'ruby-debug'
15
- end
8
+ #gem 'ruby-debug19', :platforms => :ruby_19
9
+ gem 'ruby-debug', :platforms => :ruby_18
16
10
  end
data/README.rdoc CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  table_for_collection is a simple gem used to render tables based on the given collection.
4
4
 
5
+ === Install
6
+
7
+ Just add to your Gemfile
8
+
9
+ gem 'table_for_collection'
10
+
5
11
  === Simplest examle
6
12
 
7
13
  <%= table_for @users do -%>
@@ -35,32 +41,38 @@ This ruby code will produce following HTML:
35
41
  :id => "users",
36
42
  :width => "100%",
37
43
  :tr => {
38
- :class => "simple-row"
44
+ :class => "simple-row",
45
+ :id => lambda { |user| "user-row-#{user.id}" }
39
46
  }
40
47
  } do -%>
41
48
  <% column :name, :html => { :th => { :width => "25%" }, :td => { :class => "user-name" }} %>
42
- <% column :email %>
49
+ <% column :email, :width => "20%" %>
43
50
  <% column :address, :title => "Home" %>
44
51
  <% end %>
45
52
 
46
53
  You can put :html hash to the options list (inside the :column method too). In this case
47
- table will be created with given html attributes. Also :title value in the :column method
48
- will be used as column's title. It will produce HTML similar to:
54
+ table will be created with given html attributes. To assign :tr options use :tr key under :html - :class value will be added
55
+ to the classes list, :id can be instance of the Proc class, in this it will be called for each collection element.
56
+ For column width you can use shortcut :width, remember that :width shortcut has higher priority.
57
+ Also :title value in the :column method will be used as column's title. It will produce HTML similar to:
49
58
 
50
59
  <table class="simple-table" id="users" width="100%">
51
60
  <thead>
52
61
  <tr>
53
62
  <th width="25%">Name</th>
54
- <th>Email</th>
63
+ <th width="20%">Email</th>
55
64
  <th>Home</th>
56
65
  </tr>
57
66
  </thead>
58
67
  <tbody>
59
- <tr class="simple-row">
68
+ <tr class="simple-row" id="user-row-12">
60
69
  <td class="user-name">...</td>
61
70
  <td>...</td>
62
71
  <td>...</td>
63
72
  </tr>
73
+ <tr class="simple-row" id="user-row-14">
74
+ ...
75
+ </tr>
64
76
  </tbody>
65
77
  </table>
66
78
 
@@ -78,6 +90,7 @@ will be used as column's title. It will produce HTML similar to:
78
90
  <% column :title => "Actions" do |user| %>
79
91
  <% [link_to("Show", user), link_to("Delete", user, :method => :delete)].join(" | ") %>
80
92
  <% end %>
93
+ <% column :created_at, :time_format => "%Y-%m-%d" %>
81
94
  <% end %>
82
95
 
83
96
  === Example with colorized rows
@@ -95,6 +108,11 @@ Also class given by :stripe option will be merged with options[:html][:tr] so th
95
108
  <tr class="row even">...
96
109
  <tr class="row odd">...
97
110
 
111
+ == I18n
112
+
113
+ When column name is set, but :title is not, helper tries to find translation in standard mechanism of the ActiveModel.
114
+ For more information visit: http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models
115
+
98
116
  == Known issues
99
117
 
100
118
  Unfortunately it works incorrect if there is no "-" before %> in this code:
data/Rakefile CHANGED
@@ -1,5 +1,7 @@
1
1
  require "rspec/core/rake_task"
2
2
 
3
+ task :default => :spec
4
+
3
5
  desc "Test table_for_collection plugin."
4
6
  RSpec::Core::RakeTask.new('spec')
5
7
 
data/init.rb CHANGED
@@ -1 +1 @@
1
- require 'table_for'
1
+ require 'table_for_collection'
@@ -1,12 +1,11 @@
1
1
  module TableHelper
2
2
  class CallbackColumn < Column # :nodoc:
3
- def initialize(template, obj, ops)
3
+ def initialize(template, records, obj, ops)
4
4
  super
5
5
  @callback = @options.delete(:callback)
6
6
  end
7
-
8
7
  def content_for(record)
9
- @attr ? @callback.call(record.send(@attr).to_s) : @callback.call(record)
8
+ @attr ? @callback.call(record.kind_of?(Hash) ? record[@attr] : record.send(@attr)) : @callback.call(record)
10
9
  end
11
10
  end
12
11
  end
@@ -3,10 +3,23 @@ module TableHelper
3
3
  attr_reader :title, :html
4
4
  delegate :content_tag, :to => :@template
5
5
 
6
- def initialize(template, obj, ops)
6
+ def initialize(template, records, obj, ops={})
7
7
  @template, @attr, @options = template, obj, ops
8
- @title = @options.delete(:title) || @attr.to_s.humanize.presence || "&nbsp;"
8
+
9
+ @title = if @options[:title]
10
+ @options.delete(:title)
11
+ elsif @attr.nil?
12
+ "&nbsp;"
13
+ elsif records.class.respond_to?(:human_attribute_name)
14
+ records.class.human_attribute_name(@attr.to_s)
15
+ elsif @attr.to_s.respond_to?(:humanize)
16
+ @attr.to_s.humanize
17
+ else
18
+ @attr.to_s.capitalize
19
+ end
20
+
9
21
  @html = @options.delete(:html) || {}
22
+ @html.merge!({:th => { :width => @options.delete(:width) }}) if @options[:width]
10
23
  end
11
24
 
12
25
  def content_for
@@ -30,7 +30,7 @@ module TableHelper
30
30
  def table_for(records, *args, &proc)
31
31
  raise ArgumentError, "Missing block" unless block_given?
32
32
  options = args.extract_options!
33
- raise ArgumentError, "Records should be array" unless records.is_a? Enumerable
33
+ raise ArgumentError, "Records should have #map method" unless records.respond_to?(:map)
34
34
  t = Table.new(self, records, options)
35
35
  t.instance_eval(&proc)
36
36
  t.draw
@@ -1,7 +1,11 @@
1
1
  module TableHelper
2
2
  class SimpleColumn < Column # :nodoc:
3
3
  def content_for(record)
4
- record.send(@attr).to_s
4
+ called_record = record.kind_of?(Hash) ? record[@attr] : record.send(@attr)
5
+ if @options[:time_format] and called_record.is_a? Time
6
+ called_record = called_record.strftime(@options[:time_format])
7
+ end
8
+ called_record.to_s
5
9
  end
6
10
  end
7
11
  end
@@ -2,7 +2,7 @@ require "core_ex/array"
2
2
 
3
3
  module TableHelper
4
4
  class Table # :nodoc:
5
- delegate :content_tag, :to => :@template
5
+ delegate :content_tag, :content_tag_for, :dom_id, :dom_class, :to => :@template
6
6
 
7
7
  def initialize(template, records, options = {})
8
8
  @template, @records, @columns = template, records, []
@@ -17,7 +17,7 @@ module TableHelper
17
17
 
18
18
  def columns(*args)
19
19
  res = []
20
- unless args.blank?
20
+ if args.present?
21
21
  args.each do |arg|
22
22
  res << self.column(arg)
23
23
  end
@@ -35,9 +35,9 @@ module TableHelper
35
35
 
36
36
  if block_given?
37
37
  col_options[:callback] = block
38
- @columns << (res = CallbackColumn.new(@template, attr, col_options))
38
+ @columns << (res = CallbackColumn.new(@template, @records, attr, col_options))
39
39
  elsif attr
40
- @columns << (res = SimpleColumn.new(@template, attr, col_options))
40
+ @columns << (res = SimpleColumn.new(@template, @records, attr, col_options))
41
41
  else
42
42
  raise ArgumentError, "Attribute name or block should be given"
43
43
  end
@@ -69,7 +69,7 @@ module TableHelper
69
69
  def body
70
70
  content_tag :tbody do
71
71
  @records.map do |rec|
72
- content_tag(:tr, tr_options) do
72
+ content_tag :tr, tr_options(rec) do
73
73
  @columns.map do |col|
74
74
  content_tag :td, col.html[:td] do
75
75
  col.content_for(rec).to_s
@@ -80,16 +80,18 @@ module TableHelper
80
80
  end
81
81
  end
82
82
 
83
- def tr_options
83
+ def tr_options(rec)
84
84
  res = @tr_html_options.nil? ? {} : @tr_html_options.clone
85
85
  html_class = @stripes.next
86
86
  unless html_class.nil?
87
- if res.has_key?(:class)
88
- res[:class] += " " + html_class
89
- else
90
- res.merge!({ :class => html_class })
91
- end
87
+ klasses = [res[:class], html_class]
88
+ klasses << dom_class(rec) if rec.respond_to?(:model_name)
89
+ res[:class] = klasses.compact.join(" ")
90
+ end
91
+ if res.has_key?(:id) && res[:id].respond_to?(:call)
92
+ res[:id] = res[:id].call(rec)
92
93
  end
94
+ res[:id] ||= dom_id(rec) if rec.respond_to?(:to_key)
93
95
  res
94
96
  end
95
97
 
@@ -1,4 +1,4 @@
1
1
  module TableHelper
2
2
  GEM_NAME = "table_for_collection"
3
- VERSION = "1.0.3"
3
+ VERSION = "1.0.4"
4
4
  end
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,7 @@ require "table_for_collection"
2
2
  require "webrat"
3
3
 
4
4
  def build_column(klass, arg, options = {})
5
- klass.new(template, arg, options)
5
+ klass.new(template, mock(:test), arg, options)
6
6
  end
7
7
 
8
8
  # Column instance methods
@@ -41,6 +41,13 @@ shared_examples_for "Column class instance" do
41
41
  end
42
42
  end
43
43
 
44
+ class User < OpenStruct
45
+ extend ActiveModel::Naming
46
+ def id
47
+ @table[:id]
48
+ end
49
+ end
50
+
44
51
  RSpec.configure do |config|
45
52
  config.include(Webrat::Matchers)
46
53
  end
@@ -10,6 +10,11 @@ describe TableHelper::CallbackColumn do
10
10
  # call-proc
11
11
  let(:given_proc_without_attr) { lambda { |r| "aaa-#{r.id}" }}
12
12
  let(:given_proc_with_attr) { lambda { |r| "aaa-#{r}" }}
13
+ # user hash
14
+ let(:user_hash) { { :id => 12 } }
15
+ # call-proc
16
+ let(:given_proc_without_attr_for_hash) { lambda { |r| "aaa-#{r[:id]}" }}
17
+ let(:given_proc_with_attr_for_hash) { lambda { |r| "aaa-#{r}" }}
13
18
  # Instance methods
14
19
  describe "an instance" do
15
20
  it_should_behave_like "Column class instance"
@@ -28,15 +33,26 @@ describe TableHelper::CallbackColumn do
28
33
  col.title.should eq("Id")
29
34
  end
30
35
  end
31
-
32
36
  # :content_for
33
- it ":content_for method should success without given attribute" do
34
- col = build_column(klass, nil, :callback => given_proc_without_attr)
35
- col.content_for(user).should == given_proc_without_attr.call(user)
37
+ context "user object" do
38
+ it ":content_for method should success without given attribute" do
39
+ col = build_column(klass, nil, :callback => given_proc_without_attr)
40
+ col.content_for(user).should == "aaa-12"
41
+ end
42
+ it ":content_for method should success with given attribute" do
43
+ col = build_column(klass, :id, :callback => given_proc_with_attr)
44
+ col.content_for(user).should == "aaa-12"
45
+ end
36
46
  end
37
- it ":content_for method should success with given attribute" do
38
- col = build_column(klass, :id, :callback => given_proc_with_attr)
39
- col.content_for(user).should == given_proc_with_attr.call(user.id)
47
+ context "user hash" do
48
+ it ":content_for method should success without given attribute" do
49
+ col = build_column(klass, nil, :callback => given_proc_without_attr_for_hash)
50
+ col.content_for(user_hash).should == "aaa-12"
51
+ end
52
+ it ":content_for method should success with given attribute" do
53
+ col = build_column(klass, :id, :callback => given_proc_with_attr_for_hash)
54
+ col.content_for(user_hash).should == "aaa-12"
55
+ end
40
56
  end
41
57
  end
42
- end
58
+ end
@@ -12,6 +12,11 @@ describe TableHelper::Column do
12
12
  it ":title method should success" do
13
13
  build_column(klass, :id, :title => "Test").title.should eq("Test")
14
14
  end
15
+
16
+ it ":width method should success" do
17
+ build_column(klass, :id, :width => '20%').draw_title.should eq('<th width="20%">Id</th>')
18
+ end
19
+
15
20
  # :content_for
16
21
  it ":content_for method should raise error" do
17
22
  lambda do
@@ -6,7 +6,9 @@ describe TableHelper::SimpleColumn do
6
6
  # TableHelper::SimpleColumn
7
7
  let(:klass) { TableHelper::SimpleColumn }
8
8
  # user (stubbed data)
9
- let(:user) { mock(:id => 12) }
9
+ let(:user) { mock(:id => 12, :created_at => Time.gm(2011, "feb", 24, 14, 23, 1)) }
10
+ # user (hash)
11
+ let(:user_hash) { { :id => 12, :created_at => Time.gm(2011, "feb", 24, 14, 23, 1) } }
10
12
  # Instance methods
11
13
  describe "an instance" do
12
14
  it_should_behave_like "Column class instance"
@@ -24,7 +26,16 @@ describe TableHelper::SimpleColumn do
24
26
  # :content_for
25
27
  it ":content_for method should success" do
26
28
  col = build_column(klass, :id)
27
- col.content_for(user).should == user.id.to_s
29
+ col.content_for(user).should == "12"
30
+ col.content_for(user_hash).should == "12"
31
+ end
32
+
33
+ describe "format" do
34
+ it ":time_format should be displayed correctly" do
35
+ col = build_column(klass, :created_at, :time_format => '%Y-%m')
36
+ col.content_for(user).should == "2011-02"
37
+ col.content_for(user_hash).should == "2011-02"
38
+ end
28
39
  end
29
40
  end
30
41
  end
@@ -8,14 +8,14 @@ describe TableHelper::Table do
8
8
  # users list (stubbed data)
9
9
  let(:users) do
10
10
  [
11
- mock(:id => 1,
12
- :name => "John Jackson",
13
- :email => "john.jackson@example.com",
14
- :address => "100, Spear Street, LA, USA"),
15
- mock(:id => 2,
16
- :name => "Jack Johnson",
17
- :email => "jack.johnson@example.com",
18
- :address => "12, Brooklin, NY, USA"),
11
+ User.new(:id => 1,
12
+ :name => "John Jackson",
13
+ :email => "john.jackson@example.com",
14
+ :address => "100, Spear Street, LA, USA"),
15
+ User.new(:id => 2,
16
+ :name => "Jack Johnson",
17
+ :email => "jack.johnson@example.com",
18
+ :address => "12, Brooklin, NY, USA"),
19
19
  ]
20
20
  end
21
21
  # table instance
@@ -5,66 +5,174 @@ describe ActionView::Base do
5
5
  let(:template) do
6
6
  ActionView::Base.new
7
7
  end
8
- # users list (stubbed data)
9
- let(:users) do
10
- [
11
- mock({
12
- :id => 1209,
13
- :name => "John Smith",
14
- :email => "smith@matrix.net",
15
- :address => "100, Spear Street, NY, USA",
16
- }),
17
- mock({
18
- :id => 2123,
19
- :name => "Thomas Anderson",
20
- :email => "neo@matrix.net",
21
- :address => "200, Spear Street, NY, USA",
22
- }),
23
- mock({
24
- :id => 3323,
25
- :name => "Trinity",
26
- :email => "trinity@matrix.net",
27
- :address => "300, Spear Street, NY, USA",
28
- }),
29
- mock({
30
- :id => 4912,
31
- :name => "Morpheus",
32
- :email => "morpheus@matrix.net",
33
- :address => "400, Spear Street, NY, USA",
34
- })
35
- ]
36
- end
37
8
  # check if method available
38
9
  it "should respond to :table_for" do
39
10
  template.should respond_to(:table_for)
40
11
  end
41
-
42
- # main method
43
- describe ":table_for method" do
44
- # <%= table_for @users %>
45
- it "should raise if no block given" do
46
- lambda do
47
- template.table_for(users)
48
- end.should raise_error(ArgumentError)
12
+ # <%= table_for @users %>
13
+ it "should raise if no block given" do
14
+ lambda do
15
+ template.table_for([])
16
+ end.should raise_error(ArgumentError)
17
+ end
18
+ # users list (hashes array)
19
+ context "for hashes array" do
20
+ let(:users) do
21
+ [
22
+ { :hits => 87.0, :id => 1.0 },
23
+ { :hits => 86.0, :id => 5.0 },
24
+ { :hits => 78.0, :id => 6.0 },
25
+ { :hits => 78.0, :id => 3.0 },
26
+ ]
27
+ end
28
+ # <%= table_for @users, :html => { :id => "users", :class => "simple-table" } do %>
29
+ # <% column :name %>
30
+ # <% end %>
31
+ describe "with given :html options" do
32
+ before(:each) do
33
+ @html = template.table_for(users, :html => { :id => "users", :class => "simple-table" }) do
34
+ column :hits
35
+ end
36
+ end
37
+ it "should render specialized table" do
38
+ @html.should have_selector("table#users.simple-table")
39
+ end
40
+ end
41
+ # <%= table_for @users, :stripes => ["odd", "even"] do %>
42
+ # <% column :name %>
43
+ # <% end %>
44
+ describe "with cycling stripes" do
45
+ before(:each) do
46
+ @html = template.table_for(users, :stripes => %w{s-one s-two s-three}) do
47
+ column :name
48
+ end
49
+ end
50
+
51
+ it "should have valid classes" do
52
+ @html.should have_selector("tr.s-one", :count => 2)
53
+ @html.should have_selector("tr.s-two", :count => 1)
54
+ @html.should have_selector("tr.s-three", :count => 1)
55
+ end
56
+ end
57
+
58
+ # <%= table_for @users do %>
59
+ # <% columns :hits, :id %>
60
+ # <% end %>
61
+ describe "with columns" do
62
+ before(:each) do
63
+ @html = template.table_for(users) do
64
+ columns :hits, :id
65
+ end
66
+ end
67
+ it "should render valid HTML" do
68
+ @html.should have_selector("table") do |table|
69
+ table.should have_selector("thead/tr") do |tr|
70
+ ["Id", "Hits"].each do |field|
71
+ tr.should have_selector("th") do |th|
72
+ th.should contain(field)
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ describe "with named callback column" do
80
+ before(:each) do
81
+ @html = template.table_for(users) do
82
+ column :id do |id|
83
+ mail_to id
84
+ end
85
+ end
86
+ end
87
+ it "should render valid HTML" do
88
+ @html.should have_selector("table") do |table|
89
+ table.should have_selector("thead/tr/th") do |th|
90
+ th.should contain("Id")
91
+ end
92
+ table.should have_selector("tbody/tr") do |tr|
93
+ users.each do |user|
94
+ tr.should have_selector("td") do |td|
95
+ td.should have_selector("a[@href='mailto:#{user[:id]}']")
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
49
101
  end
50
102
  # <%= table_for @users do %>
103
+ # <% column :title => "User's name" do |user| %>
104
+ # <% content_tag :div do %>
105
+ # <% [user.first_name, user.last_name].join(" ") %>
106
+ # <% end %>
107
+ # <% end %>
108
+ # <% end %>
109
+ describe "with callback column" do
110
+ before(:each) do
111
+ @html = template.table_for(users) do
112
+ column :id
113
+ column :title => "HITS" do |user|
114
+ content_tag :div do
115
+ user[:hits].to_i.to_s
116
+ end
117
+ end
118
+ end
119
+ end
120
+ it "should render valid HTML" do
121
+ @html.should have_selector("table") do |table|
122
+ table.should have_selector("thead/tr/th") do |th|
123
+ th.should contain("HITS")
124
+ end
125
+ table.should have_selector("tbody/tr") do |tr|
126
+ users.each do |user|
127
+ tr.should have_selector("td") do |td|
128
+ td.should contain(user[:id].to_s)
129
+ end
130
+ tr.should have_selector("td/div") do |td|
131
+ td.should contain(user[:hits].to_i.to_s)
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ # <%= table_for @users, :stripes => ["odd", "even"], :html => { :tr => { :class => "table-row" } } do %>
51
139
  # <% column :name %>
52
140
  # <% end %>
53
- describe "with simple column" do
141
+ describe "with tr html" do
142
+ subject { template.table_for(users, :stripes => ["odd", "even"], :html => { :tr => { :class => "table-row", :id => lambda { |f| "user_#{f[:id].to_i}" } } }) do
143
+ column :hits
144
+ end
145
+ }
146
+ it "should have valid classes" do
147
+ should have_selector("tr.odd.table-row", :count => 2)
148
+ should have_selector("tr.even.table-row", :count => 2)
149
+ end
150
+ it "should have valid ids" do
151
+ users.each do |user|
152
+ should have_selector("tr.table-row#user_#{user[:id].to_i}") do |tr|
153
+ tr.should contain(user.hits.to_s)
154
+ end
155
+ end
156
+ end
157
+ end
158
+ # <%= table_for @users do %>
159
+ # <% column :name, :title => "User's name" %>
160
+ # <% end %>
161
+ describe "with titled column" do
54
162
  before(:each) do
55
163
  @html = template.table_for(users) do
56
- column :name
164
+ column :hits, :title => "user's hits"
57
165
  end
58
166
  end
59
167
  it "should render valid HTML" do
60
168
  @html.should have_selector("table") do |table|
61
169
  table.should have_selector("thead/tr/th") do |th|
62
- th.should contain("Name")
170
+ th.should contain("user's hits")
63
171
  end
64
172
  table.should have_selector("tbody/tr") do |tr|
65
173
  users.each do |user|
66
174
  tr.should have_selector("td") do |td|
67
- td.should contain(user.name)
175
+ td.should contain(user[:hits].to_s)
68
176
  end
69
177
  end
70
178
  end
@@ -72,49 +180,84 @@ describe ActionView::Base do
72
180
  end
73
181
  end
74
182
  # <%= table_for @users do %>
75
- # <% column :name, :html => { :width => "50%" } %>
183
+ # <% column :name %>
76
184
  # <% end %>
77
- describe "with column :html options" do
185
+ describe "with simple column" do
78
186
  before(:each) do
79
187
  @html = template.table_for(users) do
80
- column :id, :html => { :td => { :class => "user-id" }, :th => { :width => "50%" }}
188
+ column :hits
81
189
  end
82
190
  end
83
191
  it "should render valid HTML" do
84
192
  @html.should have_selector("table") do |table|
85
- table.should have_selector("thead/tr") do |tr|
86
- tr.should have_selector("th[@width='50%']")
193
+ table.should have_selector("thead/tr/th") do |th|
194
+ th.should contain("Hits")
87
195
  end
88
196
  table.should have_selector("tbody/tr") do |tr|
89
197
  users.each do |user|
90
- tr.should have_selector("td.user-id")
198
+ tr.should have_selector("td") do |td|
199
+ td.should contain(user[:hits].to_s)
200
+ end
91
201
  end
92
202
  end
93
203
  end
94
204
  end
95
205
  end
96
206
  # <%= table_for @users do %>
97
- # <% columns :name, :email, :address %>
207
+ # <% column :name, :html => { :width => "50%" } %>
98
208
  # <% end %>
99
- describe "with columns" do
209
+ describe "with column :html options" do
100
210
  before(:each) do
101
211
  @html = template.table_for(users) do
102
- columns :id, :name, :email, :address
212
+ column :id, :html => { :td => { :class => "user-id" }, :th => { :width => "50%" }}
103
213
  end
104
214
  end
105
215
  it "should render valid HTML" do
106
216
  @html.should have_selector("table") do |table|
107
217
  table.should have_selector("thead/tr") do |tr|
108
- ["Id", "Name", "Email", "Address"].each do |field|
109
- tr.should have_selector("th") do |th|
110
- th.should contain(field)
218
+ tr.should have_selector("th[@width='50%']")
219
+ end
220
+ table.should have_selector("tbody/tr") do |tr|
221
+ users.each do |user|
222
+ tr.should have_selector("td.user-id") do |td|
223
+ td.should contain(user[:id].to_s)
111
224
  end
112
225
  end
113
226
  end
114
227
  end
115
228
  end
116
229
  end
117
-
230
+ end
231
+ # users list (objects array)
232
+ context "for objects array" do
233
+ let(:users) do
234
+ [
235
+ User.new({
236
+ :id => 1209,
237
+ :name => "John Smith",
238
+ :email => "smith@matrix.net",
239
+ :address => "100, Spear Street, NY, USA"
240
+ }),
241
+ User.new({
242
+ :id => 2123,
243
+ :name => "Thomas Anderson",
244
+ :email => "neo@matrix.net",
245
+ :address => "200, Spear Street, NY, USA"
246
+ }),
247
+ User.new({
248
+ :id => 3323,
249
+ :name => "Trinity",
250
+ :email => "trinity@matrix.net",
251
+ :address => "300, Spear Street, NY, USA"
252
+ }),
253
+ User.new({
254
+ :id => 4912,
255
+ :name => "Morpheus",
256
+ :email => "morpheus@matrix.net",
257
+ :address => "400, Spear Street, NY, USA"
258
+ })
259
+ ]
260
+ end
118
261
  # <%= table_for @users, :html => { :id => "users", :class => "simple-table" } do %>
119
262
  # <% column :name %>
120
263
  # <% end %>
@@ -128,30 +271,68 @@ describe ActionView::Base do
128
271
  @html.should have_selector("table#users.simple-table")
129
272
  end
130
273
  end
274
+ # <%= table_for @users, :stripes => ["odd", "even"] do %>
275
+ # <% column :name %>
276
+ # <% end %>
277
+ describe "with cycling stripes" do
278
+ before(:each) do
279
+ @html = template.table_for(users, :stripes => %w{s-one s-two s-three}) do
280
+ column :name
281
+ end
282
+ end
283
+
284
+ it "should have valid classes" do
285
+ @html.should have_selector("tr.s-one", :count => 2)
286
+ @html.should have_selector("tr.s-two", :count => 1)
287
+ @html.should have_selector("tr.s-three", :count => 1)
288
+ end
289
+ end
290
+
131
291
  # <%= table_for @users do %>
132
- # <% column :name, :title => "User's name" %>
292
+ # <% columns :name, :email, :address %>
133
293
  # <% end %>
134
- describe "with titled column" do
294
+ describe "with columns" do
135
295
  before(:each) do
136
296
  @html = template.table_for(users) do
137
- column :email, :title => "Email address"
297
+ columns :id, :name, :email, :address
298
+ end
299
+ end
300
+ it "should render valid HTML" do
301
+ @html.should have_selector("table") do |table|
302
+ table.should have_selector("thead/tr") do |tr|
303
+ ["Id", "Name", "Email", "Address"].each do |field|
304
+ tr.should have_selector("th") do |th|
305
+ th.should contain(field)
306
+ end
307
+ end
308
+ end
309
+ end
310
+ end
311
+ end
312
+ describe "with named callback column" do
313
+ before(:each) do
314
+ @html = template.table_for(users) do
315
+ column :email do |email|
316
+ mail_to email
317
+ end
138
318
  end
139
319
  end
140
320
  it "should render valid HTML" do
141
321
  @html.should have_selector("table") do |table|
142
322
  table.should have_selector("thead/tr/th") do |th|
143
- th.should contain("Email address")
323
+ th.should contain("Email")
144
324
  end
145
325
  table.should have_selector("tbody/tr") do |tr|
146
326
  users.each do |user|
147
327
  tr.should have_selector("td") do |td|
148
- td.should contain(user.email)
328
+ td.should have_selector("a[@href='mailto:#{user.email}']")
149
329
  end
150
330
  end
151
331
  end
152
332
  end
153
333
  end
154
334
  end
335
+
155
336
  # <%= table_for @users do %>
156
337
  # <% column :title => "User's name" do |user| %>
157
338
  # <% content_tag :div do %>
@@ -188,62 +369,96 @@ describe ActionView::Base do
188
369
  end
189
370
  end
190
371
  end
191
-
192
- describe "with named callback column" do
372
+ # <%= table_for @users, :stripes => ["odd", "even"], :html => { :tr => { :class => "table-row" } } do %>
373
+ # <% column :name %>
374
+ # <% end %>
375
+ describe "with tr html" do
376
+ subject { template.table_for(users, :stripes => ["odd", "even"], :html => { :tr => { :class => "table-row", :id => lambda { |f| "user_#{f.id}" } } }) do
377
+ column :name
378
+ end
379
+ }
380
+ it "should have valid classes" do
381
+ should have_selector("tr.odd.table-row", :count => 2)
382
+ should have_selector("tr.even.table-row", :count => 2)
383
+ end
384
+ it "should have valid ids" do
385
+ users.each do |user|
386
+ should have_selector("tr.table-row#user_#{user.id}") do |tr|
387
+ tr.should contain(user.name)
388
+ end
389
+ end
390
+ end
391
+ end
392
+ # <%= table_for @users do %>
393
+ # <% column :name, :title => "User's name" %>
394
+ # <% end %>
395
+ describe "with titled column" do
193
396
  before(:each) do
194
397
  @html = template.table_for(users) do
195
- column :email do |email|
196
- mail_to email
197
- end
398
+ column :email, :title => "Email address"
198
399
  end
199
400
  end
200
-
201
401
  it "should render valid HTML" do
202
402
  @html.should have_selector("table") do |table|
203
403
  table.should have_selector("thead/tr/th") do |th|
204
- th.should contain("Email")
404
+ th.should contain("Email address")
205
405
  end
206
406
  table.should have_selector("tbody/tr") do |tr|
207
407
  users.each do |user|
208
408
  tr.should have_selector("td") do |td|
209
- td.should have_selector("a[@href='mailto:#{user.email}']")
409
+ td.should contain(user.email)
210
410
  end
211
411
  end
212
412
  end
213
413
  end
214
414
  end
215
415
  end
216
-
217
- # <%= table_for @users, :stripes => ["odd", "even"] do %>
416
+ # <%= table_for @users do %>
218
417
  # <% column :name %>
219
418
  # <% end %>
220
- describe "with cycling stripes" do
419
+ describe "with simple column" do
221
420
  before(:each) do
222
- @html = template.table_for(users, :stripes => %w{s-one s-two s-three}) do
421
+ @html = template.table_for(users) do
223
422
  column :name
224
423
  end
225
424
  end
226
-
227
- it "should have valid classes" do
228
- @html.should have_selector("tr.s-one", :count => 2)
229
- @html.should have_selector("tr.s-two", :count => 1)
230
- @html.should have_selector("tr.s-three", :count => 1)
425
+ it "should render valid HTML" do
426
+ @html.should have_selector("table") do |table|
427
+ table.should have_selector("thead/tr/th") do |th|
428
+ th.should contain("Name")
429
+ end
430
+ table.should have_selector("tbody/tr") do |tr|
431
+ users.each do |user|
432
+ tr.should have_selector("td") do |td|
433
+ td.should contain(user.name)
434
+ end
435
+ end
436
+ end
437
+ end
231
438
  end
232
439
  end
233
-
234
- # <%= table_for @users, :stripes => ["odd", "even"], :html => { :tr => { :class => "table-row" } } do %>
235
- # <% column :name %>
440
+ # <%= table_for @users do %>
441
+ # <% column :name, :html => { :width => "50%" } %>
236
442
  # <% end %>
237
- describe "with tr html" do
443
+ describe "with column :html options" do
238
444
  before(:each) do
239
- @html = template.table_for(users, :stripes => ["odd", "even"], :html => { :tr => { :class => "table-row" } }) do
240
- column :name
445
+ @html = template.table_for(users) do
446
+ column :id, :html => { :td => { :class => "user-id" }, :th => { :width => "50%" }}
241
447
  end
242
448
  end
243
-
244
- it "should have valid classes" do
245
- @html.should have_selector("tr.odd.table-row", :count => 2)
246
- @html.should have_selector("tr.even.table-row", :count => 2)
449
+ it "should render valid HTML" do
450
+ @html.should have_selector("table") do |table|
451
+ table.should have_selector("thead/tr") do |tr|
452
+ tr.should have_selector("th[@width='50%']")
453
+ end
454
+ table.should have_selector("tbody/tr") do |tr|
455
+ users.each do |user|
456
+ tr.should have_selector("td.user-id") do |td|
457
+ td.should contain(user.id.to_s)
458
+ end
459
+ end
460
+ end
461
+ end
247
462
  end
248
463
  end
249
464
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_for_collection
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 31
4
5
  prerelease: false
5
6
  segments:
6
7
  - 1
7
8
  - 0
8
- - 3
9
- version: 1.0.3
9
+ - 4
10
+ version: 1.0.4
10
11
  platform: ruby
11
12
  authors:
12
13
  - Dima Lunich
@@ -16,7 +17,7 @@ autorequire:
16
17
  bindir: bin
17
18
  cert_chain: []
18
19
 
19
- date: 2010-11-16 00:00:00 +02:00
20
+ date: 2011-11-16 00:00:00 +02:00
20
21
  default_executable:
21
22
  dependencies:
22
23
  - !ruby/object:Gem::Dependency
@@ -27,6 +28,7 @@ dependencies:
27
28
  requirements:
28
29
  - - ">="
29
30
  - !ruby/object:Gem::Version
31
+ hash: 15
30
32
  segments:
31
33
  - 2
32
34
  - 0
@@ -42,6 +44,7 @@ dependencies:
42
44
  requirements:
43
45
  - - ">="
44
46
  - !ruby/object:Gem::Version
47
+ hash: 3
45
48
  segments:
46
49
  - 0
47
50
  version: "0"
@@ -94,6 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
97
  requirements:
95
98
  - - ">="
96
99
  - !ruby/object:Gem::Version
100
+ hash: 3
97
101
  segments:
98
102
  - 0
99
103
  version: "0"
@@ -102,6 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
106
  requirements:
103
107
  - - ">="
104
108
  - !ruby/object:Gem::Version
109
+ hash: 3
105
110
  segments:
106
111
  - 0
107
112
  version: "0"
@@ -111,7 +116,7 @@ rubyforge_project: ""
111
116
  rubygems_version: 1.3.7
112
117
  signing_key:
113
118
  specification_version: 3
114
- summary: table_for_collection-1.0.3
119
+ summary: table_for_collection-1.0.4
115
120
  test_files:
116
121
  - spec/core_ex/array_spec.rb
117
122
  - spec/spec_helper.rb