table_for_collection 1.0.6 → 1.0.7

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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZDdhZmYyNDM3OWU5NDI1OGE3MzdjMTBhODI0ZDE5MWU1MWQ2MzhmYw==
5
+ data.tar.gz: !binary |-
6
+ YjRkOGVmMThhZWI0YmEwZjdkYWVkODRjNTk2ZjI1ZGI0YzM2MGFkOA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZmIyNzYxOThjNTMxMzE1NjRmYWQ2ODM5MTU5M2QwYzgwZTNmOTUzMWViMjgw
10
+ ZjQxMmQ0ZTAwNWNlNzA0ZDg3ZTcwMjJlNGRjMmU0MDQzZWVkNDZiZTJiYThk
11
+ Nzk1NmUzMGNlNDY3ZWU0NGU0YWUzMzc5NjRlYmU3NzM2ODdhOTA=
12
+ data.tar.gz: !binary |-
13
+ NjJlZjlkNTQyMGYyYjYwOTI3NjdlZDVkYjM3ZGU4MDc0YTY1Njk5OGVmMmQ2
14
+ N2JiMGMzODdhNTk3ODQwYzUzOGY0ODhiMGMwNmI2MjgxY2JiZTk0Yzg3MTVh
15
+ YzlhMzZkNzllY2I3Zjg5NzA1OGZlNTIzMTcwY2JkMjFmYmI5NjI=
data/Changelog CHANGED
@@ -1,4 +1,9 @@
1
- == master
1
+ == v.1.0.7
2
+
3
+ Add :attr option
4
+ Add :default option
5
+ Dynamic :class for tr added
6
+ Gemfile fix
2
7
 
3
8
  == v.1.0.6
4
9
 
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source :gemcutter
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gem "actionpack"
4
4
 
