lost_in_translations 1.2.0 → 1.3.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: bcda78e45d1191420fb5863127b77c5315fa7b46
4
- data.tar.gz: 6cdf234915545512aac5e751a692986454fcfe27
3
+ metadata.gz: 29ebb7845e6d024febce53bff8436ac9797c75b9
4
+ data.tar.gz: aaffaef479abb13f14eb6c84de725c28f4af50c4
5
5
  SHA512:
6
- metadata.gz: 55dd44bafc0005398321643ca0b44cef0ede2dd18365735809aceeeb06acec6e0a0b8201eba40380d2fe0b552738d317633ae9a3d8f6699fd667ba1e817f0bc2
7
- data.tar.gz: c73c4f621cf6dc45e67fe3ba2901319604dbdb12ff481c01a8600edced13100a8c0d55d4e8e9a97aa1f7df8a3fd1d83851e1271d04a953251f1c0a3e56c050f8
6
+ metadata.gz: 657717a980be03b4eafadf654520d5aa464f2c39690f6fa055a15538978c29dbc341d340daf5b002a42977c42affdeae7b65b431752cd1d2e9655d2b4a32c171
7
+ data.tar.gz: a437cb00356bc9b232f17cc4c1fc81ddd326c7b6674fe41a07ad7111482864d858c9d47d7e040c1d89a7b4d523d365adc3bba8e91231ea5a6c194a94e2d4ac88
data/.rubocop.yml CHANGED
@@ -10,6 +10,7 @@ AllCops:
10
10
  - 'spec/**/*'
11
11
  - 'Gemfile*'
12
12
  - '*.gemspec'
13
+ - 'vendor/**/*'
13
14
 
14
15
  Documentation:
15
16
  Enabled: false
data/.travis.yml CHANGED
@@ -1,5 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
- - 2.1.0
5
4
  - 2.3.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lost_in_translations (1.2.0)
4
+ lost_in_translations (1.3.0)
5
5
  i18n (~> 0.7)
6
6
 
7
7
  GEM
@@ -23,7 +23,6 @@ GEM
23
23
  arel (6.0.3)
24
24
  ast (2.2.0)
25
25
  builder (3.2.2)
26
- byebug (8.2.5)
27
26
  codeclimate-test-reporter (0.4.8)
28
27
  simplecov (>= 0.7.1, < 1.0.0)
29
28
  coderay (1.1.1)
@@ -40,10 +39,8 @@ GEM
40
39
  coderay (~> 1.1.0)
41
40
  method_source (~> 0.8.1)
42
41
  slop (~> 3.4)
43
- pry-byebug (3.3.0)
44
- byebug (~> 8.0)
45
- pry (~> 0.10)
46
42
  rainbow (2.1.0)
43
+ rake (11.2.2)
47
44
  rspec (3.4.0)
48
45
  rspec-core (~> 3.4.0)
49
46
  rspec-expectations (~> 3.4.0)
@@ -81,17 +78,14 @@ PLATFORMS
81
78
 
82
79
  DEPENDENCIES
83
80
  activerecord (= 4.2.6)
84
- byebug (= 8.2.5)
85
81
  codeclimate-test-reporter (= 0.4.8)
86
82
  lost_in_translations!
87
- pry-byebug (= 3.3.0)
83
+ pry (= 0.10.3)
84
+ rake (= 11.2.2)
88
85
  rspec (= 3.4.0)
89
86
  rubocop (= 0.37.2)
90
87
  simplecov (= 0.11.2)
91
88
  sqlite3 (= 1.3.11)
92
89
 
93
- RUBY VERSION
94
- ruby 2.3.0p0
95
-
96
90
  BUNDLED WITH
97
91
  1.12.5
data/README.md CHANGED
@@ -5,7 +5,7 @@ Super light Translation Ruby Gem agnostic to your framework and source data
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
7
 
8
- ## 1. Basic Usage
8
+ ## 1) Basic Usage
9
9
  ```ruby
10
10
  class User < Struct.new(:title, :first_name, :last_name)
11
11
  include LostInTranslations
@@ -20,7 +20,7 @@ class User < Struct.new(:title, :first_name, :last_name)
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```, otherwise calls the original redefined method.
24
24
 
25
25
  ```ruby
26
26
  @user = User.new('Cavaleiro', 'Joao', 'Neve')
@@ -44,15 +44,23 @@ I18n.with_locale(:de) do
44
44
  end
45
45
  ```
46
46
 
47
- Instance method **#translate** is also available:
47
+ Instance method ```#translate``` is also available:
48
48
  ```ruby
49
49
  @user.translate(:first_name, :fr) # returns 'Jean'
50
50
  @user.translate(:first_name, :en) # returns 'Jon'
51
51
  @user.translate(:first_name, :de) # returns 'Joao'
52
52
  ```
53
53
 
