table_for_collection 1.0.4 → 1.0.5

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
@@ -48,13 +48,16 @@ This ruby code will produce following HTML:
48
48
  <% column :name, :html => { :th => { :width => "25%" }, :td => { :class => "user-name" }} %>
49
49
  <% column :email, :width => "20%" %>
50
50
  <% column :address, :title => "Home" %>
51
+ <% column :created_at, :title_callback => lambda { |title| link_to(title, users_path(params.merge(:order => "desc"))) } %>
51
52
  <% end %>
52
53
 
53
54
  You can put :html hash to the options list (inside the :column method too). In this case
54
55
  table will be created with given html attributes. To assign :tr options use :tr key under :html - :class value will be added
55
56
  to the classes list, :id can be instance of the Proc class, in this it will be called for each collection element.
56
57
  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:
58
+ Also :title value in the :column method will be used as column's title. You can pass Proc object to the :title_callback param.
59
+ In this case given proc will be called with title value.
60
+ It will produce HTML similar to:
58
61
 
59
62
  <table class="simple-table" id="users" width="100%">
60
63
  <thead>
@@ -62,6 +65,7 @@ Also :title value in the :column method will be used as column's title. It will
62
65
  <th width="25%">Name</th>
63
66
  <th width="20%">Email</th>
64
67
  <th>Home</th>
68
+ <th><a href="/users?order=desc">Created At</a></th>
65
69
  </tr>
66
70
  </thead>
67
71
  <tbody>
@@ -69,6 +73,7 @@ Also :title value in the :column method will be used as column's title. It will
69
73
  <td class="user-name">...</td>
70
74
  <td>...</td>
71
75
  <td>...</td>
76
+ <td>...</td>
72
77
  </tr>
73
78
  <tr class="simple-row" id="user-row-14">
74
79
  ...
@@ -17,9 +17,11 @@ module TableHelper
17
17
  else
18
18
  @attr.to_s.capitalize
19
19
  end
20
+
21
+ @title_callback = @options.delete(:title_callback)
20
22
 
21
23
  @html = @options.delete(:html) || {}
22
- @html.merge!({:th => { :width => @options.delete(:width) }}) if @options[:width]
24
+ @html.merge!({:th => { :width => @options.delete(:width) }}) if @options.has_key?(:width)
23
25
  end
24
26
 
25
27
  def content_for
@@ -28,7 +30,7 @@ module TableHelper
28
30
 
29
31
  def draw_title
30
32
  content_tag :th, @html[:th] do
31
- @title
33
+ @title_callback.is_a?(Proc) ? @title_callback.call(@title) : @title
32
34
  end
33
35
  end
34
36
  end
@@ -1,4 +1,4 @@
1
1
  module TableHelper
2
2
  GEM_NAME = "table_for_collection"
3
- VERSION = "1.0.4"
3
+ VERSION = "1.0.5"
4
4
  end
@@ -16,12 +16,16 @@ describe TableHelper::Column do
16
16
  it ":width method should success" do
17
17
  build_column(klass, :id, :width => '20%').draw_title.should eq('<th width="20%">Id</th>')
18
18
  end
19
+
20
+ it "should success with :title_callback option" do
21
+ build_column(klass, :id, :title_callback => lambda { |t| template.link_to(t, "#") }).draw_title.should eq(%{<th><a href="#">Id</a></th>})
22
+ end
19
23
 
20
24
  # :content_for
21
25
  it ":content_for method should raise error" do
22
- lambda do
26
+ expect {
23
27
  build_column(klass, :id, :title => "Test").content_for
24
- end.should raise_error(NoMethodError, "Use SimpleColumn or CallbackColumn")
28
+ }.to raise_error(NoMethodError, "Use SimpleColumn or CallbackColumn")
25
29
  end
26
30
  end
27
31
  end
@@ -180,6 +180,30 @@ describe ActionView::Base do
180
180
  end
181
181
  end
182
182
  # <%= table_for @users do %>
183
+ # <% column :name, :title => "User's name", :title_callback => lambda { |t| link_to(t, "#")} %>
184
+ # <% end %>
185
+ describe "with callback-titled column" do
186
+ before(:each) do
187
+ @html = template.table_for(users) do
188
+ column :hits, :title => "user's hits", :title_callback => lambda { |t| link_to(t, "/users/sort/id")}
189
+ end
190
+ end
191
+ it "should render valid HTML" do
192
+ @html.should have_selector("table") do |table|
193
+ table.should have_selector("thead/tr/th") do |th|
194
+ th.should have_selector("a[@href='/users/sort/id']", :content => "user's hits")
195
+ end
196
+ table.should have_selector("tbody/tr") do |tr|
197
+ users.each do |user|
198
+ tr.should have_selector("td") do |td|
199
+ td.should contain(user[:hits].to_s)
200
+ end
201
+ end
202
+ end
203
+ end
204
+ end
205
+ end
206
+ # <%= table_for @users do %>
183
207
  # <% column :name %>
184
208
  # <% end %>
185
209
  describe "with simple column" do
@@ -414,6 +438,30 @@ describe ActionView::Base do
414
438
  end
415
439
  end
416
440
  # <%= table_for @users do %>
441
+ # <% column :name, :title => "User's name", :title_callback => lambda { |t| link_to(t, "#")} %>
442
+ # <% end %>
443
+ describe "with callback-titled column" do
444
+ before(:each) do
445
+ @html = template.table_for(users) do
446
+ column :email, :title_callback => lambda { |t| link_to(t, "/users/sort/email")}
447
+ end
448
+ end
449
+ it "should render valid HTML" do
450
+ @html.should have_selector("table") do |table|
451
+ table.should have_selector("thead/tr/th") do |th|
452
+ th.should have_selector("a[@href='/users/sort/email']", :content => "Email")
453
+ end
454
+ table.should have_selector("tbody/tr") do |tr|
455
+ users.each do |user|
456
+ tr.should have_selector("td") do |td|
457
+ td.should contain(user.email)
458
+ end
459
+ end
460
+ end
461
+ end
462
+ end
463
+ end
464
+ # <%= table_for @users do %>
417
465
  # <% column :name %>
418
466
  # <% end %>
419
467
  describe "with simple column" do
metadata CHANGED
@@ -1,13 +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
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 4
10
- version: 1.0.4
9
+ - 5
10
+ version: 1.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dima Lunich
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-11-16 00:00:00 +02:00
20
+ date: 2011-11-17 00:00:00 +02:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
@@ -116,7 +116,7 @@ rubyforge_project: ""
116
116
  rubygems_version: 1.3.7
117
117
  signing_key:
118
118
  specification_version: 3
119
- summary: table_for_collection-1.0.4
119
+ summary: table_for_collection-1.0.5
120
120
  test_files:
121
121
  - spec/core_ex/array_spec.rb
122
122
  - spec/spec_helper.rb