diff options
author | Louise Crow <louise.crow@gmail.com> | 2014-09-24 15:20:48 +0100 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2014-09-24 16:10:59 +0100 |
commit | 0f798eb2f305748d5977774ac1c8296e89a52c2a (patch) | |
tree | 5be2fcb1f16cef9e22d02122ae8841d408ae74c1 | |
parent | 8a638b3db152652fdfa988e1d3bee552b2a0dc3d (diff) |
fixup! Basic category admin screen
-rw-r--r-- | app/models/public_body.rb | 10 | ||||
-rw-r--r-- | app/models/public_body_category.rb | 13 | ||||
-rw-r--r-- | app/models/public_body_heading.rb | 10 |
3 files changed, 17 insertions, 16 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 87b5c2227..477503a61 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -132,14 +132,14 @@ class PublicBody < ActiveRecord::Base end def translated_versions=(translation_attrs) - def skip?(attrs) - valueless = attrs.inject({}) { |h, (k, v)| h[k] = v if v != '' and k != 'locale'; h } # because we want to fall back to alternative translations where there are empty values - return valueless.length == 0 + def empty_translation?(attrs) + attrs_with_values = attrs.select{ |key, value| value != '' and key != 'locale' } + attrs_with_values.empty? end if translation_attrs.respond_to? :each_value # Hash => updating translation_attrs.each_value do |attrs| - next if skip?(attrs) + next if empty_translation?(attrs) t = translation_for(attrs[:locale]) || PublicBody::Translation.new t.attributes = attrs calculate_cached_fields(t) @@ -147,7 +147,7 @@ class PublicBody < ActiveRecord::Base end else # Array => creating translation_attrs.each do |attrs| - next if skip?(attrs) + next if empty_translation?(attrs) new_translation = PublicBody::Translation.new(attrs) calculate_cached_fields(new_translation) translations << new_translation diff --git a/app/models/public_body_category.rb b/app/models/public_body_category.rb index e78b91733..526d34887 100644 --- a/app/models/public_body_category.rb +++ b/app/models/public_body_category.rb @@ -124,31 +124,32 @@ class PublicBodyCategory < ActiveRecord::Base ) end + # Convenience methods for creating/editing translations via forms def find_translation_by_locale(locale) self.translations.find_by_locale(locale) end - def skip?(attrs) - valueless = attrs.inject({}) { |h, (k, v)| h[k] = v if v != '' and k != 'locale'; h } # because we want to fall back to alternative translations where there are empty values - return valueless.length == 0 - end def translated_versions translations end def translated_versions=(translation_attrs) + def empty_translation?(attrs) + attrs_with_values = attrs.select{ |key, value| value != '' and key != 'locale' } + attrs_with_values.empty? + end if translation_attrs.respond_to? :each_value # Hash => updating translation_attrs.each_value do |attrs| - next if skip?(attrs) + next if empty_translation?(attrs) t = translation_for(attrs[:locale]) || PublicBodyCategory::Translation.new t.attributes = attrs t.save! end else # Array => creating translation_attrs.each do |attrs| - next if skip?(attrs) + next if empty_translation?(attrs) new_translation = PublicBodyCategory::Translation.new(attrs) translations << new_translation end diff --git a/app/models/public_body_heading.rb b/app/models/public_body_heading.rb index bbed55b29..4605087ba 100644 --- a/app/models/public_body_heading.rb +++ b/app/models/public_body_heading.rb @@ -37,21 +37,21 @@ class PublicBodyHeading < ActiveRecord::Base end def translated_versions=(translation_attrs) - def skip?(attrs) - valueless = attrs.inject({}) { |h, (k, v)| h[k] = v if v != '' and k != 'locale'; h } # because we want to fall back to alternative translations where there are empty values - return valueless.length == 0 + def empty_translation?(attrs) + attrs_with_values = attrs.select{ |key, value| value != '' and key != 'locale' } + attrs_with_values.empty? end if translation_attrs.respond_to? :each_value # Hash => updating translation_attrs.each_value do |attrs| - next if skip?(attrs) + next if empty_translation?(attrs) t = translation_for(attrs[:locale]) || PublicBodyHeading::Translation.new t.attributes = attrs t.save! end else # Array => creating translation_attrs.each do |attrs| - next if skip?(attrs) + next if empty_translation?(attrs) new_translation = PublicBodyHeading::Translation.new(attrs) translations << new_translation end |