acts_as_translated 0.0.5 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e7bcf85b91c371dc0de79182b7cc0ae764db8be7
4
- data.tar.gz: d4bd339971fef7736eded1b1fbaae2261e2fdd6e
3
+ metadata.gz: 992b087e031776713de360952b84f7cdda2c4457
4
+ data.tar.gz: 46a5d433edd5429b190d34efafc25ae045bcbea7
5
5
  SHA512:
6
- metadata.gz: 7daf028af2e8b1d3244fd3a6613865a0dfe70e9a92c97397cfc3ef6504f8155147ec51d6cd6ed1436ff2a606f2b89472b91819feff7e4b2be399e5689252bc15
7
- data.tar.gz: 4f6468e62efe8b5cd2e43d6eaafaec10dbcab1db039a1fa1be8fd069654a01cc4d790230e2f443ba37f5bc6f39ab566e032b949033b638f921b208961b45e70b
6
+ metadata.gz: 67a506bca487c1b6967a9a769b84edaf6bd83a3975a06becdff13a632fef94ae8569a2ebbebf05b2ccedb0c7c8db83db2032d6eb556a98135e06922e7833bc05
7
+ data.tar.gz: 58b936dc865c9d7eacd80fa85aed97e64b1b9ab7e7e6b396fd2fcd9ce7a3c7cfd24e04eee6f4c26e2b4c2dd525857c1976d3162f90d81f02be25030e2db3a7d9
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ActsAsTranslated
2
2
 
3
- This acts_as extension allows easy attribute translation.
3
+ This acts_as extension allows easy attribute translation. The gem uses the current I18n.locale to return the translated attribute.
4
4
 
5
5
  ## Examples
6
6
 
@@ -20,7 +20,9 @@ Create a class;
20
20
 
21
21
  ```
22
22
  class Country < ActiveRecord::Base
23
- acts_as_translated :name #, :default => :nl
23
+
24
+ acts_as_translated :name # optional, pass in default fallback locale; default: :nl
25
+
24
26
  end
25
27
  ```
26
28
 
@@ -32,25 +34,36 @@ Use it in your views, controllers, ...
32
34
  <% end %>
33
35
  ```
34
36
 
35
- # Form Example
37
+ In your bundler Gemfile
38
+
39
+ ```
40
+ gem 'acts_as_translated', '~> 1.0'
41
+ ```
42
+
43
+ ## Deprication warnings
36
44
 
37
- Build a dynamic form that extends, based on the number of languages available;
45
+ To transition smoothly from the previous versions of acts_as_translated, the gem will currently support array styled definitions, as such;
38
46
 
39
47
  ```
40
- <%= form_for @country do %>
41
- ...
42
- <% @country.translated_fields_to_attributes.each do |attribute| %>
43
- <p><%= f.label attribute %><br/>
44
- <%= f.text_field attribute %></p>
45
- <% end %>
46
- ...
47
- <% end %>
48
+ class Country < ActiveRecord::Base
49
+
50
+ acts_as_translated [:name, :description]
51
+
52
+ end
48
53
  ```
49
54
 
50
- # Install
55
+ But this will be removed in future versions as the preferred way is to write;
51
56
 
52
- In your bundler Gemfile
57
+ ```
58
+ class Country < ActiveRecord::Base
53
59
 
60
+ acts_as_translated :name, :description
61
+
62
+ end
54
63
  ```
55
- gem 'acts_as_translated'
56
- ```
64
+
65
+ ## History
66
+
67
+ I've started development on this gem in 2008, way before I18n was considered the de-facto standard gem for internationalising a Ruby or Ruby-on-Rails app.
68
+
69
+ The gem has been available for a few years now and serves in various production code.
@@ -1,25 +1,27 @@
1
1
  # coding: utf-8
2
- $:.push File.expand_path("../lib", __FILE__)
2
+ $:.push File.expand_path('../lib', __FILE__)
3
3
  require 'acts_as_translated/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
