table_for_collection 1.0.2 → 1.0.3

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,7 +1,13 @@
1
- == v.0.2
1
+ == v.1.0.3
2
+
3
+ Callback column for given attribute
4
+ Compatibility with rails 3.0.2
5
+ Change dependency from rails to actionpack
6
+
7
+ == v.1.0.2
2
8
 
3
9
  Packaged as a gem.
4
10
 
5
- == v.0.1
11
+ == v.1.0.1
6
12
 
7
13
  Initial version: table_for method added.
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source :gemcutter
2
2
 
3
- gem "rails"
3
+ gem "actionpack"
4
4
 
5
5
  group :development, :test do
6
6
  gem "rspec", ">=2.0.0"
@@ -68,10 +68,13 @@ will be used as column's title. It will produce HTML similar to:
68
68
 
69
69
  <%= table_for @users do -%>
70
70
  <% column :login, :title => "User name" %>
71
- <% column :email %>
71
+ <% column :email do |email| %>
72
+ <% mail_to email %>
73
+ <% end %>
72
74
  <% column :title => "Full name" do |user| %>
73
75
  <% [user.first_name, user.last_name].join(" ") %>
74
76
  <% end %>
77
+ <% column :company %>
75
78
  <% column :title => "Actions" do |user| %>
76
79
  <% [link_to("Show", user), link_to("Delete", user, :method => :delete)].join(" | ") %>
77
80
  <% end %>
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
1
  require "rspec/core/rake_task"
2
2
 
3
- desc "Test the table_for plugin."
3
+ desc "Test table_for_collection plugin."
4
4
  RSpec::Core::RakeTask.new('spec')
5
5
 
@@ -2,10 +2,11 @@ module TableHelper
2
2
  class CallbackColumn < Column # :nodoc:
3
3
  def initialize(template, obj, ops)
4
4
  super
5
- @title = @options.delete(:title) || "&nbsp;"
5
+ @callback = @options.delete(:callback)
6
6
  end
7
+
7
8
  def content_for(record)
8
- @attr.call(record)
9
+ @attr ? @callback.call(record.send(@attr).to_s) : @callback.call(record)
9
10
  end
10
11
  end
11
12
  end
@@ -1,13 +1,22 @@
1
1
  module TableHelper
2
2
  class Column # :nodoc:
3
- attr_reader :title, :html, :html
3
+ attr_reader :title, :html
4
+ delegate :content_tag, :to => :@template
5
+
4
6
  def initialize(template, obj, ops)
5
- @template, @options, @attr, @title = template, ops, obj, ""
6
- @html = @options.delete(:html) || {}
7
+ @template, @attr, @options = template, obj, ops
8
+ @title = @options.delete(:title) || @attr.to_s.humanize.presence || "&nbsp;"
9
+ @html = @options.delete(:html) || {}
7
10
  end
8
-
11
+
9
12
  def content_for
10
13
  raise NoMethodError, "Use SimpleColumn or CallbackColumn"
11
14
  end
15
+
16
+ def draw_title
17
+ content_tag :th, @html[:th] do
18
+ @title
19
+ end
20
+ end
12
21
  end
13
22
  end
@@ -37,6 +37,6 @@ module TableHelper
37
37
  end
38
38
  end
39
39
 
40
- ActionView::Base.class_eval {
40
+ ActionView::Base.class_eval do
41
41
  include TableHelper
42
- }
42
+ end
@@ -1,11 +1,7 @@
1
1
  module TableHelper
2
2
  class SimpleColumn < Column # :nodoc:
3
- def initialize(template, obj, ops)
4
- super
5
- @title = @options.delete(:title) || @attr.to_s.humanize || "&nbsp;"
6
- end
7
3
  def content_for(record)
8
- record.send(@attr)
4
+ record.send(@attr).to_s
9
5
  end
10
6
  end
11
7
  end
@@ -3,7 +3,7 @@ require "core_ex/array"
3
3
  module TableHelper
4
4
  class Table # :nodoc:
5
5
  delegate :content_tag, :to => :@template
6
-
6
+
7
7
  def initialize(template, records, options = {})
8
8
  @template, @records, @columns = template, records, []
9
9
  # table's html options
@@ -16,27 +16,32 @@ module TableHelper
16
16
  end
