diff options
Diffstat (limited to 'app/models/public_body.rb')
-rw-r--r-- | app/models/public_body.rb | 160 |
1 files changed, 47 insertions, 113 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 232c0ffa1..5d6e51534 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- encoding : utf-8 -*- # == Schema Information # Schema version: 20131024114346 # @@ -32,6 +32,10 @@ require 'securerandom' require 'set' class PublicBody < ActiveRecord::Base + include AdminColumn + + @non_admin_columns = %w(name last_edit_comment) + strip_attributes! validates_presence_of :name, :message => N_("Name can't be blank") @@ -51,11 +55,11 @@ class PublicBody < ActiveRecord::Base has_tag_string before_save :set_api_key, - :set_default_publication_scheme, - :set_first_letter + :set_default_publication_scheme after_save :purge_in_cache after_update :reindex_requested_from + # Every public body except for the internal admin one is visible scope :visible, lambda { { @@ -64,7 +68,11 @@ 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? + + include PublicBodyDerivedFields + class Translation + include PublicBodyDerivedFields + end # Default fields available for importing from CSV, in the format # [field_name, 'short description of field (basic html allowed)'] @@ -96,6 +104,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 # @@ -124,11 +134,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) @@ -138,7 +143,7 @@ class PublicBody < ActiveRecord::Base end # Set the first letter on a public body or translation - def PublicBody.set_first_letter(instance) + def self.set_first_letter(instance) unless instance.name.nil? or instance.name.empty? # we use a regex to ensure it works with utf-8/multi-byte first_letter = Unicode.upcase instance.name.scan(/^./mu)[0] @@ -148,28 +153,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) @@ -182,31 +165,30 @@ class PublicBody < ActiveRecord::Base # like find_by_url_name but also search historic url_name if none found def self.find_by_url_name_with_historic(name) - found = PublicBody.find(:all, - :conditions => ["public_body_translations.url_name=?", name], - :joins => :translations, - :readonly => false) - # If many bodies are found (usually because the url_name is the same across - # locales) return any of them - return found.first if found.size >= 1 - - # If none found, then search the history of short names - old = PublicBody::Version.find_all_by_url_name(name) - # Find unique public bodies in it - old = old.map { |x| x.public_body_id } - old = old.uniq + # If many bodies are found (usually because the url_name is the same + # across locales) return any of them. + found = joins(:translations). + where("public_body_translations.url_name = ?", name). + readonly(false). + first + + return found if found + + # If none found, then search the history of short names and find unique + # public bodies in it + old = PublicBody::Version. + where(:url_name => name). + pluck('DISTINCT public_body_id') + # Maybe return the first one, so we show something relevant, # rather than throwing an error? raise "Two bodies with the same historical URL name: #{name}" if old.size > 1 return unless old.size == 1 # does acts_as_versioned provide a method that returns the current version? - return PublicBody.find(old.first) + PublicBody.find(old.first) end - # Set the first letter, which is used for faster queries - def set_first_letter - PublicBody.set_first_letter(self) - end + # If tagged "not_apply", then FOI/EIR no longer applies to authority at all def not_apply? @@ -301,32 +283,6 @@ class PublicBody < ActiveRecord::Base end end - # When name or short name is changed, also change the url name - def short_name=(short_name) - globalize.write(Globalize.locale, :short_name, short_name) - self[:short_name] = short_name - self.update_url_name - end - - def name=(name) - globalize.write(Globalize.locale, :name, name) - self[:name] = name - self.update_url_name - end - - def update_url_name - self.url_name = MySociety::Format.simplify_url_part(self.short_or_long_name, 'body') - 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? # 'nil' can happen during construction - self.name.nil? ? "" : self.name - else - self.short_name - end - end - # Guess home page from the request email, or use explicit override, or nil # if not known. def calculated_home_page @@ -339,23 +295,24 @@ class PublicBody < ActiveRecord::Base # Are all requests to this body under the Environmental Information Regulations? def eir_only? - return self.has_tag?('eir_only') + has_tag?('eir_only') end + def law_only_short - if self.eir_only? - return "EIR" - else - return "FOI" - end + eir_only? ? 'EIR' : 'FOI' end # Schools are allowed more time in holidays, so we change some wordings def is_school? - return self.has_tag?('school') + has_tag?('school') + end + + def site_administration? + has_tag?('site_administration') end # The "internal admin" is a special body for internal use. - def PublicBody.internal_admin_body + def self.internal_admin_body # Use find_by_sql to avoid the search being specific to a # locale, since url_name is a translated field: sql = "SELECT * FROM public_bodies WHERE url_name = 'internal_admin_authority'" @@ -379,10 +336,6 @@ class PublicBody < ActiveRecord::Base end end - def site_administration? - has_tag?('site_administration') - end - class ImportCSVDryRun < StandardError end @@ -412,7 +365,7 @@ class PublicBody < ActiveRecord::Base # matching names won't work afterwards, and we'll create new bodies instead # of updating them bodies_by_name = {} - set_of_existing = Set.new() + set_of_existing = Set.new internal_admin_body_id = PublicBody.internal_admin_body.id I18n.with_locale(I18n.default_locale) do bodies = (tag.nil? || tag.empty?) ? PublicBody.find(:all, :include => :translations) : PublicBody.find_by_tag(tag) @@ -425,7 +378,7 @@ class PublicBody < ActiveRecord::Base end end - set_of_importing = Set.new() + set_of_importing = Set.new # Default values in case no field list is given field_names = { 'name' => 1, 'request_email' => 2 } line = 0 @@ -577,17 +530,11 @@ class PublicBody < ActiveRecord::Base return self.request_email_domain end - # Returns nil if configuration variable not set - def override_request_email - e = AlaveteliConfiguration::override_all_public_body_request_emails - e if e != "" - end - def request_email - if override_request_email - override_request_email - else + if AlaveteliConfiguration::override_all_public_body_request_emails.blank? || read_attribute(:request_email).blank? read_attribute(:request_email) + else + AlaveteliConfiguration::override_all_public_body_request_emails end end @@ -598,7 +545,7 @@ class PublicBody < ActiveRecord::Base # Return the domain part of an email address, canonicalised and with common # extra UK Government server name parts removed. - def PublicBody.extract_domain_from_email(email) + def self.extract_domain_from_email(email) email =~ /@(.*)/ if $1.nil? return nil @@ -656,12 +603,6 @@ class PublicBody < ActiveRecord::Base self.info_requests.each {|x| x.purge_in_cache} end - def for_admin_column - self.class.content_columns.map{|c| c unless %w(name last_edit_comment).include?(c.name)}.compact.each do |column| - yield(column.human_name, self.send(column.name), column.type.to_s, column.name) - end - end - def self.where_clause_for_stats(minimum_requests, total_column) # When producing statistics for public bodies, we want to # exclude any that are tagged with 'test' - we use a @@ -773,13 +714,6 @@ 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? |