diff options
-rw-r--r-- | app/models/public_body.rb | 10 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 13 |
2 files changed, 22 insertions, 1 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 4aaca8337..9cb344f14 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -506,7 +506,15 @@ class PublicBody < ActiveRecord::Base 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! + + begin + public_body.save! + rescue ActiveRecord::RecordInvalid + public_body.errors.full_messages.each do |msg| + errors.push "error: line #{ line }: #{ msg } for authority '#{ name }'" + end + next + end end end end diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index c443f0d6a..38e31783d 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -533,6 +533,19 @@ describe PublicBody, " when loading CSV files" do PublicBody.count.should == original_count + 3 end + it "should handle active record validation errors" do + csv = <<-CSV +#name,request_email,short_name +Foobar,a@example.com,foobar +Foobar Test,b@example.com,foobar +CSV + + csv_contents = normalize_string_to_utf8(csv) + errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', true, 'someadmin') # true means dry run + + errors.should include("error: line 3: Url name URL name is already taken for authority 'Foobar Test'") + end + end describe PublicBody do |