table-for 3.1.4 → 3.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,6 @@
1
+ 3.1.5
2
+ * Added automatic internationalization (thanks killthekitten)
3
+
1
4
  3.1.0
2
5
  * Since templating was moved from blocks into its own gem with_template, TableFor::Base now extends WithTemplate::Base instead of Blocks::Base.
3
6
 
data/README.rdoc CHANGED
@@ -148,3 +148,20 @@ TODO
148
148
 
149
149
  == Using "Before" and "After" hooks
150
150
  TODO
151
+
152
+ == Internationalization
153
+ +table-for+ can do some dirty job for you, if you are using i18n by common rails conventions. For example, you have following in your en.yml:
154
+
155
+ en:
156
+ activerecord:
157
+ attributes:
158
+ user:
159
+ id: '#'
160
+ name: "Username"
161
+ bio: "Short biography"
162
+
163
+ Then, +table-for+ will automatically catch up correct translations for column headers. If you are not familiar with i18n, you can always pass a custom header to the column:
164
+
165
+ <%= table_for @users do |table| %>
166
+ <% table.column :parent_name, header: "Mr. Smith" %>
167
+ <% end %>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.4
1
+ 3.1.5
@@ -36,7 +36,7 @@ module TableFor
36
36
  elsif column.anonymous
37
37
  nil
38
38
  else
39
- column.name.to_s.titleize
39
+ I18n.t("#{translation_lookup_prefix}.#{column.name.to_s.underscore}", :default => column.name.to_s.titleize)
40
40
  end
41
41
  end
42
42
  end
@@ -83,6 +83,19 @@ module TableFor
83
83
  view.capture(self, &block)
84
84
  end
85
85
  end
86
+
87
+ private
88
+ def translation_lookup_prefix
89
+ if global_options[:records].respond_to?(:model)
90
+ "activerecord.attributes.#{global_options[:records].model.to_s.underscore}"
91
+ elsif global_options[:records].all? {|record| record.is_a?(ActiveRecord::Base) && record.class == global_options[:records].first.class }
92
+ "activerecord.attributes.#{global_options[:records].first.class.to_s.underscore}"
93
+ elsif global_options[:records].all? {|record| record.class == global_options[:records].first.class }
94
+ "tables.columns.#{global_options[:records].first.class.to_s.underscore}"
95
+ else
96
+ "tables.columns"
97
+ end
98
+ end
86
99
  end
87
100
  end
88
101
  end
@@ -9,6 +9,13 @@ describe "table_for" do
9
9
  end
10
10
  end
11
11
 
12
+ class Offer
13
+ attr_accessor :first_name
14
+ def initialize(first_name)
15
+ @first_name = first_name
16
+ end
17
+ end
18
+
12
19
  before :each do
13
20
  User.create! :email => "andrew.hunter@livingsocial.com", :first_name => "Andrew", :last_name => "Hunter"
14
21
  User.create! :email => "todd.fisher@livingsocial.com", :first_name => "Todd", :last_name => "Fisher"
@@ -75,6 +82,51 @@ describe "table_for" do
75
82
  xml = XmlSimple.xml_in(%%<table><thead style="background-color: orange"><tr></tr></thead><tbody><tr></tr></tbody></table>%)
76
83
  XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
77
84
  end
85
+
86
+ it "should translate column names" do
87
+ rails_4_active_record_array = @users[0, 1].clone
88
+ rails_4_active_record_array.expects(:model => User)
89
+ I18n.expects(:t).with("activerecord.attributes.user.first_name", :default => "First Name").returns("Vorname")
90
+ buffer = @view.table_for rails_4_active_record_array do |table|
91
+ table.column :first_name
92
+ end
93
+ xml = XmlSimple.xml_in(%%<table><thead><tr><th>Vorname</th></tr></thead><tbody><tr><td>Andrew</td></tr></tbody></table>%)
94
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
95
+
96
+ I18n.expects(:t).with("activerecord.attributes.user.first_name", :default => "First Name").returns("Vorname2")
97
+ buffer = @view.table_for @users[0,1] do |table|
98
+ table.column :first_name
99
+ end
100
+ xml = XmlSimple.xml_in(%%<table><thead><tr><th>Vorname2</th></tr></thead><tbody><tr><td>Andrew</td></tr></tbody></table>%)
101
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
102
+
103
+ non_activerecord_array = [Offer.new("Timmy")]
104
+
105
+ I18n.expects(:t).with("tables.columns.offer.first_name", :default => "First Name").returns("Vorname3")
106
+ buffer = @view.table_for non_activerecord_array do |table|
107
+ table.column :first_name
108
+ end
109
+ xml = XmlSimple.xml_in(%%<table><thead><tr><th>Vorname3</th></tr></thead><tbody><tr><td>Timmy</td></tr></tbody></table>%)
110
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
111
+
112
+ # the rare case where the array has objects of different types
113
+ mixed_array = [@users.first, Offer.new("Timmy")]
114
+ I18n.expects(:t).with("tables.columns.first_name", :default => "First Name").returns("Vorname3")
115
+ buffer = @view.table_for mixed_array do |table|
116
+ table.column :first_name
117
+ end
118
+ xml = XmlSimple.xml_in(%%<table><thead><tr><th>Vorname3</th></tr></thead><tbody><tr><td>Andrew</td></tr><tr><td>Timmy</td></tr></tbody></table>%)
119
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
120
+ end
121
+
122
+ it "should not translate column name if header passed" do
123
+ I18n.expects(:t).never
124
+ buffer = @view.table_for @users[0,1] do |table|
125
+ table.column :first_name, :header => "My First Name"
126
+ end
127
+ xml = XmlSimple.xml_in(%%<table><thead><tr><th>My First Name</th></tr></thead><tbody><tr><td>Andrew</td></tr></tbody></table>%)
128
+ XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml
129
+ end
78
130
  end
79
131
 
80
132
  describe "header_row block" do
data/table-for.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "table-for"
8
- s.version = "3.1.4"
8
+ s.version = "3.1.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrew Hunter"]
12
- s.date = "2013-10-15"
12
+ s.date = "2013-10-31"
13
13
  s.description = "table-for is a table builder for an array of objects, easily allowing overriding of how any aspect of the table is generated"
14
14
  s.email = "hunterae@gmail.com"
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table-for
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.4
4
+ version: 3.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-15 00:00:00.000000000 Z
12
+ date: 2013-10-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -234,7 +234,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
234
234
  version: '0'
235
235
  segments:
236
236
  - 0
237
- hash: 570005552086543120
237
+ hash: 2078517187188360372
238
238
  required_rubygems_version: !ruby/object:Gem::Requirement
239
239
  none: false
240
240
  requirements: