lost_in_translations 1.3.2 → 1.4.0

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: 4d1a0959d1fda7cbd026bfb47d428d44072c4faf
4
- data.tar.gz: d37b0255c177943d54b9dd3857f148a8425559e1
3
+ metadata.gz: 06590098e42d3c39e2c1f2c0af0ece92d977e2bf
4
+ data.tar.gz: dd7b741fdae089d107d8b06b844fef1d77b69ff3
5
5
  SHA512:
6
- metadata.gz: 8b4b4e39527f47afc0035ea6a56ef8ce0a2c2ef455ce2f0c1c7ad773f2d62f0946ad0b20d997798f7afe679b976a94b8e9913507ae532ebee74d5815c862efe1
7
- data.tar.gz: a4b34612c1a1dacf97943c5f1d2f4dee79e220cb706b3f934875cc8aa21989d61ba045a3274f0cda56e207cb4562796246fe05d52d12fd958f65e05a68bd17ce
6
+ metadata.gz: da5bad9a9d8b5ddf9a4e9e3c8224b57a30a8de73143d872236e7ff4643551a1ce56c71f1b9860348738aee4569b3887f152ccdd04c792d11ea8025b519a1bb94
7
+ data.tar.gz: 7ce6782a647d366264d79bb4f2ce7999ca357699be4c6da628e8671bf2c3264b989bd02ba019f098e423c030690afaf86e7376fe435b6d42b7d69efc900e50e7
data/.rubocop.yml CHANGED
@@ -4,6 +4,9 @@ Style/EmptyLinesAroundClassBody:
4
4
  Style/EmptyLinesAroundModuleBody:
5
5
  Enabled: false
6
6
 
7
+ Style/FrozenStringLiteralComment:
8
+ Enabled: false
9
+
7
10
  AllCops:
8
11
  DisplayCopNames: true
9
12
  Exclude:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lost_in_translations (1.3.2)
4
+ lost_in_translations (1.4.0)
5
5
  i18n (~> 0.7)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -127,9 +127,34 @@ I18n.locale = :fr
127
127
  @user.title # returns nil
128
128
  ```
129
129
 
130
- ## 3) Configuration
130
+ ## 3) I18n.available_locales as changed, how do I recreate the new translation methods?
131
+ ```ruby
132
+ LostInTranslations.reload # will run .define_translation_methods in every class that has "include LostInTranslations"
133
+ ```
134
+
135
+ ```ruby
136
+ I18n.available_locales == [:pt]
137
+
138
+ class User < ActiveRecord::Base
139
+ include LostInTranslations
140
+
141
+ translate :first_name
142
+ end
143
+
144
+ User.new.respond_to?(:pt_first_name) == true
145
+ User.new.respond_to?(:en_first_name) == false
146
+
147
+ I18n.available_locales.push(:en)
148
+
149
+ LostInTranslations.reload
150
+
151
+ User.new.respond_to?(:pt_first_name) == true
152
+ User.new.respond_to?(:en_first_name) == true
153
+ ```
154
+
155
+ ## 4) Configuration
131
156
 
132
- ### 3.1) Your "translation_data" method (in all of your objects) is not called "translation_data"
157
+ ### 4.1) Your "translation_data" method (in all of your objects) is not called "translation_data"
133
158
  ```ruby
134
159
  LostInTranslations.configure do |config|
135
160
  config.translation_data_field = 'my_translation_data_field'
@@ -155,7 +180,7 @@ I18n.locale = :fr
155
180
  User.find(1).first_name # returns 'Jean'
156
181
  ```
157
182
 
158
- ### 3.2) Your "translation_data" method (in a particular object) is not called "translation_data"
183
+ ### 4.2) Your "translation_data" method (in a particular object) is not called "translation_data"
159
184
  ```ruby
160
185
  class User < ActiveRecord::Base
161
186
  include LostInTranslations
@@ -177,7 +202,7 @@ I18n.locale = :fr
177
202
  User.find(1).first_name # returns 'Jean'
178
203
  ```
179
204
 
180
- ### 3.3) Custom translation mechanism
205
+ ### 4.3) Custom translation mechanism
181
206
  ```ruby
182
207
  class MyTranslator < Translator::Base
183
208
  def self.translation_data(object)
@@ -221,7 +246,7 @@ I18n.locale = :fr
221
246
  User.find(1).first_name # returns 'Jean' from redis or yaml file
222
247
  ```
223
248
 
224
- ## 4) Instalation
249
+ ## 5) Instalation
225
250
 
226
251
  Add your application's Gemfile:
227
252
  ```
@@ -65,4 +65,12 @@ module LostInTranslations
65
65
  yield(config)
66
66
  end
67
67
 
68
+ def self.infected_classes
69
+ @infected_classes ||= []
70
+ end
71
+
72
+ def self.reload
73
+ infected_classes.each(&:define_translation_methods)
74
+ end
75
+
68
76
  end
@@ -3,17 +3,15 @@ module LostInTranslations
3
3
 
4
4
  module ClassMethods
5
5
 
6
- def translate(*fields)
7
- LostInTranslations.define_translation_methods(self, *fields)
6
+ def define_translation_methods
7
+ LostInTranslations.define_translation_methods self, *translation_fields
8
8
  end
9
9
 
10
10
  end
11
11
 
12
12
  def self.included(base_class)
13
- base_class.class_eval do
14
- include Base
15
- extend ClassMethods
16
- end
13
+ base_class.send(:include, Base)
14
+ base_class.extend ClassMethods
17
15
  end
18
16
 
19
17
  def call_original_field(object, field)
@@ -2,6 +2,8 @@ module LostInTranslations
2
2
  module Base
3
3
 
4
4
  def self.included(base_class)
5
+ LostInTranslations.infected_classes.push(base_class)
6
+
5
7
  base_class.extend ClassMethods
6
8
  end
7
9
 
@@ -28,6 +30,16 @@ module LostInTranslations
28
30
  LostInTranslations.config.translation_data_field
29
31
  end
30
32
 
33
+ def translation_fields
34
+ @translation_fields ||= []
35
+ end
36
+
37
+ def translate(*fields)
38
+ translation_fields.concat(fields)
39
+
40
+ define_translation_methods
41
+ end
42
+
31
43
  end
32
44
 
33
45
  end
@@ -3,21 +3,23 @@ module LostInTranslations
3
3
 
4
4
  module ClassMethods
5
5
 
6
- def translate(*fields)
7
- fields.each do |field|
8
- alias_method Ruby.original_field_name(field), field.to_sym
6
+ def define_translation_methods
7
+ translation_fields.each do |field|
8
+ next if instance_methods.include?(Ruby.original_field_name(field))
9
+
10
+ class_eval do
11
+ alias_method Ruby.original_field_name(field), field.to_sym
12
+ end
9
13
  end
10
14
 
11
- LostInTranslations.define_translation_methods(self, *fields)
15
+ LostInTranslations.define_translation_methods self, *translation_fields
12
16
  end
13
17
 
14
18
  end
15
19
 
16
20
  def self.included(base_class)
17
- base_class.class_eval do
18
- include Base
19
- extend ClassMethods
20
- end
21
+ base_class.send(:include, Base)
22
+ base_class.extend ClassMethods
21
23
  end
22
24
 
23
25
  def call_original_field(object, field)
@@ -1,5 +1,5 @@
1
1
  module LostInTranslations
2
2
 
3
- VERSION = '1.3.2'.freeze
3
+ VERSION = '1.4.0'.freeze
4
4
 
5
5
  end
@@ -2,85 +2,50 @@ require 'spec_helper'
2
2
 
3
3
  describe LostInTranslations::ActiveRecord do
4
4
 
5
- describe 'readme example' do
6
- context "basic usage" do
7
- before do
8
- @user_class = Class.new(ActiveRecord::Base) do
9
- self.table_name = 'users'
5
+ context "when building a resource" do
6
+ before do
7
+ @user_class = Class.new(ActiveRecord::Base) do
8
+ self.table_name = 'users'
10
9
 