- spec.name = "acts_as_translated"
7
- spec.version = ActsAsTranslated::VERSION
8
- spec.authors = ["Stijn Mathysen"]
9
- spec.email = ["stijn@skylight.be"]
10
- spec.summary = %q{This acts_as extension allows easy attribute translation.}
11
- spec.homepage = "https://github.com/stijnster/acts_as_translated"
12
- spec.license = "MIT"
13
- spec.description = %q{ActsAsTranslated is an acts_as for ActiveRecord that allows easy attribute translation.}
6
+ spec.name = 'acts_as_translated'
7
+ spec.version = ActsAsTranslated::VERSION
8
+ spec.authors = [ 'Stijn Mathysen' ]
9
+ spec.email = [ 'stijn@skylight.be' ]
10
+ spec.summary = 'This acts_as extension allows easy attribute translation.'
11
+ spec.homepage = 'https://github.com/stijnster/acts_as_translated'
12
+ spec.license = 'MIT'
13
+ spec.description = 'ActsAsTranslated is an acts_as for ActiveRecord that allows easy attribute translation.'
14
14
 
15
- spec.files = `git ls-files -z`.split("\x0")
16
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
- spec.require_paths = ["lib"]
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = [ 'lib' ]
19
+
20
+ spec.platform = Gem::Platform::RUBY
19
21
 
20
22
  spec.add_dependency 'activerecord', '>= 3.0.0'
21
23
 
22
24
  spec.add_development_dependency 'sqlite3', '~> 0'
23
25
  spec.add_development_dependency 'rake', '~> 0'
24
26
  spec.add_development_dependency 'minitest', '~> 5.8'
25
- end
27
+ end
@@ -6,10 +6,8 @@ module ActsAsTranslated
6
6
  require 'acts_as_translated/railtie' if defined? Rails::Railtie
7
7
 
8
8
  def self.included(base)
9
-
10
9
  class << base
11
- attr_accessor :language
12
- attr_accessor :translated_fields
10
+ attr_reader :cached_translated_attributes
13
11
  end
14
12
 
15
13
  base.extend(ClassMethods)
@@ -17,45 +15,43 @@ module ActsAsTranslated
17
15
 
18
16
  module ClassMethods
19
17
 
20
- def acts_as_translated(fields, options = {})
21
- self.language = options[:default] || 'en'
22
- self.translated_fields = Array.new if self.translated_fields.blank?
23
- self.translated_fields << fields
24
- self.translated_fields.flatten!
25
- class_eval do
26
- include InstanceMethods
18
+ def acts_as_translated(*args, default: :en)
19
+
20
+ # Support old style coded array attributes, for now
21
+ if args.first.instance_of? Array
22
+ args = args.first
23
+ Kernel.warn "[DEPRECATION] ActsAsTranslated will no longer support array-styles options in the future. Please check the documentation."
27
24
  end
28
- end
29
25
 
30
- end
26
+ @cached_translated_attributes ||= []
31
27
 
32
- module InstanceMethods
28
+ class_eval do
29
+ args.each do |attribute|
33
30
 
34
- def translated_field_exists(field)
35
- self.class.translated_fields.member?(field.to_sym) && attributes.member?("#{field}_#{self.class.language}")
36
- end
31
+ raise "The method #{attribute} already exists for #{self}" if self.instance_methods(true).include?(attribute)
37
32
 
38
- def translated_fields_to_attributes
39
- result = Array.new
40
- self.class.translated_fields.each do |field|
41
- attributes.each do |attribute, value|
42
- if attribute =~ /\A#{field}_\w{2}\Z/
43
- result << attribute
33
+ define_method attribute do
34
+ self.class.acts_as_translated_attribute(self, attribute, I18n.locale, default: default)
44
35
  end
45
36
  end
46
37
  end
47
-
48
- result.sort
49
38
  end
50
39
 
