lost_in_translations 1.4.0 → 1.4.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: 06590098e42d3c39e2c1f2c0af0ece92d977e2bf
4
- data.tar.gz: dd7b741fdae089d107d8b06b844fef1d77b69ff3
3
+ metadata.gz: defec7298964d582430d90ead38dc56afd1c193b
4
+ data.tar.gz: 2b1dfb3fd71ef0a6392b9b9b75fe9094950880c6
5
5
  SHA512:
6
- metadata.gz: da5bad9a9d8b5ddf9a4e9e3c8224b57a30a8de73143d872236e7ff4643551a1ce56c71f1b9860348738aee4569b3887f152ccdd04c792d11ea8025b519a1bb94
7
- data.tar.gz: 7ce6782a647d366264d79bb4f2ce7999ca357699be4c6da628e8671bf2c3264b989bd02ba019f098e423c030690afaf86e7376fe435b6d42b7d69efc900e50e7
6
+ metadata.gz: ff1681c131c0e496a3c835b863c4199ac485ea5920f475ee5e072b95f03ddaa953ec7393377266969561f9f184c5bab47d2c1cb31aceff0137e4e74523281def
7
+ data.tar.gz: 4ba37ed6e568ed3b5d314c5bd2aea1dc291847c1a6d19ee6d4fc08058cb865d0e713ef22cf00db855f8dff490358a1e1579e7a0e41fef170f5567bf881a1ba3e
data/README.md CHANGED
@@ -4,6 +4,7 @@ Super light Translation Ruby Gem agnostic to your framework and source data
4
4
  [![Code Climate](https://codeclimate.com/github/Streetbees/lost-in-translations/badges/gpa.svg)](https://codeclimate.com/github/Streetbees/lost-in-translations)
5
5
  [![Test Coverage](https://codeclimate.com/github/Streetbees/lost-in-translations/badges/coverage.svg)](https://codeclimate.com/github/Streetbees/lost-in-translations/coverage)
6
6
  [![Build Status](https://travis-ci.org/Streetbees/lost-in-translations.svg?branch=master)](https://travis-ci.org/Streetbees/lost-in-translations)
7
+ [![Gem Version](https://badge.fury.io/rb/lost_in_translations.svg)](https://badge.fury.io/rb/lost_in_translations)
7
8
 
8
9
  ## 1) Basic Usage
9
10
  ```ruby
@@ -127,134 +128,28 @@ I18n.locale = :fr
127
128
  @user.title # returns nil
128
129
  ```
129
130
 
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)
131
+ ## 3) Instalation
148
132
 
149
- LostInTranslations.reload
150
-
151
- User.new.respond_to?(:pt_first_name) == true
152
- User.new.respond_to?(:en_first_name) == true
133
+ Add this to your Gemfile:
153
134
  ```
154
-
155
- ## 4) Configuration
156
-
157
- ### 4.1) Your "translation_data" method (in all of your objects) is not called "translation_data"
158
- ```ruby
159
- LostInTranslations.configure do |config|
160
- config.translation_data_field = 'my_translation_data_field'
161
- end
162
- ```
163
-
164
- ```ruby
165
- class User < ActiveRecord::Base
166
- include LostInTranslations
167
-
168
- translate :first_name
169
-
170
- def my_translation_data_field
171
- @my_translation_data_field ||= {
172
- en: { first_name: 'Jon', last_name: 'Snow' },
173
- fr: { first_name: 'Jean', last_name: 'Neige' }
174
- }
175
- end
176
- end
177
-
178
- I18n.locale = :fr
179
-
180
- User.find(1).first_name # returns 'Jean'
181
- ```
182
-
183
- ### 4.2) Your "translation_data" method (in a particular object) is not called "translation_data"
184
- ```ruby
185
- class User < ActiveRecord::Base
186
- include LostInTranslations
187
-
188
- translate :first_name
189
-
190
- self.translation_data_field = :my_translation_data_field
191
-
192
- def my_translation_data_field
193
- @my_translation_data_field ||= {
194
- en: { first_name: 'Jon', last_name: 'Snow' },
195
- fr: { first_name: 'Jean', last_name: 'Neige' }
196
- }
197
- end
198
- end
199
-
200
- I18n.locale = :fr
201
-
202
- User.find(1).first_name # returns 'Jean'
135
+ gem 'lost_in_translations'
203
136
  ```
204
137
 
205
- ### 4.3) Custom translation mechanism
206
- ```ruby
207
- class MyTranslator < Translator::Base
208
- def self.translation_data(object)
209
- # get_data_from_redis_or_yaml_file(object)
210
- end
211
- end
212
- ```
138
+ And then execute:
213
139
 
214
- ```ruby
215
- LostInTranslations.configure do |config|
216
- config.translator = MyTranslator
217
- end
218
140
  ```
219
-
220
- ```ruby
221
- class User < ActiveRecord::Base
222
- include LostInTranslations
223
-
224
- translate :first_name
225
- end
226
-
227
- I18n.locale = :fr
228
-
229
- User.find(1).first_name # returns 'Jean' from redis or yaml file
141
+ $> bundle install
230
142
  ```
231
143
 
232
- Don't forget that you can do the same at the object level
144
+ In Rails, create the file **config/initializers/lost_in_translations.rb** with:
233
145
  ```ruby
234
- class User < ActiveRecord::Base
235
- include LostInTranslations
236
-
237
- translate :first_name
238
-
239
- def translation_data
240
- # get_data_from_redis_or_yaml_file(self)
241
- end
242
- end
243
-
244
- I18n.locale = :fr
245
-
246
- User.find(1).first_name # returns 'Jean' from redis or yaml file
247
- ```
248
-
249
- ## 5) Instalation
250
-
251
- Add your application's Gemfile:
252
- ```
253
- gem 'lost_in_translations'
146
+ LostInTranslations.reload
254
147
  ```
148
+ Because Rails starts with a set of **I18n.available_locales** different from when it finished loading and **LostInTranslations** needs to redefine new methods for those new locales in your classes.
255
149
 
256
- And then execute:
150
+ ## 4) F.A.Q.
257
151
 
258
- ```
259
- $> bundle install
260
- ```
152
+ - [I18n.available_locales as changed, how do I recreate the new translation methods?](https://github.com/Streetbees/lost-in-translations/wiki/Redefining-translation-methods)
153
+ - [Your "translation_data" method (in all of your objects) is not called "translation_data"](https://github.com/Streetbees/lost-in-translations/wiki/translation_data-configuration-version-1)
154
+ - [Your "translation_data" method (in a particular object) is not called "translation_data"](https://github.com/Streetbees/lost-in-translations/wiki/translation_data-configuration-version-2)
155
+ - [Custom translation mechanism](https://github.com/Streetbees/lost-in-translations/wiki/Custom-translation-mechanism)
@@ -1,14 +1,5 @@
1
1
  module LostInTranslations
2
2
  module ActiveRecord
3
-
4
- module ClassMethods
5
-
6
- def define_translation_methods
7
- LostInTranslations.define_translation_methods self, *translation_fields
8
- end
9
-
10
- end
11
-
12
3
  def self.included(base_class)
13
4
  base_class.send(:include, Base)
14
5
  base_class.extend ClassMethods
@@ -18,5 +9,10 @@ module LostInTranslations
18
9
  object.read_attribute(field)
19
10
  end
20
11
 
12
+ module ClassMethods
13
+ def define_translation_methods
14
+ LostInTranslations.define_translation_methods self, *translation_fields
15
+ end
16
+ end
21
17
  end
22
18
  end
@@ -1,6 +1,5 @@
1
1
  module LostInTranslations
2
2
  module Base
3
-
4
3
  def self.included(base_class)
5
4
  LostInTranslations.infected_classes.push(base_class)
6
5
 
@@ -22,7 +21,6 @@ module LostInTranslations
22
21
  end
23
22
 
24
23
  module ClassMethods
25
-
26
24
  attr_writer :translation_data_field
27
25
 
28
26
  def translation_data_field
@@ -39,8 +37,6 @@ module LostInTranslations
39
37
 
40
38
  define_translation_methods
41
39
  end
42
-
43
40
  end
44
-
45
41
  end
46
42
  end
@@ -1,5 +1,3 @@
1
1
  module LostInTranslations
2
-
3
2
  Config = Struct.new(:translation_data_field, :translator)
4
-
5
3
  end
@@ -1,22 +1,5 @@
1
1
  module LostInTranslations
2
2
  module Ruby
3
-
4
- module ClassMethods
5
-
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
13
- end
14
-
15
- LostInTranslations.define_translation_methods self, *translation_fields
16
- end
17
-
18
- end
19
-
20
3
  def self.included(base_class)
21
4
  base_class.send(:include, Base)
22
5
  base_class.extend ClassMethods
@@ -34,5 +17,18 @@ module LostInTranslations
34
17
  "original_field_#{field}".to_sym
35
18
  end
36
19
 
20
+ module ClassMethods
21
+ def define_translation_methods
22
+ translation_fields.each do |field|
23
+ next if instance_methods.include?(Ruby.original_field_name(field))
24
+
25
+ class_eval do
26
+ alias_method Ruby.original_field_name(field), field.to_sym
27
+ end
28
+ end
29
+
30
+ LostInTranslations.define_translation_methods self, *translation_fields
31
+ end
32
+ end
37
33
  end
38
34
  end
@@ -1,7 +1,6 @@
1
1
  module LostInTranslations
2
2
  module Translator
3
3
  class Base
4
-
5
4
  def self.translate(object, field, locale)
6
5
  data = translation_data(object)
7
6
 
@@ -43,7 +42,6 @@ module LostInTranslations
43
42
 
44
43
  translation_data_field
45
44
  end
46
-
47
45
  end
48
46
  end
49
47
  end
@@ -1,5 +1,3 @@
1
1
  module LostInTranslations
2
-
3
- VERSION = '1.4.0'.freeze
4
-
2
+ VERSION = '1.4.1'.freeze
5
3
  end
@@ -6,7 +6,6 @@ require 'lost_in_translations/active_record'
6
6
  require 'lost_in_translations/translator/base'
7
7
 
8
8
  module LostInTranslations
9
-
10
9
  def self.included(base_class)
11
10
  if defined?(::ActiveRecord::Base) &&
12
11
  base_class.ancestors.include?(::ActiveRecord::Base)
@@ -47,20 +46,26 @@ module LostInTranslations
47
46
  RUBY
48
47
  end
49
48
 
50
- def self.define_particular_translation_methods(object, method_name)
49
+ def self.define_particular_translation_methods(object, attribute)
51
50
  I18n.available_locales.each do |locale|
52
- object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
53
- def #{locale}_#{method_name}
54
- translate(:#{method_name}, :#{locale})
55
- end
51
+ method_name = "#{locale.to_s.downcase.tr('-', '_')}_#{attribute}"
56
52
 
57
- def #{locale}_#{method_name}=(value)
58
- assign_translation(:#{method_name}, value, :#{locale})
59
- end
60
- RUBY
53
+ define_translation_method(object, method_name, attribute, locale)
61
54
  end
62
55
  end
63
56
 
57
+ def self.define_translation_method(object, method_name, attribute, locale)
58
+ object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
59
+ def #{method_name}
60
+ translate(:#{attribute}, :'#{locale}')
61
+ end
62
+
63
+ def #{method_name}=(value)
64
+ assign_translation(:#{attribute}, value, :'#{locale}')
65
+ end
66
+ RUBY
67
+ end
68
+
64
69
  def self.configure
65
70
  yield(config)
66
71
  end
@@ -72,5 +77,4 @@ module LostInTranslations
72
77
  def self.reload
73
78
  infected_classes.each(&:define_translation_methods)
74
79
  end
75
-
76
80
  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.4.0
4
+ version: 1.4.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-20 00:00:00.000000000 Z
11
+ date: 2016-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -143,15 +143,7 @@ executables: []
143
143
  extensions: []
144
144
  extra_rdoc_files: []
145
145
  files:
146
- - ".gitignore"
147
- - ".rspec"
148
- - ".rubocop.yml"
149
- - ".ruby-version"
150
- - ".travis.yml"
151
- - Gemfile
152
- - Gemfile.lock
153
146
  - README.md
154
- - Rakefile
155
147
  - lib/lost_in_translations.rb
156
148
  - lib/lost_in_translations/active_record.rb
157
149
  - lib/lost_in_translations/base.rb
@@ -159,15 +151,6 @@ files:
159
151
  - lib/lost_in_translations/ruby.rb
160
152
  - lib/lost_in_translations/translator/base.rb
161
153
  - lib/lost_in_translations/version.rb
162
- - lost_in_translations.gemspec
163
- - spec/lost_in_translations/active_record_spec.rb
164
- - spec/lost_in_translations/base_spec.rb
165
- - spec/lost_in_translations/lost_in_translations_spec.rb
166
- - spec/lost_in_translations/ruby_spec.rb
167
- - spec/spec_helper.rb
168
- - spec/support/db/test.sqlite3
169
- - spec/support/shared_examples/basic_usage.rb
170
- - spec/support/shared_examples/proper_translator.rb
171
154
  homepage: https://github.com/streetbees/lost-in-translations
172
155
  licenses:
173
156
  - MIT
@@ -192,12 +175,4 @@ rubygems_version: 2.5.1
192
175
  signing_key:
193
176
  specification_version: 4
194
177
  summary: Ruby Translation Gem
195
- test_files:
196
- - spec/lost_in_translations/active_record_spec.rb
197
- - spec/lost_in_translations/base_spec.rb
198
- - spec/lost_in_translations/lost_in_translations_spec.rb
199
- - spec/lost_in_translations/ruby_spec.rb
200
- - spec/spec_helper.rb
201
- - spec/support/db/test.sqlite3
202
- - spec/support/shared_examples/basic_usage.rb
203
- - spec/support/shared_examples/proper_translator.rb
178
+ test_files: []
data/.gitignore DELETED
@@ -1,50 +0,0 @@
1
- *.gem
2
- *.rbc
3
- /.config
4
- /coverage/
5
- /InstalledFiles
6
- /pkg/
7
- /spec/reports/
8
- /spec/examples.txt
9
- /test/tmp/
10
- /test/version_tmp/
11
- /tmp/
12
-
13
- ## Specific to RubyMotion:
14
- .dat*
15
- .repl_history
16
- build/
17
-
18
- ## Documentation cache and generated files:
19
- /.yardoc/
20
- /_yardoc/
21
- /doc/
22
- /rdoc/
23
-
24
- ## Environment normalization:
25
- /.bundle/
26
- /vendor/bundle
27
- /lib/bundler/man/
28
-
29
- # for a library or gem, you might want to ignore these files since the code is
30
- # intended to run in multiple environments; otherwise, check them in:
31
- # Gemfile.lock
32
- # .ruby-version
33
- .ruby-gemset
34
-
35
- # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
- .rvmrc
37
-
38
- *.bundle
39
- *.so
40
- *.o
41
- *.a
42
- mkmf.log
43
-
44
- .DS_Store
45
-
46
- # Jetbrains Idea files
47
- .idea/
48
- api.iml
49
-
50
- db/schema.rb
data/.rspec DELETED
@@ -1,4 +0,0 @@
1
- --tty
2
- --color
3
- --format documentation
4
- --format html -o "tmp/rspec_result.html"
data/.rubocop.yml DELETED
@@ -1,19 +0,0 @@
1
- Style/EmptyLinesAroundClassBody:
2
- Enabled: false
3
-
4
- Style/EmptyLinesAroundModuleBody:
5
- Enabled: false
6
-
7
- Style/FrozenStringLiteralComment:
8
- Enabled: false
9
-
10
- AllCops:
11
- DisplayCopNames: true
12
- Exclude:
13
- - 'spec/**/*'
14
- - 'Gemfile*'
15
- - '*.gemspec'
16
- - 'vendor/**/*'
17
-
18
- Documentation:
19
- Enabled: false
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.3.0
data/.travis.yml DELETED
@@ -1,4 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 1.9.3
4
- - 2.3.0
data/Gemfile DELETED
@@ -1,3 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
data/Gemfile.lock DELETED
@@ -1,91 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- lost_in_translations (1.4.0)
5
- i18n (~> 0.7)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activemodel (4.2.6)
11
- activesupport (= 4.2.6)
12
- builder (~> 3.1)
13
- activerecord (4.2.6)
14
- activemodel (= 4.2.6)
15
- activesupport (= 4.2.6)
16
- arel (~> 6.0)
17
- activesupport (4.2.6)
18
- i18n (~> 0.7)
19
- json (~> 1.7, >= 1.7.7)
20
- minitest (~> 5.1)
21
- thread_safe (~> 0.3, >= 0.3.4)
22
- tzinfo (~> 1.1)
23
- arel (6.0.3)
24
- ast (2.2.0)
25
- builder (3.2.2)
26
- codeclimate-test-reporter (0.4.8)
27
- simplecov (>= 0.7.1, < 1.0.0)
28
- coderay (1.1.1)
29
- diff-lcs (1.2.5)
30
- docile (1.1.5)
31
- i18n (0.7.0)
32
- json (1.8.3)
33
- method_source (0.8.2)
34
- minitest (5.9.0)
35
- parser (2.3.0.6)
36
- ast (~> 2.2)
37
- powerpack (0.1.1)
38
- pry (0.10.3)
39
- coderay (~> 1.1.0)
40
- method_source (~> 0.8.1)
41
- slop (~> 3.4)
42
- rainbow (2.1.0)
43
- rake (11.2.2)
44
- rspec (3.4.0)
45
- rspec-core (~> 3.4.0)
46
- rspec-expectations (~> 3.4.0)
47
- rspec-mocks (~> 3.4.0)
48
- rspec-core (3.4.4)
49
- rspec-support (~> 3.4.0)
50
- rspec-expectations (3.4.0)
51
- diff-lcs (>= 1.2.0, < 2.0)
52
- rspec-support (~> 3.4.0)
53
- rspec-mocks (3.4.1)
54
- diff-lcs (>= 1.2.0, < 2.0)
55
- rspec-support (~> 3.4.0)
56
- rspec-support (3.4.1)
57
- rubocop (0.37.2)
58
- parser (>= 2.3.0.4, < 3.0)
59
- powerpack (~> 0.1)
60
- rainbow (>= 1.99.1, < 3.0)
61
- ruby-progressbar (~> 1.7)
62
- unicode-display_width (~> 0.3)
63
- ruby-progressbar (1.7.5)
64
- simplecov (0.11.2)
65
- docile (~> 1.1.0)
66
- json (~> 1.8)
67
- simplecov-html (~> 0.10.0)
68
- simplecov-html (0.10.0)
69
- slop (3.6.0)
70
- sqlite3 (1.3.11)
71
- thread_safe (0.3.5)
72
- tzinfo (1.2.2)
73
- thread_safe (~> 0.1)
74
- unicode-display_width (0.3.1)
75
-
76
- PLATFORMS
77
- ruby
78
-
79
- DEPENDENCIES
80
- activerecord (= 4.2.6)
81
- codeclimate-test-reporter (= 0.4.8)
82
- lost_in_translations!
83
- pry (= 0.10.3)
84
- rake (= 11.2.2)
85
- rspec (= 3.4.0)
86
- rubocop (= 0.37.2)
87
- simplecov (= 0.11.2)
88
- sqlite3 (= 1.3.11)
89
-
90
- BUNDLED WITH
91
- 1.12.5
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- require 'bundler/gem_tasks'
2
-
3
- require 'rspec/core/rake_task'
4
- require 'rubocop/rake_task'
5
-
6
- RSpec::Core::RakeTask.new
7
- RuboCop::RakeTask.new
8
-
9
- task(:default).clear
10
- task default: [:rubocop, :spec]
@@ -1,32 +0,0 @@
1
- lib = File.expand_path('../lib', __FILE__)
2
-
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
-
5
- require 'lost_in_translations/version'
6
-
7
- Gem::Specification.new do |gem|
8
- gem.name = 'lost_in_translations'
9
- gem.version = LostInTranslations::VERSION
10
- gem.license = 'MIT'
11
- gem.authors = ['StreetBees Dev Team']
12
- gem.email = 'dev@streetbees.com'
13
- gem.summary = 'Ruby Translation Gem'
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'
16
-
17
- gem.files = `git ls-files`.split($/)
18
- gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
19
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
- gem.require_paths = ['lib']
21
-
22
- gem.add_development_dependency 'pry', '0.10.3'
23
- gem.add_development_dependency 'rake', '11.2.2'
24
- gem.add_development_dependency 'rspec', '3.4.0'
25
- gem.add_development_dependency 'sqlite3', '1.3.11'
26
- gem.add_development_dependency 'rubocop', '0.37.2'
27
- gem.add_development_dependency 'simplecov', '0.11.2'
28
- gem.add_development_dependency 'activerecord', '4.2.6'
29
- gem.add_development_dependency 'codeclimate-test-reporter', '0.4.8'
30
-
31
- gem.add_dependency 'i18n', '~> 0.7'
32
- end
@@ -1,51 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe LostInTranslations::ActiveRecord do
4
-
5
- context "when building a resource" do
6
- before do
7
- @user_class = Class.new(ActiveRecord::Base) do
8
- self.table_name = 'users'
9
-
10
- include LostInTranslations::ActiveRecord
11
-
12
- translate :title, :first_name
13
-
14
- def translation_data
15
- @translation_data ||= {
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(title: 'Cavaleiro', first_name: 'Joao', last_name: 'Neve')
23
- end
24
-
25
- it_behaves_like "the readme example"
26
- end
27
-
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
50
-
51
- end
@@ -1,70 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe LostInTranslations::Base do
4
-
5
- describe '.translation_data_field =' do
6
-
7
- context "when it points to a real method" do
8
- before do
9
- @user_class = Struct.new(:first_name, :last_name) do
10
- include LostInTranslations::Base
11
-
12
- self.translation_data_field = :translation_json
13
-
14
- def translation_json
15
- @translation_json ||= {
16
- en: { first_name: 'Jon', last_name: 'Snow' },
17
- fr: { first_name: 'Jean', last_name: 'Neige' }
18
- }
19
- end
20
- end
21
-
22
- LostInTranslations.define_translation_methods(@user_class, :first_name)
23
-
24
- @user = @user_class.new('Joao', 'Neve')
25
- end
26
-
27
- it "LostInTranslations.translate must return a translation" do
28
- expect(LostInTranslations.translate(@user, :first_name, :en)).to eq 'Jon'
29
- end
30
-
31
- it "calling a field not translated, must return the original data" do
32
- I18n.with_locale(:en) do
33
- expect(@user.last_name).to eq 'Neve'
34
- end
35
- end
36
-
37
- context "and the translation data doesn't contain results" do
38
- it "LostInTranslations.translate must return nil" do
39
- expect(LostInTranslations.translate(@user, :first_name, :de)).to be_nil
40
- end
41
- end
42
- end
43
-
44
- context "when it DOENS't point to a real method" do
45
- before do
46
- @user_class = Struct.new(:first_name, :last_name) do
47
- include LostInTranslations::Base
48
-
49
- self.translation_data_field = :translation_json
50
- end
51
-
52
- LostInTranslations.define_translation_methods(@user_class, :first_name)
53
-
54
- @user = @user_class.new('Joao', 'Neve')
55
- end
56
-
57
- it "LostInTranslations.translate must raise an error" do
58
- expect { LostInTranslations.translate(@user, :first_name, :en) }.to raise_error(NotImplementedError)
59
- end
60
-
61
- it "calling a field not translated, must return the original data" do
62
- I18n.with_locale(:en) do
63
- expect(@user.last_name).to eq 'Neve'
64
- end
65
- end
66
- end
67
-
68
- end
69
-
70
- end
@@ -1,232 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe LostInTranslations do
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
-
66
- describe "#define_translation_methods" do
67
- context "when the passed methods do not exist" do
68
- before do
69
- @user_class = Struct.new(:first_name, :last_name) do
70
- include LostInTranslations
71
- end
72
-
73
- LostInTranslations.define_translation_methods(@user_class, :unknown)
74
-
75
- @user = @user_class.new('Joao', 'Neve')
76
- end
77
-
78
- it "#unknown should be defined" do
79
- expect(@user.respond_to?(:unknown)).to be true
80
- end
81
-
82
- it "#<I18n.available_locales>_unknown should be defined" do
83
- I18n.available_locales.each do |locale|
84
- expect(@user.respond_to?("#{locale}_unknown")).to be true
85
- end
86
- end
87
- end
88
-
89
- context "when the passed methods do exist" do
90
- before do
91
- @user_class = Struct.new(:first_name, :last_name) do
92
- include LostInTranslations
93
- end
94
-
95
- LostInTranslations.define_translation_methods(@user_class, :first_name)
96
-
97
- @user = @user_class.new('Joao', 'Neve')
98
- end
99
-
100
- it "#first_name should be defined" do
101
- expect(@user.respond_to?(:first_name)).to be true
102
- end
103
-
104
- it "#<I18n.available_locales>_first_name should be defined" do
105
- I18n.available_locales.each do |locale|
106
- expect(@user.respond_to?("#{locale}_first_name")).to be true
107
- end
108
- end
109
- end
110
- end
111
-
112
- describe "#included" do
113
- context "when the object inherits from ActiveRecord" do
114
- before do
115
- @user_class = Class.new(ActiveRecord::Base) do
116
- self.table_name = 'users'
117
-
118
- include LostInTranslations
119
- end
120
- end
121
-
122
- it "LostInTranslations::ActiveRecord must be included" do
123
- expect(@user_class.ancestors.include?(LostInTranslations::ActiveRecord)).to be true
124
- end
125
- end
126
-
127
- context "when the object DOES NOT inherit from ActiveRecord" do
128
- before do
129
- @user_class = Class.new { include LostInTranslations }
130
- end
131
-
132
- it "LostInTranslations::Ruby must be included" do
133
- expect(@user_class.ancestors.include?(LostInTranslations::Ruby)).to be true
134
- end
135
- end
136
- end
137
-
138
- describe "#translation_data" do
139
-
140
- context "when the object.translation_data is nil" do
141
- before do
142
- @user_class = Struct.new(:first_name, :last_name) do
143
- include LostInTranslations
144
-
145
- translate :first_name
146
-
147
- attr_accessor :translation_data
148
- end
149
-
150
- @user = @user_class.new('joao', 'neve')
151
- end
152
-
153
- it "should return an empty Hash" do
154
- expect(LostInTranslations.translation_data(@user)).to eq({})
155
- expect(@user.translation_data).to eq({})
156
- end
157
- end
158
-
159
- context "when setting the #config.translation_data_field to a known method" do
160
- before do
161
- LostInTranslations.configure do |config|
162
- config.translation_data_field = 'translation_json'
163
- end
164
-
165
- @user_class = Struct.new(:first_name, :last_name) do
166
- include LostInTranslations
167
-
168
- translate :first_name
169
-
170
- def translation_json
171
- { en: { first_name: 'Jon', last_name: 'Snow' } }
172
- end
173
- end
174
-
175
- @user = @user_class.new('joao', 'neve')
176
- end
177
- after { LostInTranslations.config.translation_data_field = 'translation_data' }
178
-
179
- it "should return the known method results" do
180
- expect(LostInTranslations.translation_data(@user)).to eq({ en: { first_name: 'Jon', last_name: 'Snow' } })
181
- end
182
- end
183
-
184
- context "when setting the #config.translation_data_field to an unknown method" do
185
- before do
186
- LostInTranslations.configure do |config|
187
- config.translation_data_field = 'translation_json'
188
- end
189
-
190
- @user_class = Struct.new(:first_name, :last_name) do
191
- include LostInTranslations
192
-
193
- translate :first_name
194
- end
195
-
196
- @user = @user_class.new('joao', 'neve')
197
- end
198
- after { LostInTranslations.config.translation_data_field = 'translation_data' }
199
-
200
- it "should raise an error" do
201
- expect { LostInTranslations.translation_data(@user) }.to raise_error(NotImplementedError)
202
- end
203
- end
204
-
205
- context "when changing the #config.translator" do
206
- before do
207
- LostInTranslations.configure do |config|
208
- config.translator = Class.new(LostInTranslations::Translator::Base) do
209
- def self.translation_data(object)
210
- { en: { first_name: 'Jon', last_name: 'Snow' } }
211
- end
212
- end
213
- end
214
-
215
- @user_class = Struct.new(:first_name, :last_name) do
216
- include LostInTranslations
217
-
218
- translate :first_name
219
- end
220
-
221
- @user = @user_class.new('joao', 'neve')
222
- end
223
- after { LostInTranslations.config.translator = LostInTranslations::Translator::Base }
224
-
225
- it "should return the known method results" do
226
- expect(LostInTranslations.translation_data(@user)).to eq({ en: { first_name: 'Jon', last_name: 'Snow' } })
227
- end
228
- end
229
-
230
- end
231
-
232
- end
@@ -1,26 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe LostInTranslations::Ruby do
4
-
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
- }
17
- end
18
- end
19
-
20
- @user = @user_class.new('Cavaleiro', 'Joao', 'Neve')
21
- end
22
-
23
- it_behaves_like "the readme example"
24
- end
25
-
26
- end
data/spec/spec_helper.rb DELETED
@@ -1,35 +0,0 @@
1
- require "codeclimate-test-reporter"
2
-
3
- CodeClimate::TestReporter.start
4
-
5
- require "simplecov"
6
-
7
- SimpleCov.start do
8
- root("lib/")
9
- coverage_dir("../tmp/coverage/")
10
- end
11
-
12
- $: << File.expand_path('../', File.dirname(__FILE__))
13
-
14
- require 'pry'
15
- require 'active_record'
16
- require 'lost_in_translations'
17
-
18
- I18n.available_locales = [:en, :pt, :fr, :de]
19
- I18n.default_locale = :pt
20
-
21
- ActiveRecord::Base.establish_connection \
22
- pool: 5,
23
- timeout: 5000,
24
- adapter: 'sqlite3',
25
- database: File.expand_path('../spec/support/db/test.sqlite3', File.dirname(__FILE__))
26
-
27
- Dir["./spec/**/support/**/*.rb"].each do |file|
28
- require file
29
- end
30
-
31
- RSpec.configure do |config|
32
- config.run_all_when_everything_filtered = true
33
-
34
- config.order = 'random'
35
- end
Binary file
@@ -1,98 +0,0 @@
1
- # encoding: UTF-8
2
-
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
-
8
- it "should return what the readme says it does" do
9
- expect(@user.first_name).to eq 'Jean'
10
- expect(@user.last_name).to eq 'Neve'
11
- expect(@user.title).to be_nil
12
-
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
18
-
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
24
-
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
30
-
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
35
-
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
40
- end
41
-
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
63
- end
64
-
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
96
- end
97
- end
98
- end
@@ -1,23 +0,0 @@
1
- shared_examples_for "a proper translator" do
2
- it "#field must return a translation" do
3
- I18n.with_locale(:en) do
4
- expect(@user.first_name).to eq 'Jon'
5
- end
6
- end
7
-
8
- context "when the translation data doesn't contain the desired language" do
9
- it "#field must return nil" do
10
- I18n.with_locale(:de) do
11
- expect(@user.first_name).to be_nil
12
- end
13
- end
14
- end
15
-
16
- context "when the translation data doesn't contain the desired field" do
17
- it "#field must return nil" do
18
- I18n.with_locale(:en) do
19
- expect(@user.title).to be_nil
20
- end
21
- end
22
- end
23
- end