54
- ## 2. Ideal usage
55
- If your ActiveRecord Model has a json attribute called **translation_data**.
54
+ And ```#<I18n.available_locales>_<translated_field>``` is also available:
55
+ ```ruby
56
+ @user.fr_first_name # returns 'Jean'
57
+ @user.en_first_name # returns 'Jon'
58
+ @user.pt_first_name # returns 'Joao'
59
+ @user.de_first_name # returns 'Joao'
60
+ ```
61
+
62
+ ## 2) Ideal usage
63
+ If your ActiveRecord Model has a json attribute called ```translation_data```.
56
64
  ```ruby
57
65
  class CreateUsers < ActiveRecord::Migration
58
66
  def up
@@ -60,7 +68,7 @@ class CreateUsers < ActiveRecord::Migration
60
68
  t.string :title
61
69
  t.string :first_name
62
70
  t.string :last_name
63
- t.json :translation_data
71
+ t.jsonb :translation_data
64
72
  t.timestamps
65
73
  end
66
74
 
@@ -93,9 +101,9 @@ I18n.locale = :fr
93
101
  @user.title # returns 'Cavaleiro'
94
102
  ```
95
103
 
96
- ## 3. Configuration
104
+ ## 3) Configuration
97
105
 
98
- ### 3.1 Your "translation_data" method (in all of your objects) is not called "translation_data"
106
+ ### 3.1) Your "translation_data" method (in all of your objects) is not called "translation_data"
99
107
  ```ruby
100
108
  LostInTranslations.configure do |config|
101
109
  config.translation_data_field = 'my_translation_data_field'
@@ -121,7 +129,7 @@ I18n.locale = :fr
121
129
  User.find(1).first_name # returns 'Jean'
122
130
  ```
123
131
 
124
- ### 3.2 Your "translation_data" method (in a particular object) is not called "translation_data"
132
+ ### 3.2) Your "translation_data" method (in a particular object) is not called "translation_data"
125
133
  ```ruby
126
134
  class User < ActiveRecord::Base
127
135
  include LostInTranslations
@@ -143,7 +151,7 @@ I18n.locale = :fr
143
151
  User.find(1).first_name # returns 'Jean'
144
152
  ```
145
153
 
146
- ### 3.3 Custom translation mechanism
154
+ ### 3.3) Custom translation mechanism
147
155
  ```ruby
148
156
  class MyTranslator
149
157
  def self.translate(object, field, locale)
@@ -189,7 +197,7 @@ I18n.locale = :fr
189
197
  User.find(1).first_name # returns 'Jean' from redis or yaml file
190
198
  ```
191
199
 
192
- ## 4 Instalation
200
+ ## 4) Instalation
193
201
 
194
202
  Add your application's Gemfile:
