Skip to content

Commit

Permalink
Add NullFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Oct 31, 2011
1 parent 41bc3ac commit aee300a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/factory_girl.rb
Expand Up @@ -6,6 +6,7 @@
require 'factory_girl/proxy/attributes_for'
require 'factory_girl/proxy/stub'
require 'factory_girl/registry'
require 'factory_girl/null_factory'
require 'factory_girl/factory'
require 'factory_girl/attribute'
require 'factory_girl/attribute/static'
Expand Down
23 changes: 11 additions & 12 deletions lib/factory_girl/factory.rb
Expand Up @@ -29,7 +29,7 @@ def build_class #:nodoc:
end

def default_strategy #:nodoc:
@default_strategy || (parent && parent.default_strategy) || :create
@default_strategy || parent.default_strategy || :create
end

def allow_overrides
Expand Down Expand Up @@ -89,17 +89,15 @@ def names
end

def compile
if parent
parent.defined_traits.each {|trait| define_trait(trait) }
parent.compile
end
parent.defined_traits.each {|trait| define_trait(trait) }
parent.compile
attribute_list.ensure_compiled
end

protected

def class_name #:nodoc:
@class_name || (parent && parent.class_name) || name
@class_name || parent.class_name || name
end

def attributes
Expand All @@ -110,14 +108,12 @@ def attributes
end

list.apply_attribute_list(attribute_list)
list.apply_attribute_list(parent.attributes) if parent
list.apply_attribute_list(parent.attributes)
end
end

def callbacks
[traits.map(&:callbacks), @definition.callbacks].tap do |result|
result.unshift(*parent.callbacks) if parent
end.flatten
[parent.callbacks, traits.map(&:callbacks), @definition.callbacks].flatten
end

private
Expand All @@ -137,8 +133,11 @@ def traits
end

def parent
return unless @parent
FactoryGirl.factory_by_name(@parent)
if @parent
FactoryGirl.factory_by_name(@parent)
else
NullFactory.new
end
end

def attribute_list
Expand Down
19 changes: 19 additions & 0 deletions lib/factory_girl/null_factory.rb
@@ -0,0 +1,19 @@
module FactoryGirl
class NullFactory
attr_reader :definition

def initialize
@definition = Definition.new
end

delegate :defined_traits, :callbacks, :to => :definition

def compile; end
def default_strategy; end
def class_name; end

def attributes
AttributeList.new
end
end
end
11 changes: 11 additions & 0 deletions spec/factory_girl/null_factory_spec.rb
@@ -0,0 +1,11 @@
require "spec_helper"

describe FactoryGirl::NullFactory do
it { should delegate(:defined_traits).to(:definition) }
it { should delegate(:callbacks).to(:definition) }

its(:compile) { should be_nil }
its(:default_strategy) { should be_nil }
its(:class_name) { should be_nil }
its(:attributes) { should be_an_instance_of(FactoryGirl::AttributeList) }
end

0 comments on commit aee300a

Please sign in to comment.