aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/concerns
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/concerns')
-rw-r--r--app/models/concerns/admin_column.rb17
-rw-r--r--app/models/concerns/public_body_derived_fields.rb47
-rw-r--r--app/models/concerns/translatable.rb37
3 files changed, 101 insertions, 0 deletions
diff --git a/app/models/concerns/admin_column.rb b/app/models/concerns/admin_column.rb
new file mode 100644
index 000000000..6e19f5aa5
--- /dev/null
+++ b/app/models/concerns/admin_column.rb
@@ -0,0 +1,17 @@
+module AdminColumn
+ extend ActiveSupport::Concern
+
+ included do
+ class << self
+ attr_reader :non_admin_columns
+ end
+
+ @non_admin_columns = []
+ end
+
+ def for_admin_column
+ self.class.content_columns.reject { |c| self.class.non_admin_columns.include?(c.name) }.each do |column|
+ yield(column.human_name, send(column.name), column.type.to_s, column.name)
+ end
+ end
+end
diff --git a/app/models/concerns/public_body_derived_fields.rb b/app/models/concerns/public_body_derived_fields.rb
new file mode 100644
index 000000000..f389e3cbf
--- /dev/null
+++ b/app/models/concerns/public_body_derived_fields.rb
@@ -0,0 +1,47 @@
+module PublicBodyDerivedFields
+
+ extend ActiveSupport::Concern
+
+ included do
+ before_save :set_first_letter
+
+ # When name or short name is changed, also change the url name
+ def short_name=(short_name)
+ super
+ update_url_name
+ end
+
+ def name=(name)
+ super
+ update_url_name
+ end
+
+ end
+
+ # Return the short name if present, or else long name
+ def short_or_long_name
+ if self.short_name.nil? || self.short_name.empty?
+ self.name.nil? ? "" : self.name
+ else
+ self.short_name
+ end
+ end
+
+ # Set the first letter, which is used for faster queries
+ def set_first_letter
+ unless name.blank?
+ # we use a regex to ensure it works with utf-8/multi-byte
+ first_letter = Unicode.upcase name.scan(/^./mu)[0]
+ if first_letter != self.first_letter
+ self.first_letter = first_letter
+ end
+ end
+ end
+
+ def update_url_name
+ if changed.include?('name') || changed.include?('short_name')
+ self.url_name = MySociety::Format.simplify_url_part(self.short_or_long_name, 'body')
+ end
+ end
+
+end
diff --git a/app/models/concerns/translatable.rb b/app/models/concerns/translatable.rb
new file mode 100644
index 000000000..2aa4620d0
--- /dev/null
+++ b/app/models/concerns/translatable.rb
@@ -0,0 +1,37 @@
+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
+
+ private
+
+ def empty_translation_in_params?(attributes)
+ attributes.select { |k, v| v.present? && k.to_s != 'locale' }.empty?
+ end
+end