51
- def respond_to?(field, include_priv = false)
52
- return true if translated_field_exists(field)
53
- super
54
- end
40
+ def acts_as_translated_attribute(object, name, locale, default: :en)
41
+ attribute = "#{name}_#{locale}".to_sym
42
+
43
+ if @cached_translated_attributes.include?(attribute)
44
+ return object.send attribute
45
+ end
55
46
 
56
- def method_missing(field, *args)
57
- return self["#{field}_#{self.class.language}".to_sym] if translated_field_exists(field)
58
- super
47
+ if object.respond_to? attribute
48
+ @cached_translated_attributes << attribute
49
+ return object.send attribute
50
+ else
51
+ return object.send "#{name}_#{default}".to_sym
52
+ end
59
53
  end
54
+
60
55
  end
56
+
61
57
  end
@@ -1,5 +1,5 @@
1
1
  module ActsAsTranslated
2
2
 
3
- VERSION = '0.0.5'
3
+ VERSION = '1.0.2'
4
4
 
5
5
  end
@@ -20,12 +20,16 @@ def setup_db
20
20
  ActiveRecord::Base.logger
21
21
  ActiveRecord::Schema.define(:version => 1) do
22
22
  create_table :countries do |t|
23
+ t.column :iso, :string, index: true, unique: true, null: false
23
24
  t.column :name_nl, :string
24
25
  t.column :name_fr, :string
25
26
  t.column :name_en, :string
26
27
  t.column :description_nl, :text
27
28
  t.column :description_fr, :text
28
29
  t.column :description_en, :text
30
+ t.column :slug_nl, :string
31
+ t.column :slug_fr, :string
32
+ t.column :slug_en, :string
29
33
  t.column :created_at, :datetime
30
34
  t.column :updated_at, :datetime
31
35
  t.column :position, :integer
@@ -50,8 +54,17 @@ end
50
54
  class Country < ActiveRecord::Base
51
55
  include ActsAsTranslated
52
56
 
53
- acts_as_translated :name
54
- acts_as_translated :description
57
+ acts_as_translated :name, :description, default: :nl
58
+ acts_as_translated :slug
59
+ acts_as_translated :custom, default: :fr
60
+
61
+ def custom_fr
62
+ "Custom FR method"
63
+ end
64
+
65
+ def custom_es
66
+ "Custom ES method"
67
+ end
55
68
  end
56
69
 
57
70
 
@@ -72,50 +85,150 @@ class ActsAsTranslatedTest < Minitest::Test
72
85
  end
73
86
 
74
87
  def test_fixtures
75
- assert_equal 2, Country.count
76
- assert_equal 'Belgium', Country.first.name_en
77
- assert_equal 'Netherlands', Country.last.name_en
88
+ assert_equal 3, Country.count
89
+ assert_equal 'Belgium', Country.find_by_iso(:be).name_en
90
+ assert_equal 'Netherlands', Country.find_by_iso(:nl).name_en
91
+ assert_equal 'France', Country.find_by_iso(:fr).name_fr
78
92
  end
79
93
 
80
- def test_translated_attributes
81
- @belgium = Country.first
82
- @netherlands = Country.last
94
+ def test_acts_as_translated_attribute_presence
95
+ refute_respond_to Country.new, :acts_as_translated_attribute
96
+ assert_respond_to Country, :acts_as_translated_attribute
83
97
 
