diff options
Diffstat (limited to 'app/models/public_body.rb')
-rw-r--r-- | app/models/public_body.rb | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 81149e3c2..311e19001 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -112,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 @@ -255,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) @@ -344,7 +334,7 @@ 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, available_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? @@ -357,7 +347,11 @@ 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 @@ -391,15 +385,28 @@ class PublicBody < ActiveRecord::Base next end - field_list = ['name', 'short_name', 'request_email', 'notes', 'publication_scheme', 'home_page'] + field_list = ['name', 'short_name', 'request_email', 'notes', 'publication_scheme', 'home_page', 'tag_string'] - if public_body = bodies_by_name[name] + 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 === I18n.default_locale) ? 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) @@ -422,6 +429,11 @@ class PublicBody < ActiveRecord::Base field_list.each do |field_name| localized_field_name = (locale === I18n.default_locale) ? 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) @@ -431,7 +443,6 @@ class PublicBody < ActiveRecord::Base 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.tag_string = tag public_body.last_edit_editor = editor public_body.last_edit_comment = 'Created from spreadsheet' public_body.save! @@ -446,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 @@ -515,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 |