serial_translator 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9195a07bb5714e15a778ba0de2dc2e23d0be4f9
4
- data.tar.gz: ad43b00cdf697c73e7bde108428ff91740f4b52b
3
+ metadata.gz: 46318b38f444ed6a25379dd0aeafe616232b1f22
4
+ data.tar.gz: 4aa381357c2b9ae40d97330ca7b466114d69225f
5
5
  SHA512:
6
- metadata.gz: 831d4682cdde18ff4e9ddaef77adac67447b78093e9f8a732ada466b0b4176366662e5c668954d515e9d6d3250f05bec53f30ac4b0b73ccf90007552eb00cc56
7
- data.tar.gz: d88f89b1769a907ecf3df74640b8fad8913509eeb1de4f890b3885c092b6aba11a06388348082d52217156ef7b1e66ebbcf4168e331aadcdefee76e61572100f
6
+ metadata.gz: 179f207c79c20b56732b8a760ac18e0c02cb7e97de9d781a9de6761fc9a6480b7908f0cb701c40dfda4f51b3058f89cf9914511042d004fea80593c66baa7585
7
+ data.tar.gz: 2c46ea1ceaf2f97f76c67d954ca935167bb3f793b6fafedf3acf3a346635328feb396487be6035cd77e3ce687d0bcfe3daa1806e86fcf38b6804e9074989ac22
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -37,6 +37,7 @@ module SerialTranslator
37
37
  else
38
38
  translations.delete(current_translation_locale)
39
39
  end
40
+ __send__(:"#{attribute}_translations_will_change!")
40
41
  __send__(:"#{attribute}_translations=", translations)
41
42
  end
42
43
 
@@ -1,3 +1,3 @@
1
1
  module SerialTranslator
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -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.should have(1).error_on(:description)
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.should have(1).error_on(:description)
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.should have(0).errors_on(:description)
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.should have(1).errors_on(:description)
26
+ example.valid?
27
+ expect(example.errors[:description].size).to eq 1
28
+
25
29
  example.description_translations = { en: '', de: 'foobar' }
26
- example.should have(0).errors_on(:description)
30
+ example.valid?
31
+ expect(example.errors[:description].size).to eq 0
32
+
27
33
  example.description_translations = { en: 'foobar', de: nil }
28
- example.should have(0).errors_on(:description)
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.should have(1).error_on(:title)
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.should have(1).error_on(:title)
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.should have(0).errors_on(:title)
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.should have(2).errors_on(:title)
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.should have(1).errors_on(:title)
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.should have(1).errors_on(:title)
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.should have(0).errors_on(:title)
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.should be_valid
80
+ expect(example).to be_valid
67
81
 
68
82
  I18n.locale = :en
69
83
  example.title = ''
70
- example.should be_valid
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.should eq :length
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.should eq :presence
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).should eq 'German translation'
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).should eq 'English translation'
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).should eq 'English translation'
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).should be_nil
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.should eq 'English'
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.should be_blank
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.should eq({ de: 'Deutsch' })
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.should eq({})
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.should eq({})
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.should eq 'Deutsch'
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.should eq 'English'
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.should eq({ de: 'Deutsch' })
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.should eq({ :'en-GB' => 'Arrr' })
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.should eq :de
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.should eq I18n.locale.to_sym
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.should eq []
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.should eq [:en, :de, :nb]
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.should eq [:en]
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.should be_translated_into :en
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.should_not be_translated_into :de
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.should_not be_translated_into :yml
131
+ expect(object).to_not be_translated_into :yml
127
132
  end
128
133
  end
129
134
  end
data/spec/spec_helper.rb CHANGED
@@ -11,7 +11,3 @@ require 'active_model'
11
11
  require 'active_resource'
12
12
  require 'support/validations_matcher'
13
13
  require 'support/fake_object'
14
-
15
- RSpec.configure do |config|
16
- config.color_enabled = true
17
- end
@@ -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
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-06-17 00:00:00.000000000 Z
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
- - ".gitignore"
105
- - ".ruby-gemset"
106
- - ".ruby-version"
107
- - ".travis.yml"
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: []