lost_in_translations 1.3.0 → 1.3.1

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: 29ebb7845e6d024febce53bff8436ac9797c75b9
4
- data.tar.gz: aaffaef479abb13f14eb6c84de725c28f4af50c4
3
+ metadata.gz: 0704c23c3e9281b106f8ab586fbebbf0e26ece04
4
+ data.tar.gz: 0be875ce8e5f47fb0de2cf1535584d5ec9a14db3
5
5
  SHA512:
6
- metadata.gz: 657717a980be03b4eafadf654520d5aa464f2c39690f6fa055a15538978c29dbc341d340daf5b002a42977c42affdeae7b65b431752cd1d2e9655d2b4a32c171
7
- data.tar.gz: a437cb00356bc9b232f17cc4c1fc81ddd326c7b6674fe41a07ad7111482864d858c9d47d7e040c1d89a7b4d523d365adc3bba8e91231ea5a6c194a94e2d4ac88
6
+ metadata.gz: a61787435c7aba311e09dc7c0a9907345893089a1daef8a01619ca1071ddf5e385b217909e197f0c9182166939737739541162bce8935726a72c2d1db8ca1227
7
+ data.tar.gz: 2adae0cfdd8bf0dcce6e5678e24d3bffcae663cd67eb418d0287a25565f0df61989cda94ac58f9fc41c309eb34e23d669665e1777dd9b4d900e987a125385e2d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lost_in_translations (1.3.0)
4
+ lost_in_translations (1.3.1)
5
5
  i18n (~> 0.7)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # Lost In Translations
2
2
  Super light Translation Ruby Gem agnostic to your framework and source data
3
3
 
