serial_translator 1.1.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/lib/serial_translator.rb +17 -14
- data/lib/serial_translator/translation_type.rb +23 -0
- data/lib/serial_translator/version.rb +1 -1
- data/serial_translator.gemspec +3 -1
- data/spec/lib/serial_translator/serial_translator_length_validator_spec.rb +1 -1
- data/spec/lib/serial_translator/serial_translator_presence_validator_spec.rb +1 -1
- data/spec/lib/serial_translator/translation_type_spec.rb +33 -0
- data/spec/lib/serial_translator_spec.rb +15 -1
- data/spec/spec_helper.rb +2 -2
- data/spec/support/{fake_object.rb → fake_env.rb} +17 -19
- metadata +37 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62f5df8cb8d27b7661c49e1b23e15b2b87c6d66d
|
4
|
+
data.tar.gz: f5d879126f11013dba1f1a2832116a2a593d913e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6d142dde2b763be22b547c5eb765dc7f5214a977a9416940516763d955c45aa7abf6a834b8781fbe941bb71391522b622ba6bd47398edf7eaf11bc4549ec5ca
|
7
|
+
data.tar.gz: 6c0c04b1764fcfa8b9f5161169001429ad877310741abf40c057a42e8eb975961eca9026131ca202cf8a8b57d85f60f473e200b13bfaae879180e62fabf21c78
|
data/README.md
CHANGED
@@ -6,6 +6,10 @@
|
|
6
6
|
|
7
7
|
Translate ActiveRecord object attributes without the use of additional models.
|
8
8
|
|
9
|
+
## Changes from 1.* to 2.*
|
10
|
+
|
11
|
+
`serial_translator` will now store all translations in JSON format per default rather than in YAML. However, it is still able to read YAML serialized translations and will convert them in the background.
|
12
|
+
|
9
13
|
## Installation
|
10
14
|
|
11
15
|
Add it to your Gemfile:
|
data/lib/serial_translator.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
require 'active_support'
|
2
2
|
require 'active_model'
|
3
|
+
require 'active_record'
|
3
4
|
|
4
5
|
require 'serial_translator/serial_translator_length_validator'
|
5
6
|
require 'serial_translator/serial_translator_presence_validator'
|
7
|
+
require 'serial_translator/translation_type'
|
8
|
+
require 'serial_translator/version'
|
6
9
|
|
7
10
|
module SerialTranslator
|
8
11
|
extend ActiveSupport::Concern
|
@@ -17,42 +20,42 @@ module SerialTranslator
|
|
17
20
|
def serial_translator_for(*attributes)
|
18
21
|
@serial_translator_attributes = attributes
|
19
22
|
|
20
|
-
attributes.each do |
|
21
|
-
|
23
|
+
attributes.each do |attr_name|
|
24
|
+
attribute :"#{attr_name}_translations", SerialTranslator::TranslationType.new
|
22
25
|
|
23
26
|
# Define the normal getter, that respects the
|
24
27
|
# current translation locale
|
25
|
-
define_method
|
26
|
-
translations = translations_for(
|
28
|
+
define_method attr_name do |locale = current_translation_locale|
|
29
|
+
translations = translations_for(attr_name)
|
27
30
|
result = translations[locale] if translations[locale].present?
|
28
31
|
result ||= translations.values.detect(&:present?) if translation_fallback?
|
29
32
|
result
|
30
33
|
end
|
31
34
|
|
32
|
-
define_method :"#{
|
33
|
-
__send__(
|
35
|
+
define_method :"#{attr_name}?" do |locale = current_translation_locale|
|
36
|
+
__send__(attr_name.to_sym, locale).present?
|
34
37
|
end
|
35
38
|
|
36
39
|
# Define the normal setter, that respects the
|
37
40
|
# current translation locale
|
38
|
-
define_method "#{
|
39
|
-
__send__(:"#{
|
40
|
-
translations = translations_for(
|
41
|
+
define_method "#{attr_name}=" do |value|
|
42
|
+
__send__(:"#{attr_name}_translations_will_change!")
|
43
|
+
translations = translations_for(attr_name)
|
41
44
|
if value.present?
|
42
45
|
translations[current_translation_locale] = value
|
43
46
|
else
|
44
47
|
translations.delete(current_translation_locale)
|
45
48
|
end
|
46
|
-
__send__(:"#{
|
49
|
+
__send__(:"#{attr_name}_translations=", translations)
|
47
50
|
end
|
48
51
|
|
49
52
|
# Define getters for each specific available locale
|
50
53
|
I18n.available_locales.each do |available_locale|
|
51
|
-
define_method "#{
|
54
|
+
define_method "#{attr_name}_#{available_locale.to_s.underscore}" do
|
52
55
|
begin
|
53
56
|
old_locale = I18n.locale
|
54
57
|
I18n.locale = available_locale
|
55
|
-
__send__(
|
58
|
+
__send__(attr_name)
|
56
59
|
ensure
|
57
60
|
I18n.locale = old_locale
|
58
61
|
end
|
@@ -61,11 +64,11 @@ module SerialTranslator
|
|
61
64
|
|
62
65
|
# Define setters for each specific available locale
|
63
66
|
I18n.available_locales.each do |available_locale|
|
64
|
-
define_method "#{
|
67
|
+
define_method "#{attr_name}_#{available_locale.to_s.underscore}=" do |value|
|
65
68
|
begin
|
66
69
|
old_locale = I18n.locale
|
67
70
|
I18n.locale = available_locale
|
68
|
-
__send__(:"#{
|
71
|
+
__send__(:"#{attr_name}=", value)
|
69
72
|
ensure
|
70
73
|
I18n.locale = old_locale
|
71
74
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class SerialTranslator::TranslationType < ActiveRecord::Type::String
|
2
|
+
def cast(value)
|
3
|
+
case value
|
4
|
+
when nil
|
5
|
+
{}
|
6
|
+
when Hash
|
7
|
+
value
|
8
|
+
when /\A---/
|
9
|
+
YAML.load(value)
|
10
|
+
else
|
11
|
+
JSON.parse(value)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def serialize(value)
|
16
|
+
case value
|
17
|
+
when String
|
18
|
+
super
|
19
|
+
else
|
20
|
+
JSON(value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/serial_translator.gemspec
CHANGED
@@ -18,7 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency 'activemodel', '>=
|
21
|
+
spec.add_dependency 'activemodel', '>= 5'
|
22
|
+
spec.add_dependency 'activerecord', '>= 5'
|
22
23
|
|
23
24
|
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
25
|
spec.add_development_dependency 'rake'
|
@@ -26,4 +27,5 @@ Gem::Specification.new do |spec|
|
|
26
27
|
spec.add_development_dependency 'rspec-collection_matchers'
|
27
28
|
spec.add_development_dependency 'json', '~>2'
|
28
29
|
spec.add_development_dependency 'simplecov'
|
30
|
+
spec.add_development_dependency 'sqlite3'
|
29
31
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SerialTranslator::TranslationType do
|
4
|
+
let(:type) { SerialTranslator::TranslationType.new }
|
5
|
+
|
6
|
+
describe '#cast' do
|
7
|
+
it 'works for JSON' do
|
8
|
+
expect(type.cast('{"foo":"bar"}')).to eq('foo' => 'bar')
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'works for YAML' do
|
12
|
+
expect(type.cast("---\nfoo: bar")).to eq('foo' => 'bar')
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'does nothing if already a Hash' do
|
16
|
+
expect(type.cast({foo: :bar})).to eq(foo: :bar)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'defaults to empty hash' do
|
20
|
+
expect(type.cast(nil)).to eq({})
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#serialize' do
|
25
|
+
it 'converts to JSON' do
|
26
|
+
expect(type.serialize({foo: :bar})).to eq('{"foo":"bar"}')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'leaves strings alone (to avoid double serialization)' do
|
30
|
+
expect(type.serialize('foo')).to eq 'foo'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe SerialTranslator do
|
4
|
-
let(:object) {
|
4
|
+
let(:object) { Fake.new }
|
5
5
|
|
6
6
|
describe 'getting a translated value' do
|
7
7
|
it 'returns the translated value in the requested locale' do
|
@@ -136,4 +136,18 @@ describe SerialTranslator do
|
|
136
136
|
expect(object).to_not be_translated_into :yml
|
137
137
|
end
|
138
138
|
end
|
139
|
+
|
140
|
+
context 'use active record methods' do
|
141
|
+
it 'works with where and LIKE' do
|
142
|
+
expect {
|
143
|
+
Fake.where('title_translations LIKE ?', '%foobar').count
|
144
|
+
}.not_to raise_error
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'works with where and Hash syntax' do
|
148
|
+
expect {
|
149
|
+
Fake.where(title_translations: 'foobar').count
|
150
|
+
}.not_to raise_error
|
151
|
+
end
|
152
|
+
end
|
139
153
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,27 +1,25 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
|
2
|
+
|
3
|
+
ActiveRecord::Schema.define do
|
4
|
+
self.verbose = false
|
5
|
+
|
6
|
+
create_table :fakes, force: true do |t|
|
7
|
+
t.text :description_translations
|
8
|
+
t.text :title_translations
|
9
|
+
t.text :subtitle_translations
|
10
|
+
t.text :summary_translations
|
11
|
+
t.text :credits_translations
|
12
|
+
t.text :imprint_translations
|
13
|
+
end
|
14
|
+
end
|
7
15
|
|
8
|
-
|
16
|
+
class Fake < ActiveRecord::Base
|
17
|
+
include SerialTranslator
|
9
18
|
|
10
19
|
# Has to be set in this context to test it correctly
|
11
20
|
I18n.available_locales = [:en, :de, :'en-GB']
|
12
21
|
|
13
|
-
|
14
|
-
|
15
|
-
serial_translator_for(*TRANSLATED_ATTRIBUTES)
|
16
|
-
|
17
|
-
TRANSLATED_ATTRIBUTES.map {|ta| "#{ta}_translations" }.each do |hash_name|
|
18
|
-
attr_accessor hash_name
|
19
|
-
define_method("#{hash_name}_will_change!") { changes << hash_name.to_sym }
|
20
|
-
end
|
21
|
-
|
22
|
-
def changes
|
23
|
-
@changes ||= []
|
24
|
-
end
|
22
|
+
serial_translator_for(*%w(description title subtitle summary credits imprint))
|
25
23
|
|
26
24
|
validates :description, serial_translator_presence: true
|
27
25
|
validates :title, serial_translator_length: { within: 5..25 }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serial_translator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- betterplace development team
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -16,14 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5'
|
20
20
|
type: :runtime
|
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: '
|
26
|
+
version: '5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -108,6 +122,20 @@ dependencies:
|
|
108
122
|
- - ">="
|
109
123
|
- !ruby/object:Gem::Version
|
110
124
|
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: sqlite3
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
111
139
|
description: Translate attribute values without additional models
|
112
140
|
email:
|
113
141
|
- developers@betterplace.org
|
@@ -126,13 +154,15 @@ files:
|
|
126
154
|
- lib/serial_translator.rb
|
127
155
|
- lib/serial_translator/serial_translator_length_validator.rb
|
128
156
|
- lib/serial_translator/serial_translator_presence_validator.rb
|
157
|
+
- lib/serial_translator/translation_type.rb
|
129
158
|
- lib/serial_translator/version.rb
|
130
159
|
- serial_translator.gemspec
|
131
160
|
- spec/lib/serial_translator/serial_translator_length_validator_spec.rb
|
132
161
|
- spec/lib/serial_translator/serial_translator_presence_validator_spec.rb
|
162
|
+
- spec/lib/serial_translator/translation_type_spec.rb
|
133
163
|
- spec/lib/serial_translator_spec.rb
|
134
164
|
- spec/spec_helper.rb
|
135
|
-
- spec/support/
|
165
|
+
- spec/support/fake_env.rb
|
136
166
|
homepage: http://www.betterplace.org
|
137
167
|
licenses:
|
138
168
|
- WTFPL
|
@@ -160,6 +190,7 @@ summary: Translate attribute values without additional models
|
|
160
190
|
test_files:
|
161
191
|
- spec/lib/serial_translator/serial_translator_length_validator_spec.rb
|
162
192
|
- spec/lib/serial_translator/serial_translator_presence_validator_spec.rb
|
193
|
+
- spec/lib/serial_translator/translation_type_spec.rb
|
163
194
|
- spec/lib/serial_translator_spec.rb
|
164
195
|
- spec/spec_helper.rb
|
165
|
-
- spec/support/
|
196
|
+
- spec/support/fake_env.rb
|