@@ -1,4 +1,6 @@
1
+ {<img src="https://fury-badge.herokuapp.com/rb/table_for_collection.png" />}[http://badge.fury.io/rb/table_for_collection]
1
2
  {<img src="https://secure.travis-ci.org/lunich/table_for.png" />}[http://travis-ci.org/lunich/table_for]
3
+ {<img src="https://codeclimate.com/github/lunich/table_for.png" />}[https://codeclimate.com/github/lunich/table_for]
2
4
 
3
5
  == Description
4
6
 
@@ -43,7 +45,7 @@ This ruby code will produce following HTML:
43
45
  :id => "users",
44
46
  :width => "100%",
45
47
  :tr => {
46
- :class => "simple-row",
48
+ :class => lambda { |user| "simple-row #{user.role}" },
47
49
  :id => lambda { |user| "user-row-#{user.id}" }
48
50
  }
49
51
  } do -%>
@@ -97,6 +99,7 @@ It will produce HTML similar to:
97
99
  <% column :title => "Actions" do |user| %>
98
100
  <% [link_to("Show", user), link_to("Delete", user, :method => :delete)].join(" | ") %>
99
101
  <% end %>
102
+ <% column :role, :default => "guest" %>
100
103
  <% column :created_at, :time_format => "%Y-%m-%d" %>
101
104
  <% end %>
102
105
 
@@ -114,7 +117,17 @@ Also class given by :stripe option will be merged with options[:html][:tr] so th
114
117
  <tr class="row odd">...
115
118
  <tr class="row even">...
116
119
  <tr class="row odd">...
117
-
120
+
121
+ == Relations
122
+
123
+ If you need a column for relation or apply method for simple column you can use :attr option:
124
+
125
+ <%= table_for @users do -%>
126
+ <% column :login %>
127
+ <% column :company, :attr => :name %>
128
+ <% column :name, :attr => :underscore %>
129
+ <% end %>
130
+
118
131
  == I18n
119
132
 
120
133
  When column name is set, but :title is not, helper tries to find translation in standard mechanism of the ActiveModel.
@@ -2,6 +2,15 @@ module TableHelper
2
2
  class SimpleColumn < Column # :nodoc:
3
3
  def content_for(record)
4
4
  called_record = record.kind_of?(Hash) ? record[@attr] : record.send(@attr)
5
+
6
+ if @options[:attr] && called_record.respond_to?(@options[:attr])
7
+ called_record = called_record.send(@options[:attr])
8
+ end
9
+
10
+ if @options[:default] && called_record.nil?
11
+ called_record = @options[:default]
12
+ end
13
+
5
14
  if @options[:time_format] and called_record.is_a? Time
6
15
  called_record = called_record.strftime(@options[:time_format])
7
16
  end
@@ -66,34 +66,46 @@ module TableHelper
66
66
  end
67
67
  end
68
68
 
69
+ def columns_content(record)
70
+ @columns.map do |col|
71
+ content_tag :td, col.html[:td] do
72
+ col.content_for(record).to_s
73
+ end
74
+ end.join.html_safe
75
+ end
76
+
77
+ def records_content
78
+ @records.map do |record|
79
+ content_tag :tr, tr_options(record) do
80
+ columns_content(record)
81
+ end
82
+ end.join.html_safe
83
+ end
84
+
69
85
  def body
70
- content_tag :tbody do
71
- @records.map do |rec|
72
- content_tag :tr, tr_options(rec) do
73
- @columns.map do |col|
74
- content_tag :td, col.html[:td] do
75
- col.content_for(rec).to_s
76
- end
77
- end.join.html_safe
78
- end
79
- end.join.html_safe
80
- end
86
+ content_tag :tbody, records_content
81
87
  end
82
-
83
- def tr_options(rec)
88
+
89
+ def tr_classes(record, base_class)
90
+ klasses = []
91
+ klasses << dom_class(record) if record.respond_to?(:model_name)
92
+ klasses << base_class if base_class
93
+ stripe = @stripes.next
94
+ klasses << stripe if stripe
95
+ klasses
96
+ end
97
+
98
+ def tr_options(record)
84
99
  res = @tr_html_options.nil? ? {} : @tr_html_options.clone
85
- html_class = @stripes.next
86
- unless html_class.nil?
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)
100
+ # check if we have proc in :id or :class
101
+ [:id, :class].each do |key|
102
+ res[key] = res[key].call(record) if res.has_key?(key) && res[key].respond_to?(:call)
93
103
  end
94
- res[:id] ||= dom_id(rec) if rec.respond_to?(:to_key)
104
+ # update class
105
+ res[:class] = tr_classes(record, res[:class]).join(" ")
106
+ # update id
107
+ res[:id] ||= dom_id(record) if record.respond_to?(:to_key)
95
108
  res
96
109
  end
97
-
98
110
  end
99
111
  end
@@ -1,4 +1,4 @@
1
1
  module TableHelper
2
2
  GEM_NAME = "table_for_collection"
3
- VERSION = "1.0.6"
3
+ VERSION = "1.0.7"
4
4
  end
@@ -44,6 +44,7 @@ end
44
44
  class User < OpenStruct
45
45
  require "active_model"
46
46
  extend ActiveModel::Naming
47
+
47
48
  def id
48
49
  @table[:id]
49
50
  end
@@ -6,7 +6,8 @@ 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, :created_at => Time.gm(2011, "feb", 24, 14, 23, 1)) }
9
+ let(:company) { mock(:id => 2, :name => "Zorg inc.") }
10
+ let(:user) { mock(:id => 12, :created_at => Time.gm(2011, "feb", 24, 14, 23, 1), :company => company, :role => nil) }
10
11
  # user (hash)
11
12
  let(:user_hash) { { :id => 12, :created_at => Time.gm(2011, "feb", 24, 14, 23, 1) } }
12
13
  # Instance methods
@@ -23,18 +24,43 @@ describe TableHelper::SimpleColumn do
23
24
  col.title.should eq("Some title")
24
25
  end
25
26
  end
27
+
26
28
  # :content_for
27
29
  it ":content_for method should success" do
28
30
  col = build_column(klass, :id)
