Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add the attributes for the parent factory before those for the child,
so that the child can reference parent attributes; fixes thoughtbotgh-15
  • Loading branch information
Ben Taitelbaum authored and jferris committed Sep 7, 2010
1 parent b26a439 commit 7c649b2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/factory_girl/factory.rb
Expand Up @@ -55,11 +55,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

def define_attribute(attribute)
Expand Down
13 changes: 13 additions & 0 deletions spec/factory_girl/factory_spec.rb
Expand Up @@ -196,6 +196,19 @@
child.attributes.size.should == 1
child.attributes.first.should == @child_attr
end

it "should allow to use parent attributes in defining additional attributes" do
User.class_eval { attr_accessor :name, :email }

child = FactoryGirl::Factory.new(:child)
@child_attr = FactoryGirl::Attribute::Dynamic.new(:email, lambda {|u| "#{u.name}@example.com"})
child.define_attribute(@child_attr)
child.inherit_from(@factory)
child.attributes.size.should == 2

result = child.run(FactoryGirl::Proxy::Build, {})
result.email.should == 'value@example.com'
end
end

it "inherit all callbacks" do
Expand Down

0 comments on commit 7c649b2

Please sign in to comment.