Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: thoughtbot/factory_girl
...
head fork: gregfreeman/factory_girl
  • 1 commit
  • 3 files changed
  • 0 commit comments
  • 1 contributor
4 lib/factory_girl/definition_proxy.rb
View
@@ -135,6 +135,10 @@ def association(name, options = {})
@factory.define_attribute(Attribute::Association.new(name, factory_name, options))
end
+ def before_build(&block)
+ @factory.add_callback(:before_build, &block)
+ end
+
def after_build(&block)
@factory.add_callback(:after_build, &block)
end
11 lib/factory_girl/factory.rb
View
@@ -65,19 +65,24 @@ def define_attribute(attribute)
end
def add_callback(name, &block)
- unless [:after_build, :after_create, :after_stub].include?(name.to_sym)
- raise InvalidCallbackNameError, "#{name} is not a valid callback name. Valid callback names are :after_build, :after_create, and :after_stub"
+ unless [:after_build, :after_create, :after_stub, :before_build].include?(name.to_sym)
+ raise InvalidCallbackNameError, "#{name} is not a valid callback name. Valid callback names are :before_build, :after_build, :after_create, and :after_stub"
end
@attributes << Attribute::Callback.new(name.to_sym, block)
end
def run(proxy_class, overrides) #:nodoc:
proxy = proxy_class.new(build_class)
+ before_callbacks = @attributes.collect {|k| k if k.kind_of?(FactoryGirl::Attribute::Callback) && k.name == :before_build }.compact
+ before_callbacks.each do |attribute|
+ attribute.add_to(proxy)
+ end
+ proxy.run_callbacks(:before_build) if before_callbacks.length >0
overrides = symbolize_keys(overrides)
overrides.each {|attr, val| proxy.set(attr, val) }
passed_keys = overrides.keys.collect {|k| FactoryGirl.aliases_for(k) }.flatten
@attributes.each do |attribute|
- unless passed_keys.include?(attribute.name)
+ unless passed_keys.include?(attribute.name) || before_callbacks.include?(attribute)
attribute.add_to(proxy)
end
end
40 spec/acceptance/callbacks_spec.rb
View
@@ -3,18 +3,31 @@
describe "callbacks" do
before do
- define_model("User", :first_name => :string, :last_name => :string)
+ define_model("User", :first_name => :string, :middle_name => :string, :last_name => :string)
FactoryGirl.define do
factory :user_with_callbacks, :class => :user do
after_stub { |user| user.first_name = 'Stubby' }
after_build { |user| user.first_name = 'Buildy' }
after_create { |user| user.last_name = 'Createy' }
+ before_build { |user| user.middle_name = 'Beforey' }
end
factory :user_with_inherited_callbacks, :parent => :user_with_callbacks do
after_stub { |user| user.last_name = 'Double-Stubby' }
end
+ factory :user_with_dynamic_values, :parent => :user_with_callbacks do
+ before_build { |user| user.last_name = 'Double-Stubby' }
+ end
+ factory :user_with_callback_dynamic, :class => :user do
+ before_build { |user| @dynamic_val='Dynamic Before' }
+ middle_name { |user| @dynamic_val }
+ end
+ factory :user_with_callback_override, :class => :user do
+ middle_name 'Before override'
+ before_build { |user| user.middle_name = 'Beforey' }
+
+ end
end
end
@@ -34,6 +47,31 @@
user.last_name.should == 'Createy'
end
+ it "runs the before_build callback when building" do
+ user = FactoryGirl.build(:user_with_callbacks)
+ user.middle_name.should == 'Beforey'
+ end
+
+ it "runs the before_build callback when creating" do
+ user = FactoryGirl.create(:user_with_callbacks)
+ user.middle_name.should == 'Beforey'
+ end
+
+ it "runs the before_build callback when stubbing" do
+ user = FactoryGirl.build_stubbed(:user_with_callbacks)
+ user.middle_name.should == 'Beforey'
+ end
+
+ it "runs the before_build callback when creating dynamically" do
+ user = FactoryGirl.create(:user_with_callback_dynamic)
+ user.middle_name.should == 'Dynamic Before'
+ end
+
+ it "runs the before_build callback does not run after attribute assignment" do
+ user = FactoryGirl.create(:user_with_callback_override)
+ user.middle_name.should == 'Before override'
+ end
+
it "runs both the after_stub callback on the factory and the inherited after_stub callback" do
user = FactoryGirl.build_stubbed(:user_with_inherited_callbacks)
user.first_name.should == 'Stubby'

No commit comments for this range

Something went wrong with that request. Please try again.