From 24d417d5754a82a796bc45f0bf765df844c8d206 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Fri, 30 Mar 2012 12:01:51 -0400 Subject: [PATCH] Add a before_create callback Closes #323 --- lib/factory_girl.rb | 2 +- lib/factory_girl/strategy/create.rb | 1 + spec/factory_girl/strategy/create_spec.rb | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/factory_girl.rb b/lib/factory_girl.rb index 7754499f5..a13325fec 100644 --- a/lib/factory_girl.rb +++ b/lib/factory_girl.rb @@ -64,6 +64,6 @@ def self.trait_by_name(name) end def self.callback_names - [:after_build, :after_create, :after_stub].freeze + [:after_build, :after_create, :after_stub, :before_create].freeze end end diff --git a/lib/factory_girl/strategy/create.rb b/lib/factory_girl/strategy/create.rb index 4cf2be192..055120883 100644 --- a/lib/factory_girl/strategy/create.rb +++ b/lib/factory_girl/strategy/create.rb @@ -8,6 +8,7 @@ def association(runner) def result(attribute_assigner, to_create) attribute_assigner.object.tap do |result_instance| run_callbacks(:after_build, result_instance) + run_callbacks(:before_create, result_instance) to_create[result_instance] run_callbacks(:after_create, result_instance) end diff --git a/spec/factory_girl/strategy/create_spec.rb b/spec/factory_girl/strategy/create_spec.rb index 5db5b5ea6..a124774ea 100644 --- a/spec/factory_girl/strategy/create_spec.rb +++ b/spec/factory_girl/strategy/create_spec.rb @@ -2,7 +2,7 @@ describe FactoryGirl::Strategy::Create do it_should_behave_like "strategy with association support", FactoryGirl::Strategy::Create - it_should_behave_like "strategy with callbacks", :after_build, :after_create + it_should_behave_like "strategy with callbacks", :after_build, :before_create, :after_create it "runs a custom create block" do block_run = false