diff --git a/CHANGELOG.md b/CHANGELOG.md index 216e6ad..d21f147 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ Bug fixes: * Update the primary key sequence in PostgreSQL tables after seeding data. This ensures that id conflicts do not occur when records are subsequently added to the table. +* Raise ActiveRecord::RecordNotSaved if any of the saves fail (but they won't fail due to validation since saves are done without validation, so this guards against callbacks failing etc.) + Version 2.0.0 ------------- diff --git a/lib/seed-fu/seeder.rb b/lib/seed-fu/seeder.rb index a0cd20c..555ad94 100644 --- a/lib/seed-fu/seeder.rb +++ b/lib/seed-fu/seeder.rb @@ -65,7 +65,7 @@ def seed_record(data) puts " - #{@model_class} #{data.inspect}" unless @options[:quiet] record.send(:attributes=, data, false) - record.save(:validate => false) + record.save(:validate => false) || raise(ActiveRecord::RecordNotSaved) record end diff --git a/spec/seeder_spec.rb b/spec/seeder_spec.rb index 24f3b9f..9ab36b4 100644 --- a/spec/seeder_spec.rb +++ b/spec/seeder_spec.rb @@ -137,4 +137,8 @@ lambda { SeededModel.create!(:title => "Bla") }.should_not raise_error end end + + it "should raise an ActiveRecord::RecordNotSaved exception if any records fail to save" do + lambda { SeededModel.seed(:fail_to_save => true, :title => "Foo") }.should raise_error(ActiveRecord::RecordNotSaved) + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7251d57..43a7547 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,6 +23,9 @@ class SeededModel < ActiveRecord::Base validates_presence_of :title attr_protected :first_name + attr_accessor :fail_to_save + + before_save { false if fail_to_save } end RSpec.configure do |config|