17
17
 
18
18
  def columns(*args)
19
+ res = []
19
20
  unless args.blank?
20
21
  args.each do |arg|
21
- self.column(arg)
22
+ res << self.column(arg)
22
23
  end
23
24
  else
24
25
  raise ArgumentError, "At least one attribute name should be given"
25
26
  end
27
+ res
26
28
  end
27
29
 
28
30
  def column(*args, &block)
29
31
  col_options = args.extract_options!
30
- unless args.blank?
31
- attr = args.shift
32
- @columns << SimpleColumn.new(@template, attr, col_options)
32
+ res = nil
33
+
34
+ attr = args.shift or nil
35
+
36
+ if block_given?
37
+ col_options[:callback] = block
38
+ @columns << (res = CallbackColumn.new(@template, attr, col_options))
39
+ elsif attr
40
+ @columns << (res = SimpleColumn.new(@template, attr, col_options))
33
41
  else
34
- if block_given?
35
- @columns << CallbackColumn.new(@template, block, col_options)
36
- else
37
- raise ArgumentError, "Attribute name or block should be given"
38
- end
42
+ raise ArgumentError, "Attribute name or block should be given"
39
43
  end
44
+ res
40
45
  end
41
46
 
42
47
  def draw
@@ -56,11 +61,7 @@ module TableHelper
56
61
  def head
57
62
  content_tag :thead do
58
63
  content_tag :tr do
59
- @columns.map do |col|
60
- content_tag :th, col.html[:th] do
61
- col.title
62
- end
63
- end.join
64
+ @columns.map { |col| col.draw_title }.join.html_safe
64
65
  end
65
66
  end
66
67
  end
@@ -73,9 +74,9 @@ module TableHelper
73
74
  content_tag :td, col.html[:td] do
74
75
  col.content_for(rec).to_s
75
76
  end
76
- end.join
77
+ end.join.html_safe
77
78
  end
78
- end.join
79
+ end.join.html_safe
79
80
  end
80
81
  end
81
82
 
@@ -1,4 +1,4 @@
1
1
  module TableHelper
2
2
  GEM_NAME = "table_for_collection"
3
- VERSION = "1.0.2"
3
+ VERSION = "1.0.3"
4
4
  end
@@ -1,6 +1,46 @@
1
1
  require "table_for_collection"
2
2
  require "webrat"
3
3
 
4
+ def build_column(klass, arg, options = {})
5
+ klass.new(template, arg, options)
6
+ end
7
+
8
+ # Column instance methods
9
+ shared_examples_for "Column class instance" do
10
+ # Check are methods present
11
+ describe "should have method", :shared => true do
12
+ [:title, :html, :content_for, :content_tag].each do |method|
13
+ it ":#{method}" do
14
+ build_column(klass, :id).should respond_to(method)
15
+ end
16
+ end
17
+ end
18
+ # :html
19
+ describe ":html method" do
20
+ it "should success if :html option is empty" do
21
+ build_column(klass, :id).html.should eq({})
22
+ end
23
+ it "should success if :html option is given" do
24
+ build_column(klass, :id, :html => { :class => "some-class" }).html.should eq(:class => "some-class")
25
+ end
26
+ end
27
+ # :draw_title
28
+ describe ":draw_title method should render" do
29
+ it "valid th" do
30
+ col = build_column(klass, :id, :title => "Test", :html => {
31
+ :th => {
32
+ :class => "some-class",
33
+ :width => "50%",
34
+ }
35
+ })
36
+ html = col.draw_title
37
+ html.should have_selector("th.some-class[@width='50%']") do |th|
38
+ th.should contain(col.title)
39
+ end
40
+ end
41
+ end
42
+ end
43
+
4
44
  RSpec.configure do |config|
5
45
  config.include(Webrat::Matchers)