29
- col.content_for(user).should == "12"
30
- col.content_for(user_hash).should == "12"
31
+ col.content_for(user).should eq("12")
32
+ col.content_for(user_hash).should eq("12")
31
33
  end
32
34
 
33
35
  describe "format" do
34
36
  it ":time_format should be displayed correctly" do
35
37
  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
+ col.content_for(user).should eq("2011-02")
39
+ col.content_for(user_hash).should eq("2011-02")
40
+ end
41
+ end
42
+
43
+ describe ":attr option" do
44
+ it "should be success" do
45
+ col = build_column(klass, :company, :attr => :name)
46
+ col.content_for(user).should eq("Zorg inc.")
47
+ end
48
+
49
+ it "should not be called when not acceptable" do
50
+ col = build_column(klass, :id, :attr => :name)
51
+ col.content_for(user).should eq("12")
52
+ end
53
+ end
54
+
55
+ describe ":default option" do
56
+ it "should be success" do
57
+ col = build_column(klass, :role, :default => "Guest")
58
+ col.content_for(user).should eq("Guest")
59
+ end
60
+
61
+ it "should not be called if have a result" do
62
+ col = build_column(klass, :id, :default => 42)
63
+ col.content_for(user).should eq("12")
38
64
  end
39
65
  end
40
66
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_for_collection
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
5
- prerelease:
4
+ version: 1.0.7
6
5
  platform: ruby
7
6
  authors:
8
7
  - Dima Lunich
@@ -11,30 +10,36 @@ authors:
11
10
  autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2011-11-17 00:00:00.000000000Z
13
+ date: 2013-11-30 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: rspec
18
- requirement: &70131758124600 !ruby/object:Gem::Requirement
19
- none: false
17
+ requirement: !ruby/object:Gem::Requirement
20
18
  requirements:
21
19
  - - ! '>='
22
20
  - !ruby/object:Gem::Version
23
21
  version: 2.0.0
24
22
  type: :development
25
23
  prerelease: false
26
- version_requirements: *70131758124600
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: 2.0.0
27
29
  - !ruby/object:Gem::Dependency
28
30
  name: actionpack
29
- requirement: &70131758124180 !ruby/object:Gem::Requirement
30
- none: false
31
+ requirement: !ruby/object:Gem::Requirement
31
32
  requirements:
32
33
  - - ! '>='
33
34
  - !ruby/object:Gem::Version
34
35
  version: '0'
35
36
  type: :runtime
36
37
  prerelease: false
37
- version_requirements: *70131758124180
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
38
43
  description: This gem builds HTML-table using given array
39
44
  email:
40
45
  - dima.lunich@gmail.com
@@ -66,7 +71,9 @@ files:
66
71
  - Gemfile
67
72
  - init.rb
68
73
  homepage: http://github.com/lunich/table_for
69
- licenses: []
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
70
77
  post_install_message:
71
78
  rdoc_options:
72
79
  - --main
@@ -74,23 +81,21 @@ rdoc_options:
74
81
  require_paths:
75
82
  - lib
76
83
  required_ruby_version: !ruby/object:Gem::Requirement
77
- none: false
78
84
  requirements:
79
85
  - - ! '>='
80
86
  - !ruby/object:Gem::Version
81
87
  version: '0'
82
88
  required_rubygems_version: !ruby/object:Gem::Requirement
83
- none: false
84
89
  requirements:
85
90
  - - ! '>='
86
91
  - !ruby/object:Gem::Version
87
92
  version: '0'
88
93
  requirements: []
89
94
  rubyforge_project: ''
90
- rubygems_version: 1.8.6
95
+ rubygems_version: 2.1.10
91
96
  signing_key:
92
- specification_version: 3
93
- summary: table_for_collection-1.0.6
97
+ specification_version: 4
98
+ summary: table_for_collection-1.0.7
94
99
  test_files:
95
100
  - spec/core_ex/array_spec.rb
96
101
  - spec/spec_helper.rb