diff options
author | Louise Crow <louise.crow@gmail.com> | 2015-02-02 14:34:02 +0000 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2015-02-23 11:17:56 +0000 |
commit | 2c6250666b8dd5ca383ec29ea786805b102e2ac7 (patch) | |
tree | 25b528e65c625007cf064dee4d9cc6b1e0eb2d59 | |
parent | 60e3aad7ac32ac826b93a707db9589237fd4948d (diff) |
Test localised value for import against existing localised value
Previously the call to public_body.send would return the value for
the default locale if no value was set in the current locale,
meaning that translations for attributes that were the same as
the attribute values in the default locale were not being loaded.
Fixes #2134.
-rw-r--r-- | app/models/public_body.rb | 28 | ||||
-rw-r--r-- | spec/fixtures/files/multiple-locales-same-name.csv | 2 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 16 |
3 files changed, 44 insertions, 2 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index a9cdfeab2..ec4e8bb32 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -422,6 +422,17 @@ class PublicBody < ActiveRecord::Base has_tag?('site_administration') end + + # Read an attribute value (without using locale fallbacks if the attribute is translated) + def read_attribute_value(name, locale) + if PublicBody.translates.include?(name.to_sym) + globalize.stash.contains?(locale, name) ? globalize.stash.read(locale, name) : translation_for(locale).send(name) + else + self.send(name) + end + end + + class ImportCSVDryRun < StandardError end @@ -521,8 +532,8 @@ class PublicBody < ActiveRecord::Base end end end + if !localized_value.nil? and public_body.read_attribute_value(field_name, locale) != localized_value - if !localized_value.nil? and public_body.send(field_name) != localized_value changed[field_name] = "#{public_body.send(field_name)}: #{localized_value}" public_body.send("#{field_name}=", localized_value) end @@ -549,7 +560,7 @@ class PublicBody < ActiveRecord::Base localized_value = "#{localized_value} #{tag}" unless tag.empty? end - if !localized_value.nil? and public_body.send(field_name) != localized_value + if !localized_value.nil? and public_body.read_attribute_value(field_name, locale) != localized_value changed[field_name] = localized_value public_body.send("#{field_name}=", localized_value) end @@ -793,6 +804,19 @@ class PublicBody < ActiveRecord::Base private + # Read an attribute value (without using locale fallbacks if the attribute is translated) + def read_attribute_value(name, locale) + if self.class.translates.include?(name.to_sym) + if globalize.stash.contains?(locale, name) + globalize.stash.read(locale, name) + else + translation_for(locale).send(name) + end + else + send(name) + end + end + def request_email_if_requestable # Request_email can be blank, meaning we don't have details if self.is_requestable? diff --git a/spec/fixtures/files/multiple-locales-same-name.csv b/spec/fixtures/files/multiple-locales-same-name.csv new file mode 100644 index 000000000..43505f6a6 --- /dev/null +++ b/spec/fixtures/files/multiple-locales-same-name.csv @@ -0,0 +1,2 @@ +"#id","request_email","name","name.es","tag_string","home_page" +23842,"test@test.es","Test","Test",37,"http://www.test.es/" diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 225958cac..23f3aa5d6 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -598,6 +598,22 @@ CSV PublicBody.csv_import_fields = old_csv_import_fields end + it "should import translations for fields whose values are the same as the default locale's" do + original_count = PublicBody.count + + csv_contents = load_file_fixture("multiple-locales-same-name.csv") + + errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', true, 'someadmin', ['en', 'es']) # true means dry run + errors.should == [] + notes.size.should == 3 + notes[0..1].should == [ + "line 2: creating new authority 'Test' (locale: en):\n\t{\"name\":\"Test\",\"request_email\":\"test@test.es\",\"home_page\":\"http://www.test.es/\",\"tag_string\":\"37\"}", + "line 2: creating new authority 'Test' (locale: es):\n\t{\"name\":\"Test\"}", + ] + notes[2].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( .+\n)*You may want to delete them manually.\n/ + + PublicBody.count.should == original_count + end end describe PublicBody do |