11
- include LostInTranslations::ActiveRecord
10
+ include LostInTranslations::ActiveRecord
12
11
 
13
- translate :title, :first_name
12
+ translate :title, :first_name
14
13
 
15
- def translation_data
16
- @translation_data ||= {
17
- en: { first_name: 'Jon', last_name: 'Snow' },
18
- fr: { first_name: 'Jean', last_name: 'Neige' }
19
- }
20
- end
14
+ def translation_data
15
+ @translation_data ||= {
16
+ en: { first_name: 'Jon', last_name: 'Snow' },
17
+ fr: { first_name: 'Jean', last_name: 'Neige' }
18
+ }
21
19
  end
22
-
23
- @user = @user_class.new(title: 'Cavaleiro', first_name: 'Joao', last_name: 'Neve')
24
20
  end
25
21
 
26
- it_behaves_like "a basic usage"
22
+ @user = @user_class.new(title: 'Cavaleiro', first_name: 'Joao', last_name: 'Neve')
27
23
  end
24
+
25
+ it_behaves_like "the readme example"
28
26
  end
29
27
 
30
- # describe '.translate' do
31
- #
32
- # context "when translating a field" do
33
- # before do
34
- # @user_class = Class.new(ActiveRecord::Base) do
35
- # self.table_name = 'users'
36
- #
37
- # include LostInTranslations::ActiveRecord
38
- #
39
- # translate :title, :first_name
40
- #
41
- # def translation_data
42
- # @translation_data ||= {
43
- # en: { first_name: 'Jon', last_name: 'Snow' },
44
- # fr: { first_name: 'Jean', last_name: 'Neige' }
45
- # }
46
- # end
47
- # end
48
- #
49
- # # @user_class.create title: 'Cavaleiro', first_name: 'Joao', last_name: 'Neve'
50
- # @user = @user_class.first
51
- # end
52
- # # after { @user.destroy }
53
- #
54
- # it_behaves_like "a proper translator"
55
- # end
56
- #
57
- # context "when a particular field is not translated" do
58
- # before do
59
- # @user_class = Class.new(ActiveRecord::Base) do
60
- # self.table_name = 'users'
61
- #
62
- # include LostInTranslations::ActiveRecord
63
- #
64
- # translate :title, :first_name
65
- #
66
- # def translation_data
67
- # @translation_data ||= {
68
- # en: { first_name: 'Jon', last_name: 'Snow' },
69
- # fr: { first_name: 'Jean', last_name: 'Neige' }
70
- # }
71
- # end
72
- # end
73
- #
74
- # @user = @user_class.new title: 'Cavaleiro', first_name: 'Joao', last_name: 'Neve'
75
- # end
76
- #
77
- # it "#field must return the original data" do
78
- # I18n.with_locale(:en) do
79
- # expect(@user.last_name).to eq 'Neve'
80
- # end
81
- # end
82
- # end
83
- #
84
- # end
28
+ context "When finding a resource" do
29
+ before do
30
+ @user_class = Class.new(ActiveRecord::Base) do
31
+ self.table_name = 'users'
32
+
33
+ include LostInTranslations::ActiveRecord
34
+
35
+ translate :title, :first_name
36
+
37
+ def translation_data
38
+ @translation_data ||= {
39
+ en: { first_name: 'Jon', last_name: 'Snow' },
40
+ fr: { first_name: 'Jean', last_name: 'Neige' }
41
+ }
42
+ end
43
+ end
44
+
45
+ @user = @user_class.first
46
+ end
47
+
48
+ it_behaves_like "the readme example"
49
+ end
85
50
 
86
51
  end
@@ -2,6 +2,67 @@ require 'spec_helper'
2
2
 
3
3
  describe LostInTranslations do
4
4
 