195
203
  ```
data/Rakefile ADDED
@@ -0,0 +1,10 @@
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]
@@ -10,7 +10,7 @@ module LostInTranslations
10
10
  end
11
11
 
12
12
  def self.included(base_class)
13
- base_class.include Base
13
+ base_class.send(:include, Base)
14
14
  base_class.extend ClassMethods
15
15
  end
16
16
 
@@ -14,10 +14,12 @@ module LostInTranslations
14
14
  end
15
15
 
16
16
  def self.included(base_class)
17
- base_class.include Base
17
+ base_class.send(:include, Base)
18
18
  base_class.extend ClassMethods
19
19
  end
20
20
 
21
+ include Base
22
+
21
23
  def translate(field, locale = I18n.locale)
22
24
  LostInTranslations.translate(self, field, locale) ||
23
25
  Ruby.call_original_field(self, field)
@@ -1,5 +1,5 @@
1
1
  module LostInTranslations
2
2
 
3
- VERSION = '1.2.0'.freeze
3
+ VERSION = '1.3.0'.freeze
4
4
 
5
5
  end
@@ -10,9 +10,9 @@ module LostInTranslations
10
10
  def self.included(base_class)
11
11
  if defined?(::ActiveRecord::Base) &&
12
12
  base_class.ancestors.include?(::ActiveRecord::Base)
13
- base_class.include LostInTranslations::ActiveRecord
13
+ base_class.send(:include, LostInTranslations::ActiveRecord)
14
14
  else
15
- base_class.include Ruby
15
+ base_class.send(:include, Ruby)
16
16
  end
17
17
  end
18
18
 
@@ -26,9 +26,24 @@ module LostInTranslations
26
26
 
27
27
  def self.define_translation_methods(object, *fields)
28
28
  fields.each do |field|
29
+ define_dynamic_translation_method(object, field)
30
+ define_particular_translation_method(object, field)
31
+ end
32
+ end
33
+
34
+ def self.define_dynamic_translation_method(object, method_name)
35
+ object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
36
+ def #{method_name}
37
+ translate(:#{method_name}, I18n.locale)
38
+ end
39
+ RUBY
40
+ end
41
+
42
+ def self.define_particular_translation_method(object, method_name)
43
+ I18n.available_locales.each do |locale|
29
44
  object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
30
- def #{field}
31
- translate(:#{field}, I18n.locale)
45
+ def #{locale}_#{method_name}
46
+ translate(:#{method_name}, :#{locale})
32
47
  end
33
48
  RUBY
34
49
  end
@@ -19,12 +19,12 @@ Gem::Specification.new do |gem|
19
19
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
20
  gem.require_paths = ['lib']
21
21
 
22
+ gem.add_development_dependency 'pry', '0.10.3'
23
+ gem.add_development_dependency 'rake', '11.2.2'
22
24
  gem.add_development_dependency 'rspec', '3.4.0'
23
- gem.add_development_dependency 'byebug', '8.2.5'
24
25
  gem.add_development_dependency 'sqlite3', '1.3.11'
25
26
  gem.add_development_dependency 'rubocop', '0.37.2'
26
27
  gem.add_development_dependency 'simplecov', '0.11.2'
27
- gem.add_development_dependency 'pry-byebug', '3.3.0'
28
28
  gem.add_development_dependency 'activerecord', '4.2.6'
29
29
  gem.add_development_dependency 'codeclimate-test-reporter', '0.4.8'
30
30
 
@@ -2,6 +2,52 @@ require 'spec_helper'
2
2
 
3
3
  describe LostInTranslations do
4
4
 
5
+ describe "#define_translation_methods" do
6
+ context "when the passed methods do not exist" do
7
+ before do
8
+ @user_class = Struct.new(:first_name, :last_name) do
9
+ include LostInTranslations
10
+ end
11
+
12
+ LostInTranslations.define_translation_methods(@user_class, :unknown)
13
+
14
+ @user = @user_class.new('Joao', 'Neve')
15
+ end
16
+
17
+ it "#unknown should be defined" do
18
+ expect(@user.respond_to?(:unknown)).to be true
19
+ end
20
+
21
+ it "#<I18n.available_locales>_unknown should be defined" do
22
+ I18n.available_locales.each do |locale|
23
+ expect(@user.respond_to?("#{locale}_unknown")).to be true
24
+ end
25
+ end
26
+ end
27
+
28
+ context "when the passed methods do exist" do
29
+ before do
30
+ @user_class = Struct.new(:first_name, :last_name) do
31
+ include LostInTranslations
32
+ end
33
+
34
+ LostInTranslations.define_translation_methods(@user_class, :first_name)
35
+
36
+ @user = @user_class.new('Joao', 'Neve')
37
+ end
38
+
39
+ it "#first_name should be defined" do
40
+ expect(@user.respond_to?(:first_name)).to be true
41
+ end
42
+
43
+ it "#<I18n.available_locales>_first_name should be defined" do
44
+ I18n.available_locales.each do |locale|
45
+ expect(@user.respond_to?("#{locale}_first_name")).to be true
46
+ end
47
+ end
48
+ end
49
+ end
50
+
5
51
  describe "#included" do
6
52
  context "when the object inherits from ActiveRecord" do
7
53
  before do
data/spec/spec_helper.rb CHANGED
@@ -11,7 +11,7 @@ end
11
11
 
12
12
  $: << File.expand_path('../', File.dirname(__FILE__))
13
13
 
14
- require 'pry-byebug'
14
+ require 'pry'
15
15
  require 'active_record'
16
16
  require 'lost_in_translations'
17
17
 
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lost_in_translations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - StreetBees Dev Team
@@ -11,89 +11,89 @@ cert_chain: []
11
11
  date: 2016-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rspec
14
+ name: pry
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 3.4.0
19
+ version: 0.10.3
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 3.4.0
26
+ version: 0.10.3
27
27
  - !ruby/object:Gem::Dependency
28
- name: byebug
28
+ name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 8.2.5
33
+ version: 11.2.2
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 8.2.5
40
+ version: 11.2.2
41
41
  - !ruby/object:Gem::Dependency
42
- name: sqlite3
42
+ name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - '='
46
46
  - !ruby/object:Gem::Version
47
- version: 1.3.11
47
+ version: 3.4.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - '='
53
53
  - !ruby/object:Gem::Version
54
- version: 1.3.11
54
+ version: 3.4.0
55
55
  - !ruby/object:Gem::Dependency
56
- name: rubocop
56
+ name: sqlite3
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: 0.37.2
61
+ version: 1.3.11
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: 0.37.2
68
+ version: 1.3.11
69
69
  - !ruby/object:Gem::Dependency
70
- name: simplecov
70
+ name: rubocop
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - '='
74
74
  - !ruby/object:Gem::Version
75
- version: 0.11.2
75
+ version: 0.37.2
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - '='
81
81
  - !ruby/object:Gem::Version
82
- version: 0.11.2
82
+ version: 0.37.2
83
83
  - !ruby/object:Gem::Dependency
84
- name: pry-byebug
84
+ name: simplecov
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - '='
88
88
  - !ruby/object:Gem::Version
89
- version: 3.3.0
89
+ version: 0.11.2
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - '='
95
95
  - !ruby/object:Gem::Version
96
- version: 3.3.0
96
+ version: 0.11.2
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: activerecord
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -151,6 +151,7 @@ files:
151
151
  - Gemfile
152
152
  - Gemfile.lock
153
153
  - README.md
154
+ - Rakefile
154
155
  - lib/lost_in_translations.rb
155
156
  - lib/lost_in_translations/active_record.rb
156
157
  - lib/lost_in_translations/base.rb