serial_translator 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/lib/serial_translator.rb +1 -0
- data/lib/serial_translator/version.rb +1 -1
- data/spec/lib/serial_translator/serial_translator_validator_spec.rb +31 -17
- data/spec/lib/serial_translator_spec.rb +26 -21
- data/spec/spec_helper.rb +0 -4
- data/spec/support/fake_object.rb +22 -0
- metadata +21 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46318b38f444ed6a25379dd0aeafe616232b1f22
|
4
|
+
data.tar.gz: 4aa381357c2b9ae40d97330ca7b466114d69225f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 179f207c79c20b56732b8a760ac18e0c02cb7e97de9d781a9de6761fc9a6480b7908f0cb701c40dfda4f51b3058f89cf9914511042d004fea80593c66baa7585
|
7
|
+
data.tar.gz: 2c46ea1ceaf2f97f76c67d954ca935167bb3f793b6fafedf3acf3a346635328feb396487be6035cd77e3ce687d0bcfe3daa1806e86fcf38b6804e9074989ac22
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/lib/serial_translator.rb
CHANGED
@@ -6,80 +6,94 @@ describe SerialTranslator::SerialTranslatorValidator do
|
|
6
6
|
describe 'presence validation' do
|
7
7
|
it 'validates presence correctly if attribute is nil' do
|
8
8
|
example.valid?
|
9
|
-
example.
|
9
|
+
expect(example.errors[:description].size).to eq 1
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'validates presence correctly if attribute is empty' do
|
13
13
|
example.description = ''
|
14
|
-
example.
|
14
|
+
example.valid?
|
15
|
+
expect(example.errors[:description].size).to eq 1
|
15
16
|
end
|
16
17
|
|
17
18
|
it 'has no error if everything is fine' do
|
18
19
|
example.description = 'This is a nice foo thing'
|
19
|
-
example.
|
20
|
+
example.valid?
|
21
|
+
expect(example.errors[:description].size).to eq 0
|
20
22
|
end
|
21
23
|
|
22
24
|
it 'is valid if any language has a value' do
|
23
25
|
example.description_translations = { en: '', de: '' }
|
24
|
-
example.
|
26
|
+
example.valid?
|
27
|
+
expect(example.errors[:description].size).to eq 1
|
28
|
+
|
25
29
|
example.description_translations = { en: '', de: 'foobar' }
|
26
|
-
example.
|
30
|
+
example.valid?
|
31
|
+
expect(example.errors[:description].size).to eq 0
|
32
|
+
|
27
33
|
example.description_translations = { en: 'foobar', de: nil }
|
28
|
-
example.
|
34
|
+
example.valid?
|
35
|
+
expect(example.errors[:description].size).to eq 0
|
29
36
|
end
|
30
37
|
end
|
31
38
|
|
32
39
|
describe 'length validation' do
|
33
40
|
it 'validates min length correctly' do
|
34
41
|
example.title = '123'
|
35
|
-
example.
|
42
|
+
example.valid?
|
43
|
+
expect(example.errors[:title].size).to eq 1
|
36
44
|
end
|
37
45
|
|
38
46
|
it 'validates max length correctly' do
|
39
47
|
example.title = 'f' * 26
|
40
|
-
example.
|
48
|
+
example.valid?
|
49
|
+
expect(example.errors[:title].size).to eq 1
|
41
50
|
end
|
42
51
|
|
43
52
|
it 'ignores blank fields' do
|
44
53
|
example.title = ''
|
45
|
-
example.
|
54
|
+
example.valid?
|
55
|
+
expect(example.errors[:title].size).to eq 0
|
46
56
|
end
|
47
57
|
|
48
58
|
it 'validates all translation values' do
|
49
59
|
example.title_translations = { en: '123', de: '123' }
|
50
|
-
example.
|
60
|
+
example.valid?
|
61
|
+
expect(example.errors[:title].size).to eq 2
|
51
62
|
|
52
63
|
example.title_translations = { en: '123456', de: '123' }
|
53
|
-
example.
|
64
|
+
example.valid?
|
65
|
+
expect(example.errors[:title].size).to eq 1
|
54
66
|
|
55
67
|
example.title_translations = { en: '123', de: '123456' }
|
56
|
-
example.
|
68
|
+
example.valid?
|
69
|
+
expect(example.errors[:title].size).to eq 1
|
57
70
|
|
58
71
|
example.title_translations = { en: '123456', de: '123456' }
|
59
|
-
example.
|
72
|
+
example.valid?
|
73
|
+
expect(example.errors[:title].size).to eq 0
|
60
74
|
end
|
61
75
|
|
62
76
|
it 'validates correctly on language change as well' do
|
63
77
|
I18n.locale = :de
|
64
78
|
example.description = :something_to_make_it_pass_valid
|
65
79
|
example.title = 'Valid foo'
|
66
|
-
example.
|
80
|
+
expect(example).to be_valid
|
67
81
|
|
68
82
|
I18n.locale = :en
|
69
83
|
example.title = ''
|
70
|
-
example.
|
84
|
+
expect(example).to be_valid
|
71
85
|
end
|
72
86
|
end
|
73
87
|
|
74
88
|
describe '#kind' do
|
75
89
|
it 'returns length if length validation is set' do
|
76
90
|
validator = SerialTranslator::SerialTranslatorValidator.new(attributes: :foo, length: 1..2)
|
77
|
-
validator.kind.
|
91
|
+
expect(validator.kind).to eq :length
|
78
92
|
end
|
79
93
|
|
80
94
|
it 'returns presence if it is no length validation' do
|
81
95
|
validator = SerialTranslator::SerialTranslatorValidator.new(attributes: :foo)
|
82
|
-
validator.kind.
|
96
|
+
expect(validator.kind).to eq :presence
|
83
97
|
end
|
84
98
|
end
|
85
99
|
end
|
@@ -7,32 +7,32 @@ describe SerialTranslator do
|
|
7
7
|
describe 'getting a translated value' do
|
8
8
|
it 'returns the translated value in the requested locale' do
|
9
9
|
object.title_translations = { de: 'German translation', en: 'English translation' }
|
10
|
-
object.title(:de).
|
10
|
+
expect(object.title(:de)).to eq 'German translation'
|
11
11
|
end
|
12
12
|
|
13
13
|
it 'returns the origin locale value if no translation is present' do
|
14
14
|
object.title_translations = { en: 'English translation' }
|
15
|
-
object.title(:de).
|
15
|
+
expect(object.title(:de)).to eq 'English translation'
|
16
16
|
end
|
17
17
|
|
18
18
|
it 'returns the origin locale value if the translation is empty' do
|
19
19
|
object.title_translations = { de: '', en: 'English translation' }
|
20
|
-
object.title(:de).
|
20
|
+
expect(object.title(:de)).to eq 'English translation'
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'returns nil if no translations are there at all' do
|
24
24
|
object.title_translations = {}
|
25
|
-
object.title(:de).
|
25
|
+
expect(object.title(:de)).to be_nil
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'uses the given text locale' do
|
29
29
|
object.title_translations = { de: 'Deutsch', en: 'English' }
|
30
30
|
object.current_translation_locale = :en
|
31
|
-
object.title.
|
31
|
+
expect(object.title).to eq 'English'
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'does not fail even if translations were not initialized before' do
|
35
|
-
object.title.
|
35
|
+
expect(object.title).to be_blank
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
@@ -40,74 +40,79 @@ describe SerialTranslator do
|
|
40
40
|
it 'sets the value for the current text locale' do
|
41
41
|
object.current_translation_locale = :de
|
42
42
|
object.title = 'Deutsch'
|
43
|
-
object.title_translations.
|
43
|
+
expect(object.title_translations).to eq({ de: 'Deutsch' })
|
44
44
|
end
|
45
45
|
|
46
46
|
it 'nil removes the value for the current text locale' do
|
47
47
|
object.title_translations = { de: 'Deutsch' }
|
48
48
|
object.current_translation_locale = :de
|
49
49
|
object.title = nil
|
50
|
-
object.title_translations.
|
50
|
+
expect(object.title_translations).to eq({})
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'blank string removes the value for the current text locale' do
|
54
54
|
object.title_translations = { de: 'Deutsch' }
|
55
55
|
object.current_translation_locale = :de
|
56
56
|
object.title = " "
|
57
|
-
object.title_translations.
|
57
|
+
expect(object.title_translations).to eq({})
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'marks the changes on the object for active record' do
|
61
|
+
object.title = 'Changed'
|
62
|
+
expect(object.changes).to include :title_translations
|
58
63
|
end
|
59
64
|
end
|
60
65
|
|
61
66
|
describe 'getting an attribute for a specific locale' do
|
62
67
|
it 'gets the correct translation' do
|
63
68
|
object.title_translations = { de: 'Deutsch' }
|
64
|
-
object.title_de.
|
69
|
+
expect(object.title_de).to eq 'Deutsch'
|
65
70
|
end
|
66
71
|
|
67
72
|
it 'uses fallback mechanisms as well' do
|
68
73
|
object.title_translations = { en: 'English' }
|
69
|
-
object.title_de.
|
74
|
+
expect(object.title_de).to eq 'English'
|
70
75
|
end
|
71
76
|
end
|
72
77
|
|
73
78
|
describe 'writing an attribute with for a specific locale' do
|
74
79
|
it 'sets the value for the correct locale' do
|
75
80
|
object.title_de = 'Deutsch'
|
76
|
-
object.title_translations.
|
81
|
+
expect(object.title_translations).to eq({ de: 'Deutsch' })
|
77
82
|
end
|
78
83
|
|
79
84
|
it 'works for complex locales as well' do
|
80
85
|
object.title_en_gb = 'Arrr'
|
81
|
-
object.title_translations.
|
86
|
+
expect(object.title_translations).to eq({ :'en-GB' => 'Arrr' })
|
82
87
|
end
|
83
88
|
end
|
84
89
|
|
85
90
|
describe '#current_translation_locale' do
|
86
91
|
it 'returns the value that was given' do
|
87
92
|
object.current_translation_locale = 'de'
|
88
|
-
object.current_translation_locale.
|
93
|
+
expect(object.current_translation_locale).to eq :de
|
89
94
|
end
|
90
95
|
|
91
96
|
it 'must not return nil' do
|
92
97
|
object.current_translation_locale = nil
|
93
|
-
object.current_translation_locale.
|
98
|
+
expect(object.current_translation_locale).to eq I18n.locale.to_sym
|
94
99
|
end
|
95
100
|
end
|
96
101
|
|
97
102
|
describe '#translated_locales' do
|
98
103
|
it 'returns an empty array if nothing was translated' do
|
99
|
-
object.translated_locales.
|
104
|
+
expect(object.translated_locales).to eq []
|
100
105
|
end
|
101
106
|
|
102
107
|
it 'includes all locales where something is present' do
|
103
108
|
object.title_translations = { en: 'Foo', de: 'Foo' }
|
104
109
|
object.summary_translations = { nb: 'Foo' }
|
105
|
-
object.translated_locales.
|
110
|
+
expect(object.translated_locales).to eq [:en, :de, :nb]
|
106
111
|
end
|
107
112
|
|
108
113
|
it 'does not include locales where all translations are blank' do
|
109
114
|
object.title_translations = { en: 'Foo', de: '' }
|
110
|
-
object.translated_locales.
|
115
|
+
expect(object.translated_locales).to eq [:en]
|
111
116
|
end
|
112
117
|
end
|
113
118
|
|
@@ -115,15 +120,15 @@ describe SerialTranslator do
|
|
115
120
|
before { object.title_translations = { en: 'Foo', de: '' } }
|
116
121
|
|
117
122
|
it 'returns true if there is a translation available for this locale' do
|
118
|
-
object.
|
123
|
+
expect(object).to be_translated_into :en
|
119
124
|
end
|
120
125
|
|
121
126
|
it 'returns false if there is no translation present for this locale' do
|
122
|
-
object.
|
127
|
+
expect(object).to_not be_translated_into :de
|
123
128
|
end
|
124
129
|
|
125
130
|
it 'returns false if this locale is unknown yet' do
|
126
|
-
object.
|
131
|
+
expect(object).to_not be_translated_into :yml
|
127
132
|
end
|
128
133
|
end
|
129
134
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/support/fake_object.rb
CHANGED
@@ -1,13 +1,35 @@
|
|
1
1
|
# This class is only used for testing purposes.
|
2
2
|
class FakeObject
|
3
3
|
include ActiveModel::Validations
|
4
|
+
|
4
5
|
def self.serialize(*) ; end
|
5
6
|
def self.before_save(*) ; end
|
7
|
+
|
6
8
|
attr_accessor :title_translations, :description_translations, :summary_translations
|
7
9
|
|
8
10
|
# Has to be set in this context to test it correctly
|
9
11
|
I18n.available_locales = [:en, :de, :'en-GB']
|
10
12
|
|
13
|
+
def changes
|
14
|
+
@changes ||= []
|
15
|
+
end
|
16
|
+
|
17
|
+
def mark_change(attribute)
|
18
|
+
changes << attribute
|
19
|
+
end
|
20
|
+
|
21
|
+
def description_translations_will_change!
|
22
|
+
mark_change :description_translations
|
23
|
+
end
|
24
|
+
|
25
|
+
def title_translations_will_change!
|
26
|
+
mark_change :title_translations
|
27
|
+
end
|
28
|
+
|
29
|
+
def summary_translations_will_change!
|
30
|
+
mark_change :summary_translations
|
31
|
+
end
|
32
|
+
|
11
33
|
include SerialTranslator
|
12
34
|
serial_translator_for :title, :description, :summary
|
13
35
|
validates :title, serial_translator: { length: 5..25 }
|
metadata
CHANGED
@@ -1,97 +1,97 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serial_translator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
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: 2014-
|
11
|
+
date: 2014-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 3.0.0
|
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
26
|
version: 3.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activeresource
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 3.0.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 3.0.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '1.3'
|
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
54
|
version: '1.3'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
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
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - '>='
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
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
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: simplecov
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - '>='
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
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
96
|
version: '0'
|
97
97
|
description: Translate attribute values without additional models
|
@@ -101,10 +101,11 @@ executables: []
|
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
|
-
-
|
105
|
-
-
|
106
|
-
-
|
107
|
-
-
|
104
|
+
- .gitignore
|
105
|
+
- .rspec
|
106
|
+
- .ruby-gemset
|
107
|
+
- .ruby-version
|
108
|
+
- .travis.yml
|
108
109
|
- Gemfile
|
109
110
|
- LICENSE.txt
|
110
111
|
- README.md
|
@@ -128,12 +129,12 @@ require_paths:
|
|
128
129
|
- lib
|
129
130
|
required_ruby_version: !ruby/object:Gem::Requirement
|
130
131
|
requirements:
|
131
|
-
- -
|
132
|
+
- - '>='
|
132
133
|
- !ruby/object:Gem::Version
|
133
134
|
version: '0'
|
134
135
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
136
|
requirements:
|
136
|
-
- -
|
137
|
+
- - '>='
|
137
138
|
- !ruby/object:Gem::Version
|
138
139
|
version: '0'
|
139
140
|
requirements: []
|