5
+ describe "#reload" do
6
+ context "when the available_locales do not include :es" do
7
+ before do
8
+ LostInTranslations.infected_classes.clear
9
+
10
+ @user_class1 = Struct.new(:first_name, :last_name) do
11
+ include LostInTranslations::Ruby
12
+
13
+ translate :first_name
14
+ end
15
+
16
+ @user_class2 = Class.new(ActiveRecord::Base) do
17
+ self.table_name = 'users'
18
+
19
+ include LostInTranslations::ActiveRecord
20
+
21
+ translate :last_name
22
+ end
23
+
24
+ @user1 = @user_class1.new('Joao', 'Neve')
25
+ @user2 = @user_class2.new(title: 'Cavaleiro', first_name: 'Joao', last_name: 'Neve')
26
+ end
27
+
28
+ it "#pt_first_name SHOULD be defined" do
29
+ expect(@user1.respond_to?(:pt_first_name)).to be true
30
+ expect(@user2.respond_to?(:pt_last_name)).to be true
31
+ end
32
+
33
+ it "#es_first_name should NOT be defined" do
34
+ expect(@user1.respond_to?(:es_first_name)).to be false
35
+ expect(@user1.respond_to?(:es_last_name)).to be false
36
+ end
37
+
38
+ context "when :es gets introduced" do
39
+ before { I18n.available_locales.push(:es) }
40
+ after { I18n.available_locales.pop }
41
+
42
+ context "and .reload as NOT ran" do
43
+ it "#es_first_name should NOT be defined" do
44
+ expect(@user1.respond_to?(:es_first_name)).to be false
45
+ expect(@user2.respond_to?(:es_last_name)).to be false
46
+ end
47
+ end
48
+
49
+ context "and .define_translation_methods AS ran" do
50
+ before { LostInTranslations.reload }
51
+
52
+ it "#pt_first_name SHOULD be defined" do
53
+ expect(@user1.respond_to?(:pt_first_name)).to be true
54
+ expect(@user2.respond_to?(:pt_last_name)).to be true
55
+ end
56
+
57
+ it "#es_first_name SHOULD be defined" do
58
+ expect(@user1.respond_to?(:es_first_name)).to be true
59
+ expect(@user2.respond_to?(:es_last_name)).to be true
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+
5
66
  describe "#define_translation_methods" do
6
67
  context "when the passed methods do not exist" do
7
68
  before do
@@ -2,77 +2,25 @@ require 'spec_helper'
2
2
 
3
3
  describe LostInTranslations::Ruby do
4
4
 
5
- describe 'readme example' do
6
- context "basic usage" do
7
- before do
8
- @user_class = Struct.new(:title, :first_name, :last_name) do
9
- include LostInTranslations::Ruby
10
-
11
- translate :title, :first_name
12
-
13
- def translation_data
14
- @translation_data ||= {
15
- en: { first_name: 'Jon', last_name: 'Snow' },
16
- fr: { first_name: 'Jean', last_name: 'Neige' }
17
- }
18
- end
5
+ context "when building a resource" do
6
+ before do
7
+ @user_class = Struct.new(:title, :first_name, :last_name) do
8
+ include LostInTranslations::Ruby
9
+
10
+ translate :title, :first_name
11
+
12
+ def translation_data
13
+ @translation_data ||= {
14
+ en: { first_name: 'Jon', last_name: 'Snow' },
15
+ fr: { first_name: 'Jean', last_name: 'Neige' }
16
+ }
19
17
  end
20
-
21
- @user = @user_class.new('Cavaleiro', 'Joao', 'Neve')
22
18
  end
23
19
 
24
- it_behaves_like "a basic usage"
20
+ @user = @user_class.new('Cavaleiro', 'Joao', 'Neve')
25
21
  end
26
- end
27
22
 
