diff options
author | Gareth Rees <gareth@mysociety.org> | 2015-01-19 16:29:14 +0000 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2015-01-30 15:00:32 +0000 |
commit | a3dc8ee09b4b0a3661e623ff2665c005fbe8405e (patch) | |
tree | d9a877da51ce62d2fdb36060434ea3bbc2f17102 | |
parent | b23c9e8ba7fd87c7d41aac785fb5c132031b4d78 (diff) |
Add specs for PublicBody#translated_versions=
Also fixes #empty_translation? to check for String and Symbol keys named
'locale'.
Specs use a pre-change check to assert difference.
For some reason rspec change matcher fails with 'nil is not a symbol'.
-rw-r--r-- | app/models/public_body.rb | 2 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 110 |
2 files changed, 111 insertions, 1 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index a9cdfeab2..a434cce35 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -153,7 +153,7 @@ class PublicBody < ActiveRecord::Base def translated_versions=(translation_attrs) def empty_translation?(attrs) - attrs_with_values = attrs.select{ |key, value| value != '' and key != 'locale' } + attrs_with_values = attrs.select{ |key, value| value != '' and key.to_s != 'locale' } attrs_with_values.empty? end diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 225958cac..bdfaab6bf 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -28,6 +28,116 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +describe PublicBody do + + describe :translated_versions= 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().translated_versions = attrs + end + + it 'updates an existing translation' do + body = public_bodies(:geraldine_public_body) + translation = body.translation_for(:es) + params = { translation.id.to_sym => { :locale => 'es', + :name => 'Renamed' } } + + body.translated_versions = 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.translated_versions = { + translation.id.to_sym => { + :locale => 'es', + :name => 'Renamed' + }, + :new_translation => { + :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.translated_versions = { + translation.id.to_sym => { + :locale => 'es', + :name => 'Renamed' + }, + :empty_translation => { + :locale => 'es' + } + } + + 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().translated_versions = 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.translated_versions = [ { + :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.translated_versions = [ + { :locale => 'empty' } + ] + + expect(body.translations.size).to eq(1) + end + + end + + end + +end + describe PublicBody, " using tags" do before do @public_body = PublicBody.new(:name => 'Aardvark Monitoring Service', |