aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/concerns/translatable.rb43
-rw-r--r--app/models/public_body.rb34
-rw-r--r--app/models/public_body_category.rb40
-rw-r--r--app/models/public_body_heading.rb38
4 files changed, 48 insertions, 107 deletions
diff --git a/app/models/concerns/translatable.rb b/app/models/concerns/translatable.rb
new file mode 100644
index 000000000..bc89e4c3b
--- /dev/null
+++ b/app/models/concerns/translatable.rb
@@ -0,0 +1,43 @@
+module Translatable
+ extend ActiveSupport::Concern
+
+ included do
+ accepts_nested_attributes_for :translations, :reject_if => :empty_translation_in_params?
+ end
+
+ def find_translation_by_locale(locale)
+ translations.find_by_locale(locale)
+ end
+
+ def translated_versions
+ translations
+ end
+
+ def ordered_translations
+ translations.select do |translation|
+ I18n.available_locales.include?(translation.locale)
+ end.sort_by do |translation|
+ I18n.available_locales.index(translation.locale)
+ end
+ end
+
+ def build_all_translations
+ I18n.available_locales.each do |locale|
+ if translations.none? { |translation| translation.locale == locale }
+ translations.build(:locale => locale)
+ end
+ end
+ end
+
+ def translated_versions=(translation_attrs)
+ warn "[DEPRECATION] #{self.class.name}#translated_versions= will be replaced " \
+ "by #{self.class.name}#translations_attributes= as of release 0.22"
+ self.translations_attributes = translation_attrs
+ end
+
+ private
+
+ def empty_translation_in_params?(attributes)
+ attributes.select { |k, v| v.present? && k.to_s != 'locale' }.empty?
+ end
+end
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 5cd82f8d7..feec7cdd5 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -68,7 +68,6 @@ class PublicBody < ActiveRecord::Base
}
translates :name, :short_name, :request_email, :url_name, :notes, :first_letter, :publication_scheme
- 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)']
@@ -100,6 +99,8 @@ class PublicBody < ActiveRecord::Base
self.non_versioned_columns << 'info_requests_not_held_count' << 'info_requests_overdue'
self.non_versioned_columns << 'info_requests_overdue_count'
+ include Translatable
+
# Public: Search for Public Bodies whose name, short_name, request_email or
# tags contain the given query
#
@@ -128,11 +129,6 @@ class PublicBody < ActiveRecord::Base
uniq
end
- # Convenience methods for creating/editing translations via forms
- def find_translation_by_locale(locale)
- self.translations.find_by_locale(locale)
- end
-
# TODO: - Don't like repeating this!
def calculate_cached_fields(t)
PublicBody.set_first_letter(t)
@@ -152,28 +148,6 @@ class PublicBody < ActiveRecord::Base
end
end
- def translated_versions
- translations
- 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
-
- def translated_versions=(translation_attrs)
- warn "[DEPRECATION] PublicBody#translated_versions= will be replaced " \
- "by PublicBody#translations_attributes= as of release 0.22"
- self.translations_attributes = translation_attrs
- end
-
def set_default_publication_scheme
# Make sure publication_scheme gets the correct default value.
# (This would work automatically, were publication_scheme not a translated attribute)
@@ -762,10 +736,6 @@ class PublicBody < ActiveRecord::Base
end
end
- def empty_translation_in_params?(attributes)
- attributes.select { |k, v| !v.blank? && k.to_s != 'locale' }.empty?
- end
-
def request_email_if_requestable
# Request_email can be blank, meaning we don't have details
if self.is_requestable?
diff --git a/app/models/public_body_category.rb b/app/models/public_body_category.rb
index af7711c3a..0a64172c1 100644
--- a/app/models/public_body_category.rb
+++ b/app/models/public_body_category.rb
@@ -18,13 +18,14 @@ class PublicBodyCategory < ActiveRecord::Base
has_many :public_body_headings, :through => :public_body_category_links
translates :title, :description
- accepts_nested_attributes_for :translations, :reject_if => :empty_translation_in_params?
validates_uniqueness_of :category_tag, :message => 'Tag is already taken'
validates_presence_of :title, :message => "Title can't be blank"
validates_presence_of :category_tag, :message => "Tag can't be blank"
validates_presence_of :description, :message => "Description can't be blank"
+ include Translatable
+
def self.get
locale = I18n.locale.to_s || default_locale.to_s || ""
categories = CategoryCollection.new
@@ -52,43 +53,6 @@ class PublicBodyCategory < ActiveRecord::Base
) |
PublicBodyCategory.find_by_sql(sql)
end
-
- # Convenience methods for creating/editing translations via forms
- def find_translation_by_locale(locale)
- translations.find_by_locale(locale)
- end
-
- def translated_versions
- translations
- end
-
- def translated_versions=(translation_attrs)
- warn "[DEPRECATION] PublicBodyCategory#translated_versions= will be replaced " \
- "by PublicBodyCategory#translations_attributes= as of release 0.22"
- self.translations_attributes = translation_attrs
- 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
-
- private
-
- 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
-
end
PublicBodyCategory::Translation.class_eval do
diff --git a/app/models/public_body_heading.rb b/app/models/public_body_heading.rb
index 514821e2c..d49b388bb 100644
--- a/app/models/public_body_heading.rb
+++ b/app/models/public_body_heading.rb
@@ -16,7 +16,6 @@ class PublicBodyHeading < ActiveRecord::Base
default_scope order('display_order ASC')
translates :name
- accepts_nested_attributes_for :translations, :reject_if => :empty_translation_in_params?
validates_uniqueness_of :name, :message => 'Name is already taken'
validates_presence_of :name, :message => 'Name can\'t be blank'
@@ -29,32 +28,7 @@ class PublicBodyHeading < ActiveRecord::Base
end
end
- # Convenience methods for creating/editing translations via forms
- def find_translation_by_locale(locale)
- translations.find_by_locale(locale)
- end
-
- def translated_versions
- translations
- end
-
- def translated_versions=(translation_attrs)
- warn "[DEPRECATION] PublicBodyHeading#translated_versions= will be replaced " \
- "by PublicBodyHeading#translations_attributes= as of release 0.22"
- self.translations_attributes = translation_attrs
- 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
+ include Translatable
def add_category(category)
unless public_body_categories.include?(category)
@@ -69,14 +43,4 @@ class PublicBodyHeading < ActiveRecord::Base
0
end
end
-
- private
-
- 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
-
end