aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/public_body.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/public_body.rb')
-rw-r--r--app/models/public_body.rb190
1 files changed, 122 insertions, 68 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index ef1d60e26..ab836657b 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -38,7 +38,7 @@ class PublicBody < ActiveRecord::Base
validates_uniqueness_of :short_name, :message => N_("Short name is already taken"), :if => Proc.new { |pb| pb.short_name != "" }
validates_uniqueness_of :name, :message => N_("Name is already taken")
-
+
has_many :info_requests, :order => 'created_at desc'
has_many :track_things, :order => 'created_at desc'
@@ -46,6 +46,40 @@ class PublicBody < ActiveRecord::Base
translates :name, :short_name, :request_email, :url_name, :notes, :first_letter, :publication_scheme
+ # Convenience methods for creating/editing translations via forms
+ def translation(locale)
+ self.translations.find_by_locale(locale)
+ end
+
+ # XXX - Don't like repeating this!
+ def calculate_cached_fields(t)
+ t.first_letter = t.name.scan(/^./mu)[0].upcase unless t.name.nil? or t.name.empty?
+ short_long_name = t.name
+ short_long_name = t.short_name if t.short_name and !t.short_name.empty?
+ t.url_name = MySociety::Format.simplify_url_part(short_long_name, 'body')
+ end
+
+ def translated_versions
+ translations
+ end
+
+ def translated_versions=(translation_attrs)
+ if translation_attrs.respond_to? :each_value # Hash => updating
+ translation_attrs.each_value do |attrs|
+ t = translation(attrs[:locale]) || PublicBody::Translation.new
+ t.attributes = attrs
+ calculate_cached_fields(t)
+ t.save!
+ end
+ else # Array => creating
+ translation_attrs.each do |attrs|
+ new_translation = PublicBody::Translation.new(attrs)
+ calculate_cached_fields(new_translation)
+ translations << new_translation
+ end
+ end
+ end
+
# Make sure publication_scheme gets the correct default value.
# (This would work automatically, were publication_scheme not a translated attribute)
def after_initialize
@@ -54,15 +88,16 @@ 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)
- @locale = I18n.locale.to_s
- PublicBody.with_locale(@locale) do
+ locale = self.locale || I18n.locale
+ PublicBody.with_locale(locale) do
found = PublicBody.find(:all,
:conditions => ["public_body_translations.url_name='#{name}'"],
:joins => :translations,
:readonly => false)
- return found.first if found.size == 1
- # Shouldn't we just make url_name unique?
- raise "Two bodies with the same URL name: #{name}" if found.size > 1
+ # 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
@@ -77,16 +112,6 @@ class PublicBody < ActiveRecord::Base
end
end
- # XXX this should be saner; probably implement categories as data
- begin
- load "public_body_categories_#{I18n.locale.to_s}.rb"
- rescue MissingSourceFile
- begin
- load "public_body_categories_#{I18n.default_locale.to_s}.rb"
- rescue MissingSourceFile
- load "public_body_categories.rb"
- end
- end
# Set the first letter, which is used for faster queries
before_save(:set_first_letter)
def set_first_letter
@@ -138,7 +163,7 @@ class PublicBody < ActiveRecord::Base
return 'defunct'
elsif self.not_apply?
return 'not_apply'
- elsif self.request_email.empty? or self.request_email == 'blank'
+ elsif self.request_email.nil? or self.request_email.empty? or self.request_email == 'blank'
return 'bad_contact'
else
raise "requestable_failure_reason called with type that has no reason"
@@ -171,7 +196,7 @@ class PublicBody < ActiveRecord::Base
return self.created_at.strftime("%Y%m%d%H%M%S")
end
def variety
- "authority"
+ return "authority"
end
# if the URL name has changed, then all requested_from: queries
@@ -190,7 +215,6 @@ class PublicBody < ActiveRecord::Base
# When name or short name is changed, also change the url name
def short_name=(short_name)
-
globalize.write(self.class.locale || I18n.locale, :short_name, short_name)
self[:short_name] = short_name
self.update_url_name
@@ -203,15 +227,15 @@ class PublicBody < ActiveRecord::Base
end
def update_url_name
- url_name = MySociety::Format.simplify_url_part(self.short_or_long_name, 'body')
- self.url_name = 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? # can happen during construction
+ if self.short_name.nil? || self.short_name.empty? # 'nil' can happen during construction
self.name
else
- self.short_name.empty? ? self.name : self.short_name
+ self.short_name
end
end
@@ -221,8 +245,8 @@ class PublicBody < ActiveRecord::Base
types = []
first = true
for tag in self.tags
- if PublicBodyCategories::CATEGORIES_BY_TAG.include?(tag.name)
- desc = PublicBodyCategories::CATEGORY_SINGULAR_BY_TAG[tag.name]
+ if PublicBodyCategories::get().by_tag().include?(tag.name)
+ desc = PublicBodyCategories::get().singular_by_tag()[tag.name]
if first
# terrible that Ruby/Rails doesn't have an equivalent of ucfirst
# (capitalize shockingly converts later characters to lowercase)
@@ -310,9 +334,10 @@ class PublicBody < ActiveRecord::Base
# Import from CSV. Just tests things and returns messages if dry_run is true.
# Returns an array of [array of errors, array of notes]. If there are errors,
# always rolls back (as with dry_run).
- def self.import_csv(csv, tag, dry_run, editor, additional_locales = [])
+ def self.import_csv(csv, tag, tag_behaviour, dry_run, editor, available_locales = [])
errors = []
notes = []
+ available_locales = [I18n.default_locale] if available_locales.empty?
begin
ActiveRecord::Base.transaction do
@@ -322,14 +347,18 @@ class PublicBody < ActiveRecord::Base
bodies_by_name = {}
set_of_existing = Set.new()
PublicBody.with_locale(I18n.default_locale) do
- for existing_body in PublicBody.find_by_tag(tag)
+ bodies = (tag.nil? || tag.empty?) ? PublicBody.find(:all) : PublicBody.find_by_tag(tag)
+ for existing_body in bodies
+ # Hide InternalAdminBody from import notes
+ next if existing_body.id == PublicBody.internal_admin_body.id
+
bodies_by_name[existing_body.name] = existing_body
set_of_existing.add(existing_body.name)
end
end
set_of_importing = Set.new()
- field_names = { 'name'=>1, 'email'=>2 } # Default values in case no field list is given
+ field_names = { 'name'=>1, 'request_email'=>2 } # Default values in case no field list is given
line = 0
CSV::Reader.parse(csv) do |row|
line = line + 1
@@ -340,57 +369,82 @@ class PublicBody < ActiveRecord::Base
row.each_with_index {|field, i| field_names[field] = i}
next
end
+
+ fields = {}
+ field_names.each{|name, i| fields[name] = row[i]}
name = row[field_names['name']]
- email = row[field_names['email']]
+ email = row[field_names['request_email']]
next if name.nil?
- if email.nil?
- email = '' # unknown/bad contact is empty string
- end
name.strip!
- email.strip!
+ email.strip! unless email.nil?
- if email != "" && !MySociety::Validate.is_valid_email(email)
- errors.push "error: line " + line.to_s + ": invalid email " + email + " for authority '" + name + "'"
+ if !email.nil? && !email.empty? && !MySociety::Validate.is_valid_email(email)
+ errors.push "error: line #{line.to_s}: invalid email '#{email}' for authority '#{name}'"
next
end
+
+ field_list = ['name', 'short_name', 'request_email', 'notes', 'publication_scheme', 'home_page', 'tag_string']
+
+ if public_body = bodies_by_name[name] # Existing public body
+ available_locales.each do |locale|
+ PublicBody.with_locale(locale) do
+ changed = {}
+ field_list.each do |field_name|
+ localized_field_name = (locale.to_s == I18n.default_locale.to_s) ? field_name : "#{field_name}.#{locale}"
+ localized_value = field_names[localized_field_name] && row[field_names[localized_field_name]]
+
+ # Tags are a special case, as we support adding to the field, not just setting a new value
+ if localized_field_name == 'tag_string'
+ if localized_value.nil?
+ localized_value = tag unless tag.empty?
+ else
+ if tag_behaviour == 'add'
+ localized_value = "#{localized_value} #{tag}" unless tag.empty?
+ localized_value = "#{localized_value} #{public_body.tag_string}"
+ end
+ end
+ end
+
+ if !localized_value.nil? and public_body.send(field_name) != localized_value
+ changed[field_name] = "#{public_body.send(field_name)}: #{localized_value}"
+ public_body.send("#{field_name}=", localized_value)
+ end
+ end
- if bodies_by_name[name]
- # Already have the public body, just update email
- public_body = bodies_by_name[name]
- if public_body.request_email != email
- notes.push "line " + line.to_s + ": updating email for '" + name + "' from " + public_body.request_email + " to " + email
- public_body.request_email = email
- public_body.last_edit_editor = editor
- public_body.last_edit_comment = 'Updated from spreadsheet'
- public_body.save!
- end
-
- additional_locales.each do |locale|
- localized_name = field_names["name.#{locale}"] && row[field_names["name.#{locale}"]]
- PublicBody.with_locale(locale) do
- if !localized_name.nil? and public_body.name != localized_name
- notes.push "line " + line.to_s + ": updating name for '#{name}' from '#{public_body.name}' to '#{localized_name}' (locale: #{locale})."
- public_body.name = localized_name
+ unless changed.empty?
+ notes.push "line #{line.to_s}: updating authority '#{name}' (locale: #{locale}):\n\t#{changed.to_json}"
+ public_body.last_edit_editor = editor
+ public_body.last_edit_comment = 'Updated from spreadsheet'
public_body.save!
end
end
end
- else
- # New public body
- notes.push "line " + line.to_s + ": new authority '" + name + "' with email " + email
- public_body = PublicBody.new(:name => name, :request_email => email, :short_name => "", :home_page => "", :publication_scheme => "", :notes => "", :last_edit_editor => editor, :last_edit_comment => 'Created from spreadsheet')
- public_body.tag_string = tag
- public_body.save!
-
- additional_locales.each do |locale|
- localized_name = field_names["name.#{locale}"] && row[field_names["name.#{locale}"]]
- if !localized_name.nil?
- PublicBody.with_locale(locale) do
- notes.push "line " + line.to_s + ": (aka '#{localized_name}' in locale #{locale})"
- public_body.name = localized_name
- public_body.publication_scheme = ""
+ else # New public body
+ public_body = PublicBody.new(:name=>"", :short_name=>"", :request_email=>"")
+ available_locales.each do |locale|
+ PublicBody.with_locale(locale) do
+ changed = {}
+ field_list.each do |field_name|
+ localized_field_name = (locale.to_s == I18n.default_locale.to_s) ? field_name : "#{field_name}.#{locale}"
+ localized_value = field_names[localized_field_name] && row[field_names[localized_field_name]]
+
+ if localized_field_name == 'tag_string' and tag_behaviour == 'add'
+ localized_value = "#{localized_value} #{tag}" unless tag.empty?
+ end
+
+ if !localized_value.nil? and public_body.send(field_name) != localized_value
+ changed[field_name] = localized_value
+ public_body.send("#{field_name}=", localized_value)
+ end
+ end
+
+ unless changed.empty?
+ notes.push "line #{line.to_s}: creating new authority '#{name}' (locale: #{locale}):\n\t#{changed.to_json}"
+ public_body.publication_scheme = public_body.publication_scheme || ""
+ public_body.last_edit_editor = editor
+ public_body.last_edit_comment = 'Created from spreadsheet'
public_body.save!
end
end
@@ -403,7 +457,7 @@ class PublicBody < ActiveRecord::Base
# Give an error listing ones that are to be deleted
deleted_ones = set_of_existing - set_of_importing
if deleted_ones.size > 0
- notes.push "notes: Some " + tag + " bodies are in database, but not in CSV file:\n " + Array(deleted_ones).join("\n ") + "\nYou may want to delete them manually.\n"
+ notes.push "Notes: Some " + tag + " bodies are in database, but not in CSV file:\n " + Array(deleted_ones).join("\n ") + "\nYou may want to delete them manually.\n"
end
# Rollback if a dry run, or we had errors
@@ -472,7 +526,7 @@ class PublicBody < ActiveRecord::Base
end
def notes_without_html
# assume notes are reasonably behaved HTML, so just use simple regexp on this
- self.notes.gsub(/<\/?[^>]*>/, "")
+ self.notes.nil? ? '' : self.notes.gsub(/<\/?[^>]*>/, "")
end
def json_for_api