28
- # describe '.translate' do
29
- #
30
- # context "when translating a field" do
31
- # before do
32
- # @user_class = Struct.new(:title, :first_name, :last_name) do
33
- # include LostInTranslations::Ruby
34
- #
35
- # translate :title, :first_name
36
- #
37
- # def translation_data
38
- # @translation_data ||= {
39
- # en: { first_name: 'Jon', last_name: 'Snow' },
40
- # fr: { first_name: 'Jean', last_name: 'Neige' }
41
- # }
42
- # end
43
- # end
44
- #
45
- # @user = @user_class.new('Cavaleiro', 'Joao', 'Neve')
46
- # end
47
- #
48
- # it_behaves_like "a proper translator"
49
- # end
50
- #
51
- # context "when a particular field is not translated" do
52
- # before do
53
- # @user_class = Struct.new(:title, :first_name, :last_name) do
54
- # include LostInTranslations::Ruby
55
- #
56
- # translate :title, :first_name
57
- #
58
- # def translation_data
59
- # @translation_data ||= {
60
- # en: { first_name: 'Jon', last_name: 'Snow' },
61
- # fr: { first_name: 'Jean', last_name: 'Neige' }
62
- # }
63
- # end
64
- # end
65
- #
66
- # @user = @user_class.new('Cavaleiro', 'Joao', 'Neve')
67
- # end
68
- #
69
- # it "#field must return the original data" do
70
- # I18n.with_locale(:en) do
71
- # expect(@user.last_name).to eq 'Neve'
72
- # end
73
- # end
74
- # end
75
- #
76
- # end
23
+ it_behaves_like "the readme example"
24
+ end
77
25
 
78
26
  end
@@ -1,63 +1,98 @@
1
1
  # encoding: UTF-8
2
2
 
3
- shared_examples_for "a basic usage" do
4
- it "should behave like the readme says it does" do
5
- I18n.default_locale = :pt
6
- I18n.locale = :fr
3
+ shared_examples_for "the readme example" do
4
+ context "when I18n.locale == :fr" do
5
+ before { I18n.locale = :fr }
6
+ after { I18n.locale = :pt }
7
7
 
8
- expect(@user.first_name).to eq 'Jean'
9
- expect(@user.last_name).to eq 'Neve'
10
- expect(@user.title).to be_nil
11
-
12
- I18n.with_locale(:en) do
13
- expect(@user.first_name).to eq 'Jon'
8
+ it "should return what the readme says it does" do
9
+ expect(@user.first_name).to eq 'Jean'
14
10
  expect(@user.last_name).to eq 'Neve'
15
11
  expect(@user.title).to be_nil
16
- end
17
12
 
18
- I18n.with_locale(:pt) do
19
- expect(@user.first_name).to eq 'Joao'
20
- expect(@user.last_name).to eq 'Neve'
21
- expect(@user.title).to eq 'Cavaleiro'
22
- end
13
+ I18n.with_locale(:en) do
14
+ expect(@user.first_name).to eq 'Jon'
15
+ expect(@user.last_name).to eq 'Neve'
16
+ expect(@user.title).to be_nil
17
+ end
23
18
 
24
- I18n.with_locale(:de) do
25
- expect(@user.first_name).to be_nil
26
- expect(@user.last_name).to eq 'Neve'
27
- expect(@user.title).to be_nil
28
- end
19
+ I18n.with_locale(:pt) do
20
+ expect(@user.first_name).to eq 'Joao'
21
+ expect(@user.last_name).to eq 'Neve'
22
+ expect(@user.title).to eq 'Cavaleiro'
23
+ end
29
24
 
30
- expect(@user.translate(:first_name, :fr)).to eq 'Jean'
31
- expect(@user.translate(:first_name, :en)).to eq 'Jon'
32
- expect(@user.translate(:first_name, :pt)).to eq 'Joao'
33
- expect(@user.translate(:first_name, :de)).to be_nil
25
+ I18n.with_locale(:de) do
26
+ expect(@user.first_name).to be_nil
27
+ expect(@user.last_name).to eq 'Neve'
28
+ expect(@user.title).to be_nil
29
+ end
34
30
 
