Skip to content

Commit

Permalink
Make Factory#attributes protected
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Oct 7, 2011
1 parent 1153783 commit 3bef858
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 80 deletions.
24 changes: 12 additions & 12 deletions lib/factory_girl/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,6 @@ def add_callback(name, &block)
@attribute_list.add_callback(Callback.new(name, block))
end

def attributes
ensure_compiled
AttributeList.new.tap do |list|
@traits.reverse.map { |name| trait_by_name(name) }.each do |trait|
list.apply_attributes(trait.attributes)
end

list.apply_attributes(@attribute_list)
list.apply_attributes(parent.attributes) if parent
end
end

def run(proxy_class, overrides) #:nodoc:
ensure_compiled
proxy = proxy_class.new(build_class)
Expand Down Expand Up @@ -149,6 +137,18 @@ def add_child(factory)
@children << factory unless @children.include?(factory)
end

def attributes
ensure_compiled
AttributeList.new.tap do |list|
@traits.reverse.map { |name| trait_by_name(name) }.each do |trait|
list.apply_attributes(trait.attributes)
end

list.apply_attributes(@attribute_list)
list.apply_attributes(parent.attributes) if parent
end
end

private

def callbacks
Expand Down
21 changes: 21 additions & 0 deletions spec/acceptance/attribute_existing_on_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "spec_helper"

describe "declaring attributes on a Factory that are private methods on Object" do
before do
define_model("Website", :system => :boolean, :link => :string, :sleep => :integer)

FactoryGirl.define do
factory :website do
system false
link "http://example.com"
sleep 15
end
end
end

subject { FactoryGirl.build(:website, :sleep => -5) }

its(:system) { should == false }
its(:link) { should == "http://example.com" }
its(:sleep) { should == -5 }
end
19 changes: 0 additions & 19 deletions spec/factory_girl/definition_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@
let(:factory) { FactoryGirl::Factory.new(:object) }
subject { FactoryGirl::DefinitionProxy.new(factory) }

def attributes
factory.attributes
end

it "should add a static attribute for type" do
subject.type 'value'
attributes.to_a.last.should be_kind_of(FactoryGirl::Attribute::Static)
end

it "should add a static attribute for id" do
subject.id 'value'
attributes.to_a.last.should be_kind_of(FactoryGirl::Attribute::Static)
end

it "should add a static attribute when an attribute is defined with a value" do
attribute = stub('attribute', :name => :name)
FactoryGirl::Attribute::Static.stubs(:new => attribute)
Expand Down Expand Up @@ -45,11 +31,6 @@ def attributes
}.should raise_error(FactoryGirl::AttributeDefinitionError)
end

it "should add an attribute with a built-in private method" do
subject.instance_eval { sleep(0.1) }
attributes.map { |attribute| attribute.name }.should == [:sleep]
end

describe "child factories" do
its(:child_factories) { should == [] }

Expand Down
50 changes: 1 addition & 49 deletions spec/factory_girl/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
factory = FactoryGirl::Factory.new(:post)
lambda {
factory.declare_attribute(FactoryGirl::Declaration::Association.new(:parent, { :factory => :post }))
factory.attributes
factory.ensure_compiled
}.should raise_error(FactoryGirl::AssociationDefinitionError)
end

Expand Down Expand Up @@ -117,54 +117,6 @@
child.ensure_compiled
child.build_class.should == String
end

describe "given a parent with attributes" do
before do
@parent_attr = :name
@factory.declare_attribute(FactoryGirl::Declaration::Static.new(@parent_attr, 'value'))
end

it "should create a new factory with attributes of the parent" do
child = FactoryGirl::Factory.new(:child, :parent => @factory.name)
child.ensure_compiled
child.attributes.size.should == 1
child.attributes.first.name.should == @parent_attr
end

it "should allow a child to define additional attributes" do
child = FactoryGirl::Factory.new(:child, :parent => @factory.name)
child.declare_attribute(FactoryGirl::Declaration::Static.new(:email, 'value'))
child.ensure_compiled
child.attributes.size.should == 2
end

it "should allow to override parent attributes" do
child = FactoryGirl::Factory.new(:child, :parent => @factory.name)
@child_declaration = FactoryGirl::Declaration::Static.new(@parent_attr, 'overridden')
child.declare_attribute(@child_declaration)
child.ensure_compiled
child.attributes.size.should == 1
child.attributes.first.should == @child_declaration.to_attributes.first
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, :parent => @factory.name)
@child_declaration = FactoryGirl::Declaration::Dynamic.new(:email, lambda {|u| "#{u.name}@example.com"})
child.declare_attribute(@child_declaration)
child.ensure_compiled
child.attributes.size.should == 2

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

it "compiles when looking for attributes" do
@factory.declare_attribute(FactoryGirl::Declaration::Static.new("name", "value"))
@factory.attributes.size.should == 1
end
end

describe FactoryGirl::Factory, "when defined with a custom class" do
Expand Down

0 comments on commit 3bef858

Please sign in to comment.