84
- # test default language
85
- assert_equal 'Belgium', @belgium.name
86
- assert_equal 'Netherlands', @netherlands.name
87
- assert_equal 'Kingdom of Belgium', @belgium.description
88
- assert_equal 'Kingdom of the Netherlands', @netherlands.description
98
+ end
99
+
100
+ def test_acts_as_translated_attribute_matches
101
+ @country = Country.find_by_iso(:be)
102
+
103
+ assert_equal 'België', Country.acts_as_translated_attribute(@country, 'name', 'nl')
104
+ assert_equal 'Belgique', Country.acts_as_translated_attribute(@country, 'name', 'fr')
105
+ assert_equal 'Royaume de Belgique', Country.acts_as_translated_attribute(@country, 'description', 'fr')
106
+
107
+ @country = Country.find_by_iso(:nl)
108
+
109
+ assert_equal 'Nederland', Country.acts_as_translated_attribute(@country, :name, :nl)
110
+ assert_equal 'Pays-Bas', Country.acts_as_translated_attribute(@country, :name, :fr)
111
+ assert_equal 'Kingdom of the Netherlands', Country.acts_as_translated_attribute(@country, 'description', :en)
112
+ end
113
+
114
+ def test_acts_as_translated_attribute_defaults
115
+ @country = Country.find_by_iso(:be)
116
+
117
+ assert_equal 'België', Country.acts_as_translated_attribute(@country, 'name', 'es', default: 'nl')
118
+ assert_equal 'Belgium', Country.acts_as_translated_attribute(@country, 'name', 'de')
119
+ assert_equal 'Royaume de Belgique', Country.acts_as_translated_attribute(@country, 'description', 'ch', default: 'fr')
120
+
121
+ @country = Country.find_by_iso(:nl)
122
+
123
+ assert_equal 'Nederland', Country.acts_as_translated_attribute(@country, :name, :es, default: :nl)
124
+ assert_equal 'Netherlands', Country.acts_as_translated_attribute(@country, :name, :de)
125
+ assert_equal 'Koninkrijk der Nederlanden', Country.acts_as_translated_attribute(@country, :description, :ch, default: :nl)
126
+ end
127
+
128
+ def test_acts_as_translated_attribute_no_default
129
+ @country = Country.find_by_iso(:be)
130
+
131
+ assert_raises NoMethodError do
132
+ Country.acts_as_translated_attribute(@country, 'name', 'es', default: 'es')
133
+ end
134
+
135
+ assert_raises NoMethodError do
136
+ Country.acts_as_translated_attribute(@country, :name, :es, default: :es)
137
+ end
138
+
139
+ assert_raises NoMethodError do
140
+ Country.acts_as_translated_attribute(@country, 'position', 'es')
141
+ end
142
+
143
+ assert_raises NoMethodError do
144
+ Country.acts_as_translated_attribute(@country, :position, :es)
145
+ end
146
+ end
147
+
148
+ def test_method_generation
149
+ @country = Country.find_by_iso(:be)
150
+
151
+ refute_respond_to Country, :description
152
+ assert_respond_to @country, :description
153
+ assert_respond_to @country, :name
154
+ assert_respond_to @country, :slug
155
+ end
156
+
157
+ def test_acts_as_translated_attributes
158
+ @belgium = Country.find_by_iso(:be)
159
+ @netherlands = Country.find_by_iso(:nl)
160
+
161
+ I18n.enforce_available_locales = false
162
+
163
+ I18n.locale = :nl
89
164
 
90
- # change language to dutch
91
- Country.language = 'nl'
92
165
  assert_equal 'België', @belgium.name
93
- assert_equal 'Nederland', @netherlands.name
94
166
  assert_equal 'Koninkrijk België', @belgium.description
167
+ assert_equal 'belgie', @belgium.slug
168
+
169
+ assert_equal 'Nederland', @netherlands.name
95
170
  assert_equal 'Koninkrijk der Nederlanden', @netherlands.description
171
+ assert_equal 'nederland', @netherlands.slug
172
+
173
+
174
+ I18n.locale = :fr
96
175
 
97
- # change language to french
98
- Country.language = 'fr'
99
176
  assert_equal 'Belgique', @belgium.name
100
- assert_equal 'Pays-Bas', @netherlands.name
101
177
  assert_equal 'Royaume de Belgique', @belgium.description
178
+ assert_equal 'belgique', @belgium.slug
179
+
180
+ assert_equal 'Pays-Bas', @netherlands.name
102
181
  assert_equal 'Royaume des Pays-Bas', @netherlands.description
