diff options
-rw-r--r-- | app/models/public_body.rb | 20 | ||||
-rw-r--r-- | app/views/admin_public_body/import_csv.html.erb | 18 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 52 |
3 files changed, 76 insertions, 14 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index b22482541..87b5c2227 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -60,6 +60,21 @@ class PublicBody < ActiveRecord::Base translates :name, :short_name, :request_email, :url_name, :notes, :first_letter, :publication_scheme + # Default fields available for importing from CSV, in the format + # [field_name, 'short description of field (basic html allowed)'] + cattr_accessor :csv_import_fields do + [ + ['name', '(i18n)<strong>Existing records cannot be renamed</strong>'], + ['short_name', '(i18n)'], + ['request_email', '(i18n)'], + ['notes', '(i18n)'], + ['publication_scheme', '(i18n)'], + ['disclosure_log', '(i18n)'], + ['home_page', ''], + ['tag_string', '(tags separated by spaces)'], + ] + end + # Public: Search for Public Bodies whose name, short_name, request_email or # tags contain the given query # @@ -477,7 +492,10 @@ class PublicBody < ActiveRecord::Base next end - field_list = ['name', 'short_name', 'request_email', 'notes', 'publication_scheme', 'disclosure_log', 'home_page', 'tag_string'] + field_list = [] + self.csv_import_fields.each do |field_name, field_notes| + field_list.push field_name + end if public_body = bodies_by_name[name] # Existing public body available_locales.each do |locale| diff --git a/app/views/admin_public_body/import_csv.html.erb b/app/views/admin_public_body/import_csv.html.erb index d15ef1791..c690f0fc2 100644 --- a/app/views/admin_public_body/import_csv.html.erb +++ b/app/views/admin_public_body/import_csv.html.erb @@ -51,19 +51,11 @@ Another One,another@example.com,Otro organismo,a_tag </pre> <p><strong>Supported fields:</strong> - <ul> - <li> - <code>name</code> (i18n) - <strong>Existing records cannot be renamed</strong> - </li> - <li><code>short_name</code> (i18n)</li> - <li><code>request_email</code> (i18n)</li> - <li><code>notes</code> (i18n)</li> - <li><code>publication_scheme</code> (i18n)</li> - <li><code>disclosure_log</code> (i18n)</li> - <li><code>home_page</code></li> - <li><code>tag_string</code> (tags separated by spaces)</li> - </ul> + <ul> + <% PublicBody.csv_import_fields.each do |field, notes| %> + <li><code><%= field %></code> <%= sanitize(notes) %></li> + <% end %> + </ul> </p> <p><strong>Note:</strong> Choose <strong>dry run</strong> to test, without diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index a7544c218..225958cac 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -546,6 +546,58 @@ CSV errors.should include("error: line 3: Url name URL name is already taken for authority 'Foobar Test'") end + it 'has a default list of fields to import' do + expected_fields = [ + ['name', '(i18n)<strong>Existing records cannot be renamed</strong>'], + ['short_name', '(i18n)'], + ['request_email', '(i18n)'], + ['notes', '(i18n)'], + ['publication_scheme', '(i18n)'], + ['disclosure_log', '(i18n)'], + ['home_page', ''], + ['tag_string', '(tags separated by spaces)'], + ] + + expect(PublicBody.csv_import_fields).to eq(expected_fields) + end + + it 'allows you to override the default list of fields to import' do + old_csv_import_fields = PublicBody.csv_import_fields.clone + expected_fields = [ + ['name', '(i18n)<strong>Existing records cannot be renamed</strong>'], + ['short_name', '(i18n)'], + ] + + PublicBody.csv_import_fields = expected_fields + + expect(PublicBody.csv_import_fields).to eq(expected_fields) + + # Reset our change so that we don't affect other specs + PublicBody.csv_import_fields = old_csv_import_fields + end + + it 'allows you to append to the default list of fields to import' do + old_csv_import_fields = PublicBody.csv_import_fields.clone + expected_fields = [ + ['name', '(i18n)<strong>Existing records cannot be renamed</strong>'], + ['short_name', '(i18n)'], + ['request_email', '(i18n)'], + ['notes', '(i18n)'], + ['publication_scheme', '(i18n)'], + ['disclosure_log', '(i18n)'], + ['home_page', ''], + ['tag_string', '(tags separated by spaces)'], + ['a_new_field', ''], + ] + + PublicBody.csv_import_fields << ['a_new_field', ''] + + expect(PublicBody.csv_import_fields).to eq(expected_fields) + + # Reset our change so that we don't affect other specs + PublicBody.csv_import_fields = old_csv_import_fields + end + end describe PublicBody do |