Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Backport fix from master for attribute/callback inheritance order

  • Loading branch information...
commit 3923d2072c203fc4043b7ad0a88fb80d436b3e27 1 parent 543ec4e
@jferris jferris authored
Showing with 21 additions and 2 deletions.
  1. +4 −1 lib/factory_girl/factory.rb
  2. +17 −1 spec/factory_girl/factory_spec.rb
View
5 lib/factory_girl/factory.rb
@@ -85,11 +85,14 @@ def initialize (name, options = {}) #:nodoc:
def inherit_from(parent) #:nodoc:
@options[:class] ||= parent.class_name
@options[:default_strategy] ||= parent.default_strategy
+
+ new_attributes = []
parent.attributes.each do |attribute|
unless attribute_defined?(attribute.name)
- @attributes << attribute.clone
+ new_attributes << attribute.clone
end
end
+ @attributes.unshift *new_attributes
end
# Adds an attribute that should be assigned on generated instances for this
View
18 spec/factory_girl/factory_spec.rb
@@ -528,6 +528,22 @@ class Other; end
child.attributes.first.should be_kind_of(Factory::Attribute::Dynamic)
end
+ it "should allow to use parent attributes in defining additional attributes" do
+ User.class_eval { attr_accessor :name, :email }
+
+ parent = Factory.define(:parent, :class => User) do |f|
+ f.name 'value'
+ end
+
+ child = Factory.new(:child)
+ child.add_attribute(:email) {|u| "#{u.name}@example.com" }
+ child.inherit_from(parent)
+ child.attributes.size.should == 2
+
+ result = child.run(Factory::Proxy::Build, {})
+ result.email.should == 'value@example.com'
+ end
+
it "inherit all callbacks" do
Factory.define(:child, :parent => :object) do |f|
f.after_stub {|o| o.name = 'Stubby' }
@@ -538,8 +554,8 @@ class Other; end
end
grandchild.attributes.size.should == 3
- grandchild.attributes.first.should be_kind_of(Factory::Attribute::Callback)
grandchild.attributes[1].should be_kind_of(Factory::Attribute::Callback)
+ grandchild.attributes[2].should be_kind_of(Factory::Attribute::Callback)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.