diff options
Diffstat (limited to 'app/models/public_body.rb')
-rw-r--r-- | app/models/public_body.rb | 45 |
1 files changed, 17 insertions, 28 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 0e90a3c16..232c0ffa1 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -64,7 +64,7 @@ class PublicBody < ActiveRecord::Base } translates :name, :short_name, :request_email, :url_name, :notes, :first_letter, :publication_scheme - accepts_nested_attributes_for :translations + accepts_nested_attributes_for :translations, :reject_if => :empty_translation_in_params? # Default fields available for importing from CSV, in the format # [field_name, 'short description of field (basic html allowed)'] @@ -152,33 +152,15 @@ class PublicBody < ActiveRecord::Base translations end - def translations_attributes=(translation_attrs) - def empty_translation?(attrs) - attrs_with_values = attrs.select{ |key, value| value != '' and key.to_s != 'locale' } - attrs_with_values.empty? - end - if translation_attrs.respond_to? :each_value # Hash => updating - translation_attrs.each_value do |attrs| - next if empty_translation?(attrs) - t = translation_for(attrs[:locale]) || PublicBody::Translation.new - t.attributes = attrs - calculate_cached_fields(t) - t.save! - end - else # Array => creating - warn "[DEPRECATION] PublicBody#translations_attributes= " \ - "will no longer accept an Array as of release 0.22. " \ - "Use Hash arguments instead. See " \ - "spec/models/public_body_spec.rb and " \ - "app/views/admin_public_body/_form.html.erb for more " \ - "details." - - translation_attrs.each do |attrs| - next if empty_translation?(attrs) - new_translation = PublicBody::Translation.new(attrs) - calculate_cached_fields(new_translation) - translations << new_translation - end + def ordered_translations + translations. + select { |t| I18n.available_locales.include?(t.locale) }. + sort_by { |t| I18n.available_locales.index(t.locale) } + end + + def build_all_translations + I18n.available_locales.each do |locale| + translations.build(:locale => locale) unless translations.detect{ |t| t.locale == locale } end end @@ -791,6 +773,13 @@ class PublicBody < ActiveRecord::Base end end + def empty_translation_in_params?(attributes) + attrs_with_values = attributes.select do |key, value| + value != '' and key.to_s != 'locale' + end + attrs_with_values.empty? + end + def request_email_if_requestable # Request_email can be blank, meaning we don't have details if self.is_requestable? |