6
46
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe TableHelper::CallbackColumn do
4
+ # ActionView::Base instance
5
+ let(:template) { ActionView::Base.new }
6
+ # TableHelper::SimpleColumn
7
+ let(:klass) { TableHelper::CallbackColumn }
8
+ # user (stubbed data)
9
+ let(:user) { mock(:id => 12) }
10
+ # call-proc
11
+ let(:given_proc_without_attr) { lambda { |r| "aaa-#{r.id}" }}
12
+ let(:given_proc_with_attr) { lambda { |r| "aaa-#{r}" }}
13
+ # Instance methods
14
+ describe "an instance" do
15
+ it_should_behave_like "Column class instance"
16
+ # :title
17
+ describe ":title method should success" do
18
+ it "if :title option is empty" do
19
+ col = build_column(klass, nil, :callback => given_proc_without_attr)
20
+ col.title.should eq("&nbsp;")
21
+ end
22
+ it "if :title options is set" do
23
+ col = build_column(klass, nil, :callback => given_proc_without_attr, :title => "Some title")
24
+ col.title.should eq("Some title")
25
+ end
26
+ it "if :title options is empty, but attribute is set" do
27
+ col = build_column(klass, :id, :callback => given_proc_with_attr)
28
+ col.title.should eq("Id")
29
+ end
30
+ end
31
+
32
+ # :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)
36
+ 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)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe TableHelper::Column do
4
+ # ActionView::Base instance
5
+ let(:template) { ActionView::Base.new }
6
+ # TableHelper::Column
7
+ let(:klass) { TableHelper::Column }
8
+ # Instance methods
9
+ describe "an instance" do
10
+ it_should_behave_like "Column class instance"
11
+ # :title
12
+ it ":title method should success" do
13
+ build_column(klass, :id, :title => "Test").title.should eq("Test")
14
+ end
15
+ # :content_for
16
+ it ":content_for method should raise error" do
17
+ lambda do
18
+ build_column(klass, :id, :title => "Test").content_for
19
+ end.should raise_error(NoMethodError, "Use SimpleColumn or CallbackColumn")
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe TableHelper::SimpleColumn do
4
+ # ActionView::Base instance
5
+ let(:template) { ActionView::Base.new }
6
+ # TableHelper::SimpleColumn
7
+ let(:klass) { TableHelper::SimpleColumn }
8
+ # user (stubbed data)
9
+ let(:user) { mock(:id => 12) }
10
+ # Instance methods
11
+ describe "an instance" do
12
+ it_should_behave_like "Column class instance"
13
+ # :title
14
+ describe ":title method should success" do
15
+ it "if :title option is empty" do
16
+ col = build_column(klass, attr = :id)
17
+ col.title.should eq(attr.to_s.humanize)
18
+ end
19
+ it "if :title options is set" do
20
+ col = build_column(klass, :id, :title => "Some title")
21
+ col.title.should eq("Some title")
22
+ end
23
+ end
24
+ # :content_for
25
+ it ":content_for method should success" do
26
+ col = build_column(klass, :id)
27
+ col.content_for(user).should == user.id.to_s
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,156 @@
1
+ require 'spec_helper'
2
+
3
+ describe TableHelper::Table do
4
+ # ActionView::Base instance
5
+ let(:template) do
6
+ ActionView::Base.new
7
+ end
8
+ # users list (stubbed data)
9
+ let(:users) do
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"),
19
+ ]
20
+ end
21
+ # table instance
22
+ let(:table) do
23
+ TableHelper::Table.new(template, users)
24
+ end
25
+ # Instance methods
26
+ describe "An instance" do
27
+ # check are methods available
28
+ describe "should have method" do
29
+ [:columns, :column, :draw, :content_tag].each do |method|
30
+ it "#{method}" do
31
+ table.should respond_to(method)
32
+ end
33
+ end
34
+ end
35
+ # :column method
36
+ describe ":column method" do
37
+ describe "with argument only" do
38
+ it "should build simple column" do
39
+ col = table.column(:id)
40
+ last = table.instance_variable_get(:@columns).last
41
+ last.should be_instance_of(TableHelper::SimpleColumn)
42
+ last.should eq(col)
43
+ end
44
+ it "should build column" do
45
+ lambda do
46
+ table.column(:id)
47
+ end.should change(table.instance_variable_get(:@columns), :size).by(1)
48
+ end
49
+ end
50
+ describe "with argument and options" do
51
+ it "should build simple column" do
52
+ col = table.column(:id, :title => "ID")
53
+ last = table.instance_variable_get(:@columns).last
54
+ last.should be_instance_of(TableHelper::SimpleColumn)
55
+ last.should eq(col)
56
+ end
57
+ it "should build column" do
58
+ lambda do
59
+ table.column(:id, :title => "ID")
60
+ end.should change(table.instance_variable_get(:@columns), :size).by(1)
61
+ end
62
+ end
63
+ describe "with block only" do
64
+ it "should build callback column" do
65
+ col = table.column { |r| "aaa-#{r}" }
66
+ last = table.instance_variable_get(:@columns).last
67
+ last.should be_instance_of(TableHelper::CallbackColumn)
68
+ last.should eq(col)
69
+ end
70
+ it "should build column" do
71
+ lambda do
72
+ table.column { |r| "aaa-#{r}" }
73
+ end.should change(table.instance_variable_get(:@columns), :size).by(1)
74
+ end
75
+ end
76
+ describe "with block and options" do
77
+ it "should build callback column" do
78
+ col = table.column(:title => "AAA") { |r| "aaa-#{r}" }
79
+ last = table.instance_variable_get(:@columns).last
80
+ last.should be_instance_of(TableHelper::CallbackColumn)
81
+ last.should eq(col)
82
+ end
83
+ it "should build column" do
84
+ lambda do
85
+ table.column(:title => "AAA") { |r| "aaa-#{r}" }
86
+ end.should change(table.instance_variable_get(:@columns), :size).by(1)
87
+ end
88
+ end
89
+ describe "without block and argument" do
90
+ it "should raise error" do
91
+ lambda do
92
+ table.column
93
+ end.should raise_error(ArgumentError, "Attribute name or block should be given")
94
+ end
95
+ end
96
+ describe "with options only" do
97
+ it "should raise error" do
98
+ lambda do
99
+ table.column(:title => "Title")
100
+ end.should raise_error(ArgumentError, "Attribute name or block should be given")
101
+ end
102
+ end
103
+ end
104
+ # :columns method
105
+ describe ":columns method" do
106
+ it "should create columns by attributes" do
107
+ attrs = [:id, :name, :email]
108
+ res = nil
109
+ lambda do
110
+ res = table.columns *attrs
111
+ end.should change(table.instance_variable_get(:@columns), :size).by(attrs.size)
112
+ res.should be_instance_of(Array)
113
+ res.size.should eq(attrs.size)
114
+ end
115
+ it "should raise if no arguments given" do
116
+ lambda do
117
+ table.columns
118
+ end.should raise_error(ArgumentError, "At least one attribute name should be given")
119
+ end
120
+ end
121
+ # :draw method
122
+ describe ":draw method" do
123
+ describe "for simple table" do
124
+ before(:each) do
125
+ @attr = :id
126
+ table = build_table
127
+ @col = table.column(@attr)
128
+ @html = table.draw
129
+ end
130
+ describe "should render table" do
131
+ it { @html.should have_selector("table") }
132
+ describe "thead" do
133
+ it { @html.should have_selector("table/thead") }
134
+ describe "tr" do
135
+ it { @html.should have_selector("table/thead/tr") }
136
+ describe "th" do
137
+ it "should contain valid title" do
138
+ @html.should have_selector("table/thead/tr/th") do |th|
139
+ th.should contain(@col.title)
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
145
+ describe "tbody" do
146
+ it { @html.should have_selector("table/tbody") }
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
152
+ protected
153
+ def build_table(options = {})
154
+ TableHelper::Table.new(template, users, options)
155
+ end
156
+ end
@@ -1,4 +1,4 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
3
  describe ActionView::Base do
