Skip to content

Commit

Permalink
Backport fix from master for attribute/callback inheritance order
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Jan 11, 2011
1 parent 543ec4e commit 3923d20
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/factory_girl/factory.rb
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion spec/factory_girl/factory_spec.rb
Expand Up @@ -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' }
Expand All @@ -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

Expand Down

0 comments on commit 3923d20

Please sign in to comment.