182
+ assert_equal 'pays-bas', @netherlands.slug
183
+
184
+ I18n.locale = :en
103
185
 
104
- # change language to english
105
- Country.language = 'en'
106
186
  assert_equal 'Belgium', @belgium.name
107
- assert_equal 'Netherlands', @netherlands.name
108
187
  assert_equal 'Kingdom of Belgium', @belgium.description
188
+ assert_equal 'belgium', @belgium.slug
189
+
190
+ assert_equal 'Netherlands', @netherlands.name
109
191
  assert_equal 'Kingdom of the Netherlands', @netherlands.description
192
+ assert_equal 'netherlands', @netherlands.slug
110
193
  end
111
194
 
112
- def test_class_helpers
113
- @country = Country.first
195
+ def test_acts_as_translated_attribute_defaults
196
+ @belgium = Country.find_by_iso(:be)
197
+ @netherlands = Country.find_by_iso(:nl)
114
198
 
115
- assert @country.translated_field_exists('name')
116
- refute @country.translated_field_exists('position')
199
+ I18n.enforce_available_locales = false
117
200
 
118
- assert_equal %w(description_en description_fr description_nl name_en name_fr name_nl), @country.translated_fields_to_attributes
201
+ I18n.locale = :es
202
+
203
+ assert_equal 'België', @belgium.name
204
+ assert_equal 'Koninkrijk België', @belgium.description
205
+ assert_equal 'belgium', @belgium.slug
206
+
207
+ assert_equal 'Nederland', @netherlands.name
208
+ assert_equal 'Koninkrijk der Nederlanden', @netherlands.description
209
+ assert_equal 'netherlands', @netherlands.slug
210
+ end
211
+
212
+ def test_acts_as_translated_methods_with_defaults
213
+ @belgium = Country.find_by_iso(:be)
214
+
215
+ I18n.enforce_available_locales = false
216
+
217
+ I18n.locale = :nl
218
+ assert_equal 'Custom FR method', @belgium.custom
219
+
220
+ I18n.locale = :es
221
+ assert_equal 'Custom ES method', @belgium.custom
222
+ end
223
+
224
+ def test_existing_method_names_should_fail
225
+ Country.acts_as_translated(:some_method_name_that_doesnt_exist)
226
+ assert_raises RuntimeError do
227
+ Country.acts_as_translated(:id)
228
+ end
229
+ assert_raises RuntimeError do
230
+ Country.acts_as_translated(:send)
231
+ end
119
232
  end
120
233
 
121
234
  end
@@ -1,16 +1,36 @@
1
1
  belgium:
2
+ iso: be
2
3
  name_nl: België
3
4
  name_fr: Belgique
4
5
  name_en: Belgium
5
6
  description_nl: Koninkrijk België
6
7
  description_fr: Royaume de Belgique
7
8
  description_en: Kingdom of Belgium
9
+ slug_nl: belgie
10
+ slug_fr: belgique
11
+ slug_en: belgium
8
12
  position: 1
9
13
  holland:
14
+ iso: nl
10
15
  name_nl: Nederland
11
16
  name_fr: Pays-Bas
12
17
  name_en: Netherlands
13
18
  description_nl: Koninkrijk der Nederlanden
14
19
  description_fr: Royaume des Pays-Bas
15
20
  description_en: Kingdom of the Netherlands
16
- position: 2
21
+ slug_nl: nederland
22
+ slug_fr: pays-bas
23
+ slug_en: netherlands
24
+ position: 2
25
+ france:
26
+ iso: fr
27
+ name_nl: Frankrijk
28
+ name_fr: France
29
+ name_en:
30
+ description_nl:
31
+ description_fr:
32
+ description_en:
33
+ slug_nl: frankrijk
34
+ slug_fr: france
35
+ slug_en:
36
+ position: 3
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_translated
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stijn Mathysen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-16 00:00:00.000000000 Z
11
+ date: 2015-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord