diff --git a/lib/seed-fu/active_record_extension.rb b/lib/seed-fu/active_record_extension.rb index 87c5167..ab9d982 100644 --- a/lib/seed-fu/active_record_extension.rb +++ b/lib/seed-fu/active_record_extension.rb @@ -29,7 +29,12 @@ module ActiveRecordExtension # { :x => 5, :y => 9, :name => "Office" } # ) def seed(*args, &block) - SeedFu::Seeder.new(self, *parse_seed_fu_args(args, block)).seed + do_seed({}, *args, &block) + end + + # Has the same behavior as {#seed}, but will run validations and raise an error if they fail. + def seed!(*args, &block) + do_seed({ :validate => true}, *args, &block) end # Has the same syntax as {#seed}, but if a record already exists with the same values for @@ -40,11 +45,20 @@ def seed(*args, &block) # Person.seed(:id, :id => 1, :name => "Bob") # => Name changed # Person.seed(:id, :id => 1, :name => "Harry") # => Name *not* changed def seed_once(*args, &block) - constraints, data = parse_seed_fu_args(args, block) - SeedFu::Seeder.new(self, constraints, data, :insert_only => true).seed + do_seed({:insert_only => true}, *args, &block) + end + + # Has the same behavior as {#seed_once}, but will run validations and raise an error if they fail. + def seed_once!(*args, &block) + do_seed({:insert_only => true, :validate => true}, *args, &block) end private + + def do_seed(options, *args, &block) + constraints, data = parse_seed_fu_args(args, block) + SeedFu::Seeder.new(self, constraints, data, options).seed + end def parse_seed_fu_args(args, block) if block.nil? diff --git a/lib/seed-fu/seeder.rb b/lib/seed-fu/seeder.rb index ef4f53c..c2fc45d 100644 --- a/lib/seed-fu/seeder.rb +++ b/lib/seed-fu/seeder.rb @@ -24,6 +24,7 @@ def initialize(model_class, constraints, data, options = {}) @options = options.symbolize_keys @options[:quiet] ||= SeedFu.quiet + @options[:validate] ||= false # replace nil with false validate_constraints! validate_data! @@ -65,7 +66,7 @@ def seed_record(data) puts " - #{@model_class} #{data.inspect}" unless @options[:quiet] record.assign_attributes(data, :without_protection => true) - record.save(:validate => false) || raise(ActiveRecord::RecordNotSaved) + record.save!(:validate => @options[:validate]) record end diff --git a/spec/seeder_spec.rb b/spec/seeder_spec.rb index cf89077..b5ef457 100644 --- a/spec/seeder_spec.rb +++ b/spec/seeder_spec.rb @@ -123,7 +123,38 @@ it "should not perform validation" do lambda { SeededModel.seed(:id => 1) }.should_not raise_error(ActiveRecord::RecordInvalid) end - + + it "should validate attributes with seed!" do + expect do + SeededModel.seed!(:id) do |s| + s.id = 1 + s.login = "bob" + end + end.to raise_error(ActiveRecord::RecordInvalid) + end + + it "should validate attributes with seed_once!" do + expect do + SeededModel.seed_once!(:id) do |s| + s.id = 1 + s.login = "bob" + end + end.to raise_error(ActiveRecord::RecordInvalid) + end + + it "should pass validation with seed!" do + expect do + SeededModel.seed!(:id) do |s| + s.id = 1 + s.login = "bob" + s.title = "Exists" + end + end.should_not raise_error(ActiveRecord::RecordInvalid) + + bob = SeededModel.find_by_id(1) + bob.title.should == 'Exists' + end + if ENV["DB"] == "postgresql" it "should update the primary key sequence after a records have been seeded" do id = SeededModel.connection.select_value("SELECT currval('seeded_models_id_seq')").to_i + 1