aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2014-09-24 15:20:48 +0100
committerLouise Crow <louise.crow@gmail.com>2014-09-24 16:10:59 +0100
commit0f798eb2f305748d5977774ac1c8296e89a52c2a (patch)
tree5be2fcb1f16cef9e22d02122ae8841d408ae74c1
parent8a638b3db152652fdfa988e1d3bee552b2a0dc3d (diff)
fixup! Basic category admin screen
-rw-r--r--app/models/public_body.rb10
-rw-r--r--app/models/public_body_category.rb13
-rw-r--r--app/models/public_body_heading.rb10
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