Skip to content

Commit

Permalink
Move logic of what to do to create an instance to the definition wher…
Browse files Browse the repository at this point in the history
…e it belongs
  • Loading branch information
joshuaclayton committed Nov 25, 2011
1 parent e15ae8d commit 5bbbcb9
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/factory_girl/definition.rb
Expand Up @@ -6,7 +6,7 @@ def initialize(name = nil)
@declarations = DeclarationList.new(name)
@callbacks = []
@defined_traits = []
@to_create = nil
@to_create = lambda {|instance| instance.save! }
@traits = []
end

Expand Down
1 change: 0 additions & 1 deletion lib/factory_girl/proxy/create.rb
Expand Up @@ -4,7 +4,6 @@ class Create < Build #:nodoc:
def result(to_create)
super

to_create ||= lambda {|instance| instance.save! }
to_create[@instance]

run_callbacks(:after_create)
Expand Down
8 changes: 7 additions & 1 deletion spec/factory_girl/definition_spec.rb
Expand Up @@ -49,7 +49,13 @@
end

describe FactoryGirl::Definition, "#to_create" do
its(:to_create) { should be_nil }
its(:to_create) { should be_a(Proc) }

it "calls save! on the object when run" do
instance = stub("model instance", :save! => true)
subject.to_create[instance]
instance.should have_received(:save!).once
end

it "returns the assigned value when given a block" do
block = proc { nil }
Expand Down
2 changes: 1 addition & 1 deletion spec/factory_girl/factory_spec.rb
Expand Up @@ -271,7 +271,7 @@

it "returns the result from the proxy when running" do
subject.run(FactoryGirl::Proxy::Build, {}).should == "result"
proxy.should have_received(:result).with(nil)
proxy.should have_received(:result).with(subject.definition.to_create)
end

it "sets overrides once on the factory" do
Expand Down
8 changes: 1 addition & 7 deletions spec/factory_girl/proxy/create_spec.rb
Expand Up @@ -11,19 +11,13 @@
it_should_behave_like "proxy with callbacks", :after_build
it_should_behave_like "proxy with callbacks", :after_create

it "saves the instance before returning the result" do
subject.result(nil)
instance.should have_received(:save!)
end

it "runs a custom create block" do
block_run = false
block = lambda {|instance| block_run = true }
subject.result(block)
instance.should have_received(:save!).never
block_run.should be_true
end

end

describe FactoryGirl::Proxy::Create, "when running callbacks" do
Expand All @@ -38,7 +32,7 @@
subject { FactoryGirl::Proxy::Create.new(proxy_class, [after_create_one, after_create_two, after_build_one]) }

it "runs callbacks in the correct order" do
subject.result(nil)
subject.result(lambda {|instance| instance })
callback_result.should == [:after_build_one, :after_create_one, :after_create_two]
end
end
4 changes: 2 additions & 2 deletions spec/support/shared_examples/proxy.rb
Expand Up @@ -102,11 +102,11 @@
subject { described_class.new(proxy_class, [callback]) }

it "runs the #{callback_name} callback" do
subject.result(nil)
subject.result(lambda {|instance| instance })
callback_instance.should have_received(:foo).once
end

it "returns the proxy instance" do
subject.result(nil).should == instance
subject.result(lambda {|instance| instance }).should == instance
end
end

0 comments on commit 5bbbcb9

Please sign in to comment.