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.rb68
1 files changed, 51 insertions, 17 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 0e32a5164..b75da4331 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -54,15 +54,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
@@ -138,7 +139,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"
@@ -310,29 +311,39 @@ 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)
+ def self.import_csv(csv, tag, dry_run, editor, additional_locales = [])
errors = []
notes = []
begin
ActiveRecord::Base.transaction do
- existing_bodies = PublicBody.find_by_tag(tag)
-
+ # Use the default locale when retrieving existing bodies; otherwise
+ # matching names won't work afterwards, and we'll create new bodies instead
+ # of updating them
bodies_by_name = {}
set_of_existing = Set.new()
- for existing_body in existing_bodies
- bodies_by_name[existing_body.name] = existing_body
- set_of_existing.add(existing_body.name)
+ PublicBody.with_locale(I18n.default_locale) do
+ for existing_body in PublicBody.find_by_tag(tag)
+ 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
line = 0
-
CSV::Reader.parse(csv) do |row|
line = line + 1
- name = row[1]
- email = row[2]
+ # Parse the first line as a field list if it starts with '#'
+ if line==1 and row.to_s =~ /^#(.*)$/
+ row[0] = row[0][1..-1] # Remove the # sign on first field
+ row.each_with_index {|field, i| field_names[field] = i}
+ next
+ end
+
+ name = row[field_names['name']]
+ email = row[field_names['email']]
next if name.nil?
if email.nil?
email = '' # unknown/bad contact is empty string
@@ -353,15 +364,38 @@ class PublicBody < ActiveRecord::Base
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.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
+ 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 = ""
+ public_body.save!
+ end
+ end
+ end
end
set_of_importing.add(name)