aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/admin_public_body_controller.rb4
-rw-r--r--app/models/public_body.rb22
-rw-r--r--app/views/admin_public_body/import_csv.rhtml13
3 files changed, 35 insertions, 4 deletions
diff --git a/app/controllers/admin_public_body_controller.rb b/app/controllers/admin_public_body_controller.rb
index d80e2937f..e249cef11 100644
--- a/app/controllers/admin_public_body_controller.rb
+++ b/app/controllers/admin_public_body_controller.rb
@@ -150,7 +150,7 @@ class AdminPublicBodyController < AdminController
# Try with dry run first
csv_contents = params[:csv_file].read
- en = PublicBody.import_csv(csv_contents, params[:tag], true, admin_http_auth_user(), I18n.available_locales)
+ en = PublicBody.import_csv(csv_contents, params[:tag], params[:tag_behaviour], true, admin_http_auth_user(), I18n.available_locales)
errors = en[0]
notes = en[1]
@@ -159,7 +159,7 @@ class AdminPublicBodyController < AdminController
notes.push("Dry run was successful, real run would do as above.")
else
# And if OK, with real run
- en = PublicBody.import_csv(csv_contents, params[:tag], false, admin_http_auth_user(), I18n.available_locales)
+ en = PublicBody.import_csv(csv_contents, params[:tag], params[:tag_behaviour], false, admin_http_auth_user(), I18n.available_locales)
errors = en[0]
notes = en[1]
if errors.size != 0
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 430aa8f34..311e19001 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -334,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?
@@ -387,13 +387,26 @@ class PublicBody < ActiveRecord::Base
field_list = ['name', 'short_name', 'request_email', 'notes', 'publication_scheme', 'home_page', 'tag_string']
- if public_body = bodies_by_name[name] # Existing public body
+ 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)
@@ -416,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)
diff --git a/app/views/admin_public_body/import_csv.rhtml b/app/views/admin_public_body/import_csv.rhtml
index a7a13f477..ecd2c38b7 100644
--- a/app/views/admin_public_body/import_csv.rhtml
+++ b/app/views/admin_public_body/import_csv.rhtml
@@ -20,6 +20,14 @@
<label for="tag">Optional: Tag to add entries to / alter entries for:</label>
<%= text_field_tag 'tag', params[:tag] %>
</p>
+
+ <p>
+ <label for="tag_behaviour">What to do with existing tags?</label>
+ <%= select_tag 'tag_behaviour',
+ "<option value='add' selected>Add new tags to existing ones</option>
+ <option value='replace'>Replace existing tags with new ones</option>"
+ %>
+ </p>
<p><strong>CSV file format:</strong> A first row with the list of fields,
starting with '#', is optional but highly recommended. The fields 'name'
@@ -42,6 +50,11 @@
of the changes. When uploading, any changes since last import will be
overwritten - e.g. email addresses changed back.
</p>
+
+ <p><strong>Note:</strong> The import tag will also be added to the imported bodies
+ if no tags are provided in the CSV file or if the import mode is set to
+ "Add new tags to existing ones".
+ </p>
<p><%= submit_tag 'Dry run' %> <%= submit_tag 'Upload' %></p>
<% end %>