4
- [![Code Climate](https://codeclimate.com/github/Streetbees/lost_in_translations/badges/gpa.svg)](https://codeclimate.com/github/Streetbees/lost_in_translations)
5
- [![Test Coverage](https://codeclimate.com/github/Streetbees/lost_in_translations/badges/coverage.svg)](https://codeclimate.com/github/Streetbees/lost_in_translations/coverage)
6
- [![Build Status](https://travis-ci.org/Streetbees/lost_in_translations.svg?branch=master)](https://travis-ci.org/Streetbees/lost_in_translations)
4
+ [![Code Climate](https://codeclimate.com/github/Streetbees/lost-in-translations/badges/gpa.svg)](https://codeclimate.com/github/Streetbees/lost-in-translations)
5
+ [![Test Coverage](https://codeclimate.com/github/Streetbees/lost-in-translations/badges/coverage.svg)](https://codeclimate.com/github/Streetbees/lost-in-translations/coverage)
6
+ [![Build Status](https://travis-ci.org/Streetbees/lost-in-translations.svg?branch=master)](https://travis-ci.org/Streetbees/lost-in-translations)
7
7
 
8
8
  ## 1) Basic Usage
9
9
  ```ruby
@@ -13,50 +13,76 @@ class User < Struct.new(:title, :first_name, :last_name)
13
13
  translate :title, :first_name
14
14
 
15
15
  def translation_data
16
- {
16
+ @translation_data ||= {
17
17
  en: { first_name: 'Jon', last_name: 'Snow' },
18
18
  fr: { first_name: 'Jean', last_name: 'Neige' }
19
19
  }
20
20
  end
21
21
  end
22
22
  ```
23
- Class method ```.translate``` will redefine ```#title``` and ```#first_name``` instance methods in order to return the values that match the ```I18n.locale``` and the ```attribute name``` from the Hash returned by ```#translation_data```, otherwise calls the original redefined method.
23
+ Class method ```.translate``` will redefine ```#title``` and ```#first_name``` instance methods in order to return the values that match the ```I18n.locale``` and the ```attribute name``` from the Hash returned by ```#translation_data```.
24
24
 
25
25
  ```ruby
26
26
  @user = User.new('Cavaleiro', 'Joao', 'Neve')
27
27
 
28
+ I18n.default_locale = :pt
28
29
  I18n.locale = :fr
29
30
 
30
31
  @user.first_name # returns 'Jean'
31
32
  @user.last_name # returns 'Neve'
32
- @user.title # returns 'Cavaleiro'
33
+ @user.title # returns nil
33
34
 
34
35
  I18n.with_locale(:en) do
35
36
  @user.first_name # returns 'Jon'
36
37
  @user.last_name # returns 'Neve'
37
- @user.title # returns 'Cavaleiro'
38
+ @user.title # returns nil
38
39
  end
39
40
 
40
- I18n.with_locale(:de) do
41
+ I18n.with_locale(:pt) do
42
+ # there is no translation present but since locale
43
+ # matches the default_locale, the original value is returned
44
+
41
45
  @user.first_name # returns 'Joao'
42
46
  @user.last_name # returns 'Neve'
43
47
  @user.title # returns 'Cavaleiro'
44
48
  end
49
+
50
+ I18n.with_locale(:de) do
51
+ @user.first_name # returns nil
52
+ @user.last_name # returns 'Neve'
53
+ @user.title # returns nil
54
+ end
45
55
  ```
46
56
 
47
- Instance method ```#translate``` is also available:
57
+ The following instance methods are also available:
58
+ #### 1.1) ```#translate```
48
59
  ```ruby
49
60
  @user.translate(:first_name, :fr) # returns 'Jean'
50
61
  @user.translate(:first_name, :en) # returns 'Jon'
51
- @user.translate(:first_name, :de) # returns 'Joao'
62
+ @user.translate(:first_name, :pt) # returns 'Joao'
63
+ @user.translate(:first_name, :de) # returns nil
52
64
  ```
53
65
 
54
- And ```#<I18n.available_locales>_<translated_field>``` is also available:
66
+ #### 1.2) ```#<I18n.available_locales>_<translated_field>```
55
67
  ```ruby
56
68
  @user.fr_first_name # returns 'Jean'
57
69
  @user.en_first_name # returns 'Jon'
58
70
  @user.pt_first_name # returns 'Joao'
59
- @user.de_first_name # returns 'Joao'
71
+ @user.de_first_name # returns nil
72
+ ```
73
+
74
+ #### 1.3) ```#<I18n.available_locales>_<translated_field>=```
75
+ ```ruby
76
+ @user.pt_first_name = 'João'
77
+ @user.de_first_name = 'Hans'
78
+
79
+ @user.translation_data # will contain
80
+ # {
81
+ # en: { first_name: 'Jon', last_name: 'Snow' },
82
+ # pt: { first_name: 'João' },
83
+ # de: { first_name: 'Hans' },
84
+ # fr: { first_name: 'Jean', last_name: 'Neige' }
85
+ # }
60
86
  ```
61
87
 
62
88
  ## 2) Ideal usage
@@ -117,7 +143,7 @@ class User < ActiveRecord::Base
117
143
  translate :first_name
118
144
 
119
145
  def my_translation_data_field
120
- {
146
+ @my_translation_data_field ||= {
121
147
  en: { first_name: 'Jon', last_name: 'Snow' },
122
148
  fr: { first_name: 'Jean', last_name: 'Neige' }
123
149
  }
@@ -139,7 +165,7 @@ class User < ActiveRecord::Base
139
165
  self.translation_data_field = :my_translation_data_field
140
166
 
141
167
  def my_translation_data_field
142
- {
168
+ @my_translation_data_field ||= {
143
169
  en: { first_name: 'Jon', last_name: 'Snow' },
144
170
  fr: { first_name: 'Jean', last_name: 'Neige' }
145
171
  }
@@ -153,7 +179,7 @@ User.find(1).first_name # returns 'Jean'
153
179
 
154
180
  ### 3.3) Custom translation mechanism
155
181
  ```ruby
156
- class MyTranslator
182
+ class MyTranslator < Translator::Base
157
183
  def self.translate(object, field, locale)
158
184
  translations = #get_data_from_redis_or_yaml_file(object)
159
185
 
@@ -2,8 +2,8 @@ require 'i18n'
2
2
  require 'lost_in_translations/base'
3
3
  require 'lost_in_translations/ruby'
4
4
  require 'lost_in_translations/config'
5
- require 'lost_in_translations/translator'
6
5
  require 'lost_in_translations/active_record'
6
+ require 'lost_in_translations/translator/base'
7
7
 
8
8
  module LostInTranslations
9
9
 
@@ -17,17 +17,21 @@ module LostInTranslations
17
17
  end
18
18
 
19
19
  def self.config
20
- @config ||= Config.new('translation_data', Translator)
20
+ @config ||= Config.new('translation_data', Translator::Base)
21
21
  end
22
22
 
23
23
  def self.translate(*args)
24
24
  config.translator.translate(*args)
25
25
  end
26
26
 
27
+ def self.assign_translation(*args)
28
+ config.translator.assign_translation(*args)
29
+ end
30
+
27
31
  def self.define_translation_methods(object, *fields)
28
32
  fields.each do |field|
29
33
  define_dynamic_translation_method(object, field)
30
- define_particular_translation_method(object, field)
34
+ define_particular_translation_methods(object, field)
31
35
  end
32
36
  end
33
37
 
@@ -39,12 +43,16 @@ module LostInTranslations
39
43
  RUBY
40
44
  end
41
45
 
42
- def self.define_particular_translation_method(object, method_name)
46
+ def self.define_particular_translation_methods(object, method_name)
43
47
  I18n.available_locales.each do |locale|
44
48
  object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
45
49
  def #{locale}_#{method_name}
46
50
  translate(:#{method_name}, :#{locale})
47
51
  end
52
+
53
+ def #{locale}_#{method_name}=(value)
54
+ assign_translation(:#{method_name}, value, :#{locale})
55
+ end
48
56
  RUBY
49
57
  end
50
58
  end
@@ -10,16 +10,13 @@ module LostInTranslations
10
10
  end
11
11
 
12
12
  def self.included(base_class)
13
- base_class.send(:include, Base)
14
- base_class.extend ClassMethods
15
- end
16
-
17
- def translate(field, locale = I18n.locale)
18
- LostInTranslations.translate(self, field, locale) ||
19
- ActiveRecord.call_original_field(self, field)
13
+ base_class.class_eval do
14
+ include Base
15
+ extend ClassMethods
16
+ end
20
17
  end
21
18
 
22
- def self.call_original_field(object, field)
19
+ def call_original_field(object, field)
23
20
  object.read_attribute(field)
24
21
  end
25
22
 
@@ -5,6 +5,20 @@ module LostInTranslations
5
5
  base_class.extend ClassMethods
6
6
  end
7
7
 
8
+ def translate(field, locale = I18n.locale)
9
+ translation = LostInTranslations.translate(self, field, locale)
10
+
11
+ if translation.nil? && locale.to_sym == I18n.default_locale.to_sym
12
+ translation = call_original_field(self, field)
13
+ end
14
+
15
+ translation
16
+ end
17
+
18
+ def assign_translation(field, value, locale = I18n.locale)
19
+ LostInTranslations.assign_translation(self, field, value, locale)
20
+ end
21
+
8
22
  module ClassMethods
9
23
 
10
24
  attr_writer :translation_data_field
@@ -14,18 +14,13 @@ module LostInTranslations
14
14
  end
15
15
 
16
16
  def self.included(base_class)
17
- base_class.send(:include, Base)
18
- base_class.extend ClassMethods
19
- end
20
-
21
- include Base
22
-
23
- def translate(field, locale = I18n.locale)
24
- LostInTranslations.translate(self, field, locale) ||
25
- Ruby.call_original_field(self, field)
17
+ base_class.class_eval do
18
+ include Base
19
+ extend ClassMethods
20
+ end
26
21
  end
27
22
 
28
- def self.call_original_field(object, field)
23
+ def call_original_field(object, field)
29
24
  method_name = Ruby.original_field_name(field)
30
25
 
31
26
  return object.send(field) unless object.respond_to?(method_name)
@@ -0,0 +1,49 @@
1
+ module LostInTranslations
2
+ module Translator
3
+ class Base
4
+
5
+ def self.translate(object, field, locale)
6
+ data = translation_data(object)
7
+
8
+ translations = data[locale.to_sym] || data[locale.to_s] || {}
9
+
10
+ translations[field.to_sym] || translations[field.to_s]
11
+ end
12
+
13
+ def self.assign_translation(object, field, value, locale)
14
+ data = translation_data(object)
15
+
16
+ translations = data[locale.to_sym] || data[locale.to_s]
17
+
18
+ translations ||= data[locale.to_sym] = {}
19
+
20
+ translations[field.to_sym] = value
21
+ end
22
+
23
+ def self.translation_data(object)
24
+ translation_data_field = translation_data_field(object)
25
+
26
+ data = object.send(translation_data_field)
27
+
28
+ if data.nil? && object.respond_to?("#{translation_data_field}=")
29
+ data = object.send("#{translation_data_field}=", {})
30
+ end
31
+
32
+ data
33
+ end
34
+
35
+ def self.translation_data_field(object)
36
+ translation_data_field = object.class.translation_data_field
37
+
38
+ unless object.respond_to?(translation_data_field)
39
+ raise \
40
+ NotImplementedError,
41
+ "#{object.class.name} does not implement .#{translation_data_field}"
42
+ end
43
+
44
+ translation_data_field
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -1,5 +1,5 @@
1
1
  module LostInTranslations
2
2
 
3
- VERSION = '1.3.0'.freeze
3
+ VERSION = '1.3.1'.freeze
4
4
 
5
5
  end
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
12
12
  gem.email = 'dev@streetbees.com'
13
13
  gem.summary = 'Ruby Translation Gem'
14
14
  gem.description = 'Super light Translation Ruby Gem agnostic to your framework and source data'
15
- gem.homepage = 'https://github.com/streetbees/lost_in_translations'
15
+ gem.homepage = 'https://github.com/streetbees/lost-in-translations'
16
16
 
17
17
  gem.files = `git ls-files`.split($/)
18
18
  gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
@@ -2,34 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  describe LostInTranslations::ActiveRecord do
4
4
 
5
- describe '.translate' do
6
-
7
- context "when translating a field" do
8
- before do
9
- @user_class = Class.new(ActiveRecord::Base) do
10
- self.table_name = 'users'
11
-
12
- include LostInTranslations::ActiveRecord
13
-
14
- translate :title, :first_name
15
-
16
- def translation_data
17
- {
18
- en: { first_name: 'Jon', last_name: 'Snow' },
19
- fr: { first_name: 'Jean', last_name: 'Neige' }
20
- }
21
- end
22
- end
23
-
24
- @user_class.create title: 'Cavaleiro', first_name: 'Joao', last_name: 'Neve'
25
- @user = @user_class.first
26
- end
27
- after { @user.destroy }
28
-
29
- it_behaves_like "a proper translator"
30
- end
31
-
32
- context "when a particular field is not translated" do
5
+ describe 'readme example' do
6
+ context "basic usage" do
33
7
  before do
34
8
  @user_class = Class.new(ActiveRecord::Base) do
35
9
  self.table_name = 'users'
@@ -39,23 +13,74 @@ describe LostInTranslations::ActiveRecord do
39
13
  translate :title, :first_name
40
14
 
41
15
  def translation_data
42
- {
16
+ @translation_data ||= {
43
17
  en: { first_name: 'Jon', last_name: 'Snow' },
44
18
  fr: { first_name: 'Jean', last_name: 'Neige' }
45
19
  }
46
20
  end
47
21
  end
48
22
 
49
- @user = @user_class.new title: 'Cavaleiro', first_name: 'Joao', last_name: 'Neve'
23
+ @user = @user_class.new(title: 'Cavaleiro', first_name: 'Joao', last_name: 'Neve')
50
24
  end
51
25
 
52
- it "#field must return the original data" do
53
- I18n.with_locale(:en) do
54
- expect(@user.last_name).to eq 'Neve'
55
- end
56
- end
26
+ it_behaves_like "a basic usage"
57
27
  end
58
-
59
28
  end
60
29
 
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
85
+
61
86
  end
@@ -12,7 +12,7 @@ describe LostInTranslations::Base do
12
12
  self.translation_data_field = :translation_json
13
13
 
14
14
  def translation_json
15
- {
15
+ @translation_json ||= {
16
16
  en: { first_name: 'Jon', last_name: 'Snow' },
17
17
  fr: { first_name: 'Jean', last_name: 'Neige' }
18
18
  }
@@ -133,7 +133,7 @@ describe LostInTranslations do
133
133
  context "changing the source of the translation_data" do
134
134
  before do
135
135
  LostInTranslations.configure do |config|
136
- config.translator = Class.new(LostInTranslations::Translator) do
136
+ config.translator = Class.new(LostInTranslations::Translator::Base) do
137
137
  def self.translation_data(object)
138
138
  { en: { first_name: 'Jon', last_name: 'Snow' } }
139
139
  end
@@ -148,7 +148,7 @@ describe LostInTranslations do
148
148
 
149
149
  @user = @user_class.new('joao', 'neve')
150
150
  end
151
- after { LostInTranslations.config.translator = LostInTranslations::Translator }
151
+ after { LostInTranslations.config.translator = LostInTranslations::Translator::Base }
152
152
 
153
153
  it "calling a translated field must return a translation" do
154
154
  I18n.with_locale(:en) do
@@ -2,38 +2,16 @@ require 'spec_helper'
2
2
 
3
3
  describe LostInTranslations::Ruby do
4
4
 
5
- describe '.translate' do
6
-
7
- context "when translating a field" do
8
- before do
9
- @user_class = Struct.new(:title, :first_name, :last_name) do
10
- include LostInTranslations
11
-
12
- translate :title, :first_name
13
-
14
- def translation_data
15
- {
16
- en: { first_name: 'Jon', last_name: 'Snow' },
17
- fr: { first_name: 'Jean', last_name: 'Neige' }
18
- }
19
- end
20
- end
21
-
22
- @user = @user_class.new('Cavaleiro', 'Joao', 'Neve')
23
- end
24
-
25
- it_behaves_like "a proper translator"
26
- end
27
-
28
- context "when a particular field is not translated" do
5
+ describe 'readme example' do
6
+ context "basic usage" do
29
7
  before do
30
8
  @user_class = Struct.new(:title, :first_name, :last_name) do
31
- include LostInTranslations
9
+ include LostInTranslations::Ruby
32
10
 
33
11
  translate :title, :first_name
34
12
 
35
13
  def translation_data
36
- {
14
+ @translation_data ||= {
37
15
  en: { first_name: 'Jon', last_name: 'Snow' },
38
16
  fr: { first_name: 'Jean', last_name: 'Neige' }
39
17
  }
@@ -43,13 +21,58 @@ describe LostInTranslations::Ruby do
43
21
  @user = @user_class.new('Cavaleiro', 'Joao', 'Neve')
44
22
  end
45
23
 
46
- it "#field must return the original data" do
47
- I18n.with_locale(:en) do
48
- expect(@user.last_name).to eq 'Neve'
49
- end
50
- end
24
+ it_behaves_like "a basic usage"
51
25
  end
52
-
53
26
  end
54
27
 
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
77
+
55
78
  end
Binary file
@@ -0,0 +1,63 @@
1
+ # encoding: UTF-8
2
+
3
+ shared_examples_for "a basic usage" do
4
+ it "should behave like the readme says" do
5
+ I18n.default_locale = :pt
6
+ I18n.locale = :fr
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'
14
+ expect(@user.last_name).to eq 'Neve'
15
+ expect(@user.title).to be_nil
16
+ end
17
+
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
23
+
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
29
+
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
34
+
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
40
+
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'
45
+ end
46
+
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
+ })
54
+ end
55
+
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'
61
+ end
62
+ end
63
+ end
@@ -6,17 +6,17 @@ shared_examples_for "a proper translator" do
6
6
  end
7
7
 
8
8
  context "when the translation data doesn't contain the desired language" do
9
- it "#field must return the original data" do
9
+ it "#field must return nil" do
10
10
  I18n.with_locale(:de) do
11
- expect(@user.first_name).to eq 'Joao'
11
+ expect(@user.first_name).to be_nil
12
12
  end
13
13
  end
14
14
  end
15
15
 
16
16
  context "when the translation data doesn't contain the desired field" do
17
- it "#field must return the original data" do
17
+ it "#field must return nil" do
18
18
  I18n.with_locale(:en) do
19
- expect(@user.title).to eq 'Cavaleiro'
19
+ expect(@user.title).to be_nil
20
20
  end
21
21
  end
22
22
  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.0
4
+ version: 1.3.1
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-07 00:00:00.000000000 Z
11
+ date: 2016-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -157,7 +157,7 @@ files:
157
157
  - lib/lost_in_translations/base.rb
158
158
  - lib/lost_in_translations/config.rb
159
159
  - lib/lost_in_translations/ruby.rb
160
- - lib/lost_in_translations/translator.rb
160
+ - lib/lost_in_translations/translator/base.rb
161
161
  - lib/lost_in_translations/version.rb
162
162
  - lost_in_translations.gemspec
163
163
  - spec/lost_in_translations/active_record_spec.rb
@@ -166,8 +166,9 @@ files:
166
166
  - spec/lost_in_translations/ruby_spec.rb
167
167
  - spec/spec_helper.rb
168
168
  - spec/support/db/test.sqlite3
169
+ - spec/support/shared_examples/basic_usage.rb
169
170
  - spec/support/shared_examples/proper_translator.rb
170
- homepage: https://github.com/streetbees/lost_in_translations
171
+ homepage: https://github.com/streetbees/lost-in-translations
171
172
  licenses:
172
173
  - MIT
173
174
  metadata: {}
@@ -198,4 +199,5 @@ test_files:
198
199
  - spec/lost_in_translations/ruby_spec.rb
199
200
  - spec/spec_helper.rb
200
201
  - spec/support/db/test.sqlite3
202
+ - spec/support/shared_examples/basic_usage.rb
201
203
  - spec/support/shared_examples/proper_translator.rb
@@ -1,29 +0,0 @@
1
- module LostInTranslations
2
- class Translator
3
-
4
- def self.translate(object, field, locale)
5
- translations = translations_for(object, locale) || {}
6
-
7
- translations[field.to_sym] || translations[field.to_s]
8
- end
9
-
10
- def self.translations_for(object, locale)
11
- translations = translation_data(object) || {}
12
-
13
- translations[locale.to_sym] || translations[locale.to_s]
14
- end
15
-
16
- def self.translation_data(object)
17
- translation_data_field = object.class.translation_data_field
18
-
19
- unless object.respond_to?(translation_data_field)
20
- raise \
21
- NotImplementedError,
22
- "#{object.class.name} does not respond to .#{translation_data_field}"
23
- end
24
-
25
- object.send(translation_data_field)
26
- end
27
-
28
- end
29
- end