4
4
  # ActionView::Base instance
@@ -189,6 +189,31 @@ describe ActionView::Base do
189
189
  end
190
190
  end
191
191
 
192
+ describe "with named callback column" do
193
+ before(:each) do
194
+ @html = template.table_for(users) do
195
+ column :email do |email|
196
+ mail_to email
197
+ end
198
+ end
199
+ end
200
+
201
+ it "should render valid HTML" do
202
+ @html.should have_selector("table") do |table|
203
+ table.should have_selector("thead/tr/th") do |th|
204
+ th.should contain("Email")
205
+ end
206
+ table.should have_selector("tbody/tr") do |tr|
207
+ users.each do |user|
208
+ tr.should have_selector("td") do |td|
209
+ td.should have_selector("a[@href='mailto:#{user.email}']")
210
+ end
211
+ end
212
+ end
213
+ end
214
+ end
215
+ end
216
+
192
217
  # <%= table_for @users, :stripes => ["odd", "even"] do %>
193
218
  # <% column :name %>
194
219
  # <% end %>
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_for_collection
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
4
  prerelease: false
6
5
  segments:
7
6
  - 1
8
7
  - 0
9
- - 2
10
- version: 1.0.2
8
+ - 3
9
+ version: 1.0.3
11
10
  platform: ruby
