Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to validate on save #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/seed-fu/active_record_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down
3 changes: 2 additions & 1 deletion lib/seed-fu/seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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

Expand Down
33 changes: 32 additions & 1 deletion spec/seeder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down