2011-09-23 15:00:00 -04:00
|
|
|
require "active_support/core_ext/hash/keys"
|
|
|
|
require "active_support/inflector"
|
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
module FactoryGirl
|
|
|
|
class Factory
|
2011-10-28 17:01:27 -04:00
|
|
|
attr_reader :name, :definition #:nodoc:
|
2011-08-12 10:35:41 -04:00
|
|
|
|
2010-10-02 00:00:58 -04:00
|
|
|
def initialize(name, options = {}) #:nodoc:
|
2010-06-24 09:45:57 -04:00
|
|
|
assert_valid_options(options)
|
2011-10-27 15:45:37 -04:00
|
|
|
@name = name.is_a?(Symbol) ? name : name.to_s.underscore.to_sym
|
2011-09-23 16:33:39 -04:00
|
|
|
@parent = options[:parent]
|
|
|
|
@aliases = options[:aliases] || []
|
|
|
|
@class_name = options[:class]
|
2012-01-17 23:15:41 -05:00
|
|
|
@definition = Definition.new(@name, options[:traits] || [])
|
2012-01-08 00:57:40 -05:00
|
|
|
@compiled = false
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
|
2011-10-28 17:01:27 -04:00
|
|
|
delegate :add_callback, :declare_attribute, :to_create, :define_trait,
|
2012-03-09 17:20:38 -05:00
|
|
|
:defined_traits, :inherit_traits, :processing_order, to: :@definition
|
2011-10-14 22:34:51 -04:00
|
|
|
|
2011-10-07 16:15:15 -04:00
|
|
|
def build_class #:nodoc:
|
2011-12-14 17:10:32 -05:00
|
|
|
@build_class ||= if class_name.is_a? Class
|
|
|
|
class_name
|
|
|
|
else
|
|
|
|
class_name.to_s.camelize.constantize
|
|
|
|
end
|
2011-10-07 16:15:15 -04:00
|
|
|
end
|
|
|
|
|
2012-02-08 10:17:57 -05:00
|
|
|
def run(strategy_class, overrides, &block) #:nodoc:
|
2011-11-20 21:42:59 -05:00
|
|
|
block ||= lambda {|result| result }
|
2011-12-04 01:34:50 -05:00
|
|
|
compile
|
2011-11-20 21:42:59 -05:00
|
|
|
|
2012-02-08 10:17:57 -05:00
|
|
|
strategy = strategy_class.new
|
2011-12-16 11:15:13 -05:00
|
|
|
|
2012-02-08 10:17:57 -05:00
|
|
|
evaluator = evaluator_class.new(strategy, overrides.symbolize_keys)
|
2012-04-19 21:34:47 -04:00
|
|
|
attribute_assigner = AttributeAssigner.new(evaluator, build_class, &instance_builder)
|
2011-12-16 11:15:13 -05:00
|
|
|
|
2012-04-13 14:20:19 -04:00
|
|
|
evaluation = Evaluation.new(attribute_assigner, to_create)
|
|
|
|
evaluation.add_observer(CallbackRunner.new(callbacks, evaluator))
|
|
|
|
|
|
|
|
strategy.result(evaluation).tap(&block)
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
2009-09-15 15:47:47 -04:00
|
|
|
|
2011-06-30 18:27:25 -04:00
|
|
|
def human_names
|
2011-09-23 15:00:00 -04:00
|
|
|
names.map {|name| name.to_s.humanize.downcase }
|
2009-09-15 15:47:47 -04:00
|
|
|
end
|
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
def associations
|
2012-02-08 08:14:34 -05:00
|
|
|
evaluator_class.attribute_list.associations
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
2009-09-15 16:56:20 -04:00
|
|
|
|
2011-01-25 17:55:40 -05:00
|
|
|
# Names for this factory, including aliases.
|
2010-11-11 17:34:01 -05:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
2012-03-09 17:20:38 -05:00
|
|
|
# factory :user, aliases: [:author] do
|
2010-11-11 17:34:01 -05:00
|
|
|
# # ...
|
|
|
|
# end
|
|
|
|
#
|
2011-01-26 20:55:06 -05:00
|
|
|
# FactoryGirl.create(:author).class
|
2010-11-11 17:34:01 -05:00
|
|
|
# # => User
|
|
|
|
#
|
|
|
|
# Because an attribute defined without a value or block will build an
|
|
|
|
# association with the same name, this allows associations to be defined
|
|
|
|
# without factories, such as:
|
|
|
|
#
|
2012-03-09 17:20:38 -05:00
|
|
|
# factory :user, aliases: [:author] do
|
2010-11-11 17:34:01 -05:00
|
|
|
# # ...
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# factory :post do
|
|
|
|
# author
|
|
|
|
# end
|
|
|
|
#
|
2011-01-26 20:55:06 -05:00
|
|
|
# FactoryGirl.create(:post).author.class
|
2010-11-11 17:34:01 -05:00
|
|
|
# # => User
|
2011-01-25 17:55:40 -05:00
|
|
|
def names
|
2011-09-23 16:33:39 -04:00
|
|
|
[name] + @aliases
|
2010-11-11 17:34:01 -05:00
|
|
|
end
|
|
|
|
|
2011-10-28 10:59:49 -04:00
|
|
|
def compile
|
2012-01-08 00:57:40 -05:00
|
|
|
unless @compiled
|
|
|
|
parent.compile
|
2012-03-04 14:28:09 -05:00
|
|
|
parent.defined_traits.each {|trait| define_trait(trait) }
|
2012-01-08 00:57:40 -05:00
|
|
|
@definition.compile
|
|
|
|
@compiled = true
|
|
|
|
end
|
2011-10-07 15:00:38 -04:00
|
|
|
end
|
|
|
|
|
Traits can be added to factories when the factory creates an instance
This allows for traits to be used with normal factories without having
to name every single factory that uses one (or many) traits.
So, instead of creating male_admin and female_admin factories:
FactoryGirl.define do
factory :user do
trait(:admin) { admin true }
trait(:male) { gender "Male" }
trait(:female) { gender "Female" }
factory :male_admin, :traits => [:male, :admin]
factory :female_admin, :traits => [:admin, :female]
end
end
FactoryGirl.create(:male_admin)
FactoryGirl.create(:female_admin)
You could just create a user with those traits assigned:
FactoryGirl.create(:user, :admin, :male)
FactoryGirl.create(:user, :admin, :female)
This can be combined with attribute overrides as expected.
FactoryGirl.create(:user, :admin, :male, :name => "John Doe")
FactoryGirl.create(:user, :admin, :female, :name => "Jane Doe")
2011-11-18 09:25:49 -05:00
|
|
|
def with_traits(traits)
|
|
|
|
self.clone.tap do |factory_with_traits|
|
|
|
|
factory_with_traits.inherit_traits traits
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-07 15:00:38 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def class_name #:nodoc:
|
2011-10-28 21:23:06 -04:00
|
|
|
@class_name || parent.class_name || name
|
2011-10-07 15:00:38 -04:00
|
|
|
end
|
|
|
|
|
2012-01-03 09:31:40 -05:00
|
|
|
def evaluator_class
|
2012-04-13 14:20:19 -04:00
|
|
|
@evaluator_class ||= EvaluatorClassDefiner.new(attributes, parent.evaluator_class).evaluator_class
|
2012-01-03 09:31:40 -05:00
|
|
|
end
|
|
|
|
|
2011-10-07 16:43:36 -04:00
|
|
|
def attributes
|
2011-10-28 10:59:49 -04:00
|
|
|
compile
|
2011-10-28 17:01:27 -04:00
|
|
|
AttributeList.new(@name).tap do |list|
|
2011-12-04 02:09:02 -05:00
|
|
|
processing_order.each do |factory|
|
|
|
|
list.apply_attributes factory.attributes
|
2011-10-07 16:43:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-07 16:06:28 -04:00
|
|
|
def callbacks
|
2012-04-13 14:20:19 -04:00
|
|
|
parent.callbacks + processing_order.map {|factory| factory.callbacks }.flatten
|
2011-10-07 16:06:28 -04:00
|
|
|
end
|
|
|
|
|
2012-01-20 13:04:48 -05:00
|
|
|
def constructor
|
|
|
|
@constructor ||= @definition.constructor || parent.constructor
|
|
|
|
end
|
|
|
|
|
2011-10-28 10:59:49 -04:00
|
|
|
private
|
2011-09-23 13:14:02 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
def assert_valid_options(options)
|
2012-03-09 15:24:40 -05:00
|
|
|
options.assert_valid_keys(:class, :parent, :aliases, :traits)
|
2008-12-11 15:54:33 -05:00
|
|
|
end
|
2010-06-07 15:51:18 -04:00
|
|
|
|
2011-10-07 15:00:38 -04:00
|
|
|
def parent
|
2011-10-28 21:23:06 -04:00
|
|
|
if @parent
|
|
|
|
FactoryGirl.factory_by_name(@parent)
|
|
|
|
else
|
|
|
|
NullFactory.new
|
|
|
|
end
|
2011-10-07 15:00:38 -04:00
|
|
|
end
|
2011-10-20 11:29:57 -04:00
|
|
|
|
2012-01-20 13:04:48 -05:00
|
|
|
def instance_builder
|
|
|
|
build_class = self.build_class
|
|
|
|
constructor || lambda { build_class.new }
|
|
|
|
end
|
|
|
|
|
Traits can be added to factories when the factory creates an instance
This allows for traits to be used with normal factories without having
to name every single factory that uses one (or many) traits.
So, instead of creating male_admin and female_admin factories:
FactoryGirl.define do
factory :user do
trait(:admin) { admin true }
trait(:male) { gender "Male" }
trait(:female) { gender "Female" }
factory :male_admin, :traits => [:male, :admin]
factory :female_admin, :traits => [:admin, :female]
end
end
FactoryGirl.create(:male_admin)
FactoryGirl.create(:female_admin)
You could just create a user with those traits assigned:
FactoryGirl.create(:user, :admin, :male)
FactoryGirl.create(:user, :admin, :female)
This can be combined with attribute overrides as expected.
FactoryGirl.create(:user, :admin, :male, :name => "John Doe")
FactoryGirl.create(:user, :admin, :female, :name => "Jane Doe")
2011-11-18 09:25:49 -05:00
|
|
|
def initialize_copy(source)
|
|
|
|
super
|
|
|
|
@definition = @definition.clone
|
2012-01-16 12:29:43 -05:00
|
|
|
@evaluator_class = nil
|
Traits can be added to factories when the factory creates an instance
This allows for traits to be used with normal factories without having
to name every single factory that uses one (or many) traits.
So, instead of creating male_admin and female_admin factories:
FactoryGirl.define do
factory :user do
trait(:admin) { admin true }
trait(:male) { gender "Male" }
trait(:female) { gender "Female" }
factory :male_admin, :traits => [:male, :admin]
factory :female_admin, :traits => [:admin, :female]
end
end
FactoryGirl.create(:male_admin)
FactoryGirl.create(:female_admin)
You could just create a user with those traits assigned:
FactoryGirl.create(:user, :admin, :male)
FactoryGirl.create(:user, :admin, :female)
This can be combined with attribute overrides as expected.
FactoryGirl.create(:user, :admin, :male, :name => "John Doe")
FactoryGirl.create(:user, :admin, :female, :name => "Jane Doe")
2011-11-18 09:25:49 -05:00
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
2008-05-28 18:20:25 -04:00
|
|
|
end
|