12
11
  authors:
13
12
  - Dima Lunich
@@ -17,7 +16,7 @@ autorequire:
17
16
  bindir: bin
18
17
  cert_chain: []
19
18
 
20
- date: 2010-10-24 00:00:00 +03:00
19
+ date: 2010-11-16 00:00:00 +02:00
21
20
  default_executable:
22
21
  dependencies:
23
22
  - !ruby/object:Gem::Dependency
@@ -28,21 +27,21 @@ dependencies:
28
27
  requirements:
29
28
  - - ">="
30
29
  - !ruby/object:Gem::Version
31
- hash: 3
32
30
  segments:
31
+ - 2
33
32
  - 0
34
- version: "0"
33
+ - 0
34
+ version: 2.0.0
35
35
  type: :development
36
36
  version_requirements: *id001
37
37
  - !ruby/object:Gem::Dependency
38
- name: rails
38
+ name: actionpack
39
39
  prerelease: false
40
40
  requirement: &id002 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
43
  - - ">="
44
44
  - !ruby/object:Gem::Version
45
- hash: 3
46
45
  segments:
47
46
  - 0
48
47
  version: "0"
@@ -70,7 +69,10 @@ files:
70
69
  - lib/table_for_collection.rb
71
70
  - spec/core_ex/array_spec.rb
72
71
  - spec/spec_helper.rb
73
- - spec/table_for_collection/table_spec.rb
72
+ - spec/table_for/callback_column_spec.rb
73
+ - spec/table_for/column_spec.rb
74
+ - spec/table_for/simple_column_spec.rb
75
+ - spec/table_for/table_spec.rb
74
76
  - spec/table_for_collection_spec.rb
75
77
  - README.rdoc
76
78
  - Rakefile
@@ -92,7 +94,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
94
  requirements:
93
95
  - - ">="
94
96
  - !ruby/object:Gem::Version
95
- hash: 3
96
97
  segments:
97
98
  - 0
98
99
  version: "0"
@@ -101,7 +102,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
102
  requirements:
102
103
  - - ">="
103
104
  - !ruby/object:Gem::Version
104
- hash: 3
105
105
  segments:
106
106
  - 0
107
107
  version: "0"
@@ -111,9 +111,12 @@ rubyforge_project: ""
111
111
  rubygems_version: 1.3.7
112
112
  signing_key:
113
113
  specification_version: 3
114
- summary: table_for_collection-1.0.2
114
+ summary: table_for_collection-1.0.3
115
115
  test_files:
116
116
  - spec/core_ex/array_spec.rb
117
117
  - spec/spec_helper.rb
118
- - spec/table_for_collection/table_spec.rb
118
+ - spec/table_for/callback_column_spec.rb
119
+ - spec/table_for/column_spec.rb
120
+ - spec/table_for/simple_column_spec.rb
121
+ - spec/table_for/table_spec.rb
119
122
  - spec/table_for_collection_spec.rb
@@ -1,30 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe TableHelper::Table do
4
- # ActionView::Base instance
5
- let(:template) do
6
- ActionView::Base.new
7
- end
8
- # users list (stubbed data)
9
- let(:users) do
10
- [
11
- mock({
12
- :name => "John Smith",
13
- :email => "tester@example.com",
14
- :address => "100, Spear Street, NY, USA",
15
- })
16
- ]
17
- end
18
- # table instance
19
- let(:table) do
20
- TableHelper::Table.new(template, users)
21
- end
22
- # check are methods available
23
- describe "should respond_to" do
24
- [:columns, :column, :draw, :content_tag].each do |method|
25
- it "#{method}" do
26
- table.should respond_to(method)
27
- end
28
- end
29
- end
30
- end