35
- expect(@user.fr_first_name).to eq 'Jean'
36
- expect(@user.en_first_name).to eq 'Jon'
37
- expect(@user.pt_first_name).to eq 'Joao'
38
- expect(@user.de_first_name).to be_nil
39
- end
31
+ expect(@user.translate(:first_name, :fr)).to eq 'Jean'
32
+ expect(@user.translate(:first_name, :en)).to eq 'Jon'
33
+ expect(@user.translate(:first_name, :pt)).to eq 'Joao'
34
+ expect(@user.translate(:first_name, :de)).to be_nil
40
35
 
41
- context "when making use of setter translation methods" do
42
- before do
43
- @user.pt_first_name = 'João'
44
- @user.de_first_name = 'Hans'
36
+ expect(@user.fr_first_name).to eq 'Jean'
37
+ expect(@user.en_first_name).to eq 'Jon'
38
+ expect(@user.pt_first_name).to eq 'Joao'
39
+ expect(@user.de_first_name).to be_nil
45
40
  end
46
41
 
47
- it "#translation_data should be updated" do
48
- expect(@user.translation_data).to eq ({
49
- en: { first_name: 'Jon', last_name: 'Snow' },
50
- pt: { first_name: 'João' },
51
- de: { first_name: 'Hans' },
52
- fr: { first_name: 'Jean', last_name: 'Neige' }
53
- })
42
+ context "when making use of setter translation methods" do
43
+ before do
44
+ @user.pt_first_name = 'João'
45
+ @user.de_first_name = 'Hans'
46
+ end
47
+
48
+ it "#translation_data should be updated" do
49
+ expect(@user.translation_data).to eq ({
50
+ en: { first_name: 'Jon', last_name: 'Snow' },
51
+ pt: { first_name: 'João' },
52
+ de: { first_name: 'Hans' },
53
+ fr: { first_name: 'Jean', last_name: 'Neige' }
54
+ })
55
+ end
56
+
57
+ it "translation method should return the updated results" do
58
+ expect(@user.translate(:first_name, :fr)).to eq 'Jean'
59
+ expect(@user.translate(:first_name, :en)).to eq 'Jon'
60
+ expect(@user.translate(:first_name, :pt)).to eq 'João'
61
+ expect(@user.translate(:first_name, :de)).to eq 'Hans'
62
+ end
54
63
  end
55
64
 
56
- it "translation method should return the updated results" do
57
- expect(@user.translate(:first_name, :fr)).to eq 'Jean'
58
- expect(@user.translate(:first_name, :en)).to eq 'Jon'
59
- expect(@user.translate(:first_name, :pt)).to eq 'João'
60
- expect(@user.translate(:first_name, :de)).to eq 'Hans'
65
+ context "when the available_locales do not include :es" do
66
+ it "#es_first_name should NOT be defined" do
67
+ expect(@user.respond_to?(:es_first_name)).to be false
68
+ end
69
+
70
+ context "when :es gets introduced" do
71
+ before { I18n.available_locales.push(:es) }
72
+ after { I18n.available_locales.pop }
73
+
74
+ context "and .define_translation_methods as NOT ran" do
75
+ it "#es_first_name should NOT be defined" do
76
+ expect(@user.respond_to?(:es_first_name)).to be false
77
+ end
78
+ end
79
+
80
+ context "and .define_translation_methods AS ran" do
81
+ before { @user_class.define_translation_methods }
82
+
83
+ it "already defined method should work" do
84
+ expect(@user.fr_first_name).to eq 'Jean'
85
+ expect(@user.en_first_name).to eq 'Jon'
86
+ expect(@user.de_first_name).to be_nil
87
+ expect(@user.pt_first_name).to eq 'Joao'
88
+ end
89
+
90
+ it "#es_first_name SHOULD be defined" do
91
+ expect(@user.respond_to?(:es_first_name)).to be true
92
+ expect(@user.es_first_name).to be_nil
93
+ end
94
+ end
95
+ end
61
96
  end
62
97
  end
63
98
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lost_in_translations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - StreetBees Dev Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-08 00:00:00.000000000 Z
11
+ date: 2016-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry