aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/public_body_spec.rb98
1 files changed, 98 insertions, 0 deletions
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index 510ad59fe..a9e801bfd 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -136,6 +136,104 @@ describe PublicBody do
end
+ describe :translations_attributes= do
+
+ context 'translation_attrs is a Hash' do
+
+ it 'takes the correct code path for a Hash' do
+ attrs = {}
+ attrs.should_receive(:each_value)
+ PublicBody.new().translations_attributes = attrs
+ end
+
+ it 'updates an existing translation' do
+ body = public_bodies(:geraldine_public_body)
+ translation = body.translation_for(:es)
+ params = { 'es' => { :locale => 'es',
+ :name => 'Renamed' } }
+
+ body.translations_attributes = params
+ I18n.with_locale(:es) { expect(body.name).to eq('Renamed') }
+ end
+
+ it 'updates an existing translation and creates a new translation' do
+ body = public_bodies(:geraldine_public_body)
+ translation = body.translation_for(:es)
+
+ expect(body.translations.size).to eq(2)
+
+ body.translations_attributes = {
+ 'es' => { :locale => 'es',
+ :name => 'Renamed' },
+ 'fr' => { :locale => 'fr',
+ :name => 'Le Geraldine Quango' }
+ }
+
+ expect(body.translations.size).to eq(3)
+ I18n.with_locale(:es) { expect(body.name).to eq('Renamed') }
+ I18n.with_locale(:fr) { expect(body.name).to eq('Le Geraldine Quango') }
+ end
+
+ it 'skips empty translations' do
+ body = public_bodies(:geraldine_public_body)
+ translation = body.translation_for(:es)
+
+ expect(body.translations.size).to eq(2)
+
+ body.translations_attributes = {
+ 'es' => { :locale => 'es',
+ :name => 'Renamed' },
+ 'fr' => { :locale => 'fr' }
+ }
+
+ expect(body.translations.size).to eq(2)
+ end
+
+ end
+
+ context 'translation_attrs is an Array' do
+
+ it 'takes the correct code path for an Array' do
+ attrs = []
+ attrs.should_receive(:each)
+ PublicBody.new().translations_attributes = attrs
+ end
+
+ it 'creates a new translation' do
+ body = public_bodies(:geraldine_public_body)
+ body.translation_for(:es).destroy
+ body.reload
+
+ expect(body.translations.size).to eq(1)
+
+ body.translations_attributes = [ {
+ :locale => 'es',
+ :name => 'Renamed'
+ }
+ ]
+
+ expect(body.translations.size).to eq(2)
+ I18n.with_locale(:es) { expect(body.name).to eq('Renamed') }
+ end
+
+ it 'skips empty translations' do
+ body = public_bodies(:geraldine_public_body)
+ body.translation_for(:es).destroy
+ body.reload
+
+ expect(body.translations.size).to eq(1)
+
+ body.translations_attributes = [
+ { :locale => 'empty' }
+ ]
+
+ expect(body.translations.size).to eq(1)
+ end
+
+ end
+
+ end
+
end
describe PublicBody, " using tags" do