2012-05-05 01:14:21 -04:00
|
|
|
require 'active_support/core_ext/hash/keys'
|
|
|
|
require 'active_support/inflector'
|
2011-09-23 15:00:00 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
module FactoryGirl
|
2012-05-05 02:31:31 -04:00
|
|
|
# @api private
|
2010-06-24 09:45:57 -04:00
|
|
|
class Factory
|
2012-05-05 02:31:31 -04:00
|
|
|
attr_reader :name, :definition
|
2011-08-12 10:35:41 -04:00
|
|
|
|
2012-05-05 02:31:31 -04:00
|
|
|
def initialize(name, options = {})
|
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
|
|
|
|
|
2012-05-11 11:43:08 -04:00
|
|
|
delegate :add_callback, :declare_attribute, :to_create, :define_trait, :constructor,
|
2012-06-12 23:11:55 -04:00
|
|
|
:defined_traits, :inherit_traits, :append_traits, to: :@definition
|
2011-10-14 22:34:51 -04:00
|
|
|
|
2012-05-05 02:31:31 -04:00
|
|
|
def build_class
|
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-05-05 02:31:31 -04:00
|
|
|
def run(build_strategy, overrides, &block)
|
2012-04-20 16:59:39 -04:00
|
|
|
block ||= ->(result) { result }
|
2011-12-04 01:34:50 -05:00
|
|
|
compile
|
2011-11-20 21:42:59 -05:00
|
|
|
|
2012-05-04 13:36:46 -04:00
|
|
|
strategy = StrategyCalculator.new(build_strategy).strategy.new
|
2011-12-16 11:15:13 -05:00
|
|
|
|
2012-07-24 18:50:11 -04:00
|
|
|
evaluator = evaluator_class.new(strategy, overrides.symbolize_keys)
|
2012-05-12 18:53:17 -04:00
|
|
|
attribute_assigner = AttributeAssigner.new(evaluator, build_class, &compiled_constructor)
|
2011-12-16 11:15:13 -05:00
|
|
|
|
2012-05-06 16:56:37 -04:00
|
|
|
evaluation = Evaluation.new(attribute_assigner, compiled_to_create)
|
2012-04-20 16:41:27 -04:00
|
|
|
evaluation.add_observer(CallbacksObserver.new(callbacks, evaluator))
|
2012-04-13 14:20:19 -04:00
|
|
|
|
|
|
|
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
|
2012-07-11 21:32:03 -04:00
|
|
|
build_hierarchy
|
2012-01-08 00:57:40 -05:00
|
|
|
@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|
|
2012-05-06 16:56:37 -04:00
|
|
|
factory_with_traits.append_traits traits
|
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
|
|
|
|
end
|
|
|
|
|
2011-10-07 15:00:38 -04:00
|
|
|
protected
|
|
|
|
|
2012-05-05 02:31:31 -04:00
|
|
|
def class_name
|
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|
|
2012-06-12 23:11:55 -04:00
|
|
|
list.apply_attributes definition.attributes
|
2011-10-07 16:43:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-11 21:32:03 -04:00
|
|
|
def hierarchy_class
|
|
|
|
@hierarchy_class ||= Class.new(parent.hierarchy_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def hierarchy_instance
|
|
|
|
@hierarchy_instance ||= hierarchy_class.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_hierarchy
|
2012-09-14 08:22:28 -04:00
|
|
|
hierarchy_class.build_from_definition definition
|
2012-07-11 21:32:03 -04:00
|
|
|
end
|
|
|
|
|
2011-10-07 16:06:28 -04:00
|
|
|
def callbacks
|
2012-07-11 21:32:03 -04:00
|
|
|
hierarchy_instance.callbacks
|
2011-10-07 16:06:28 -04:00
|
|
|
end
|
|
|
|
|
2012-05-11 11:43:08 -04:00
|
|
|
def compiled_to_create
|
2012-07-11 21:32:03 -04:00
|
|
|
hierarchy_instance.to_create
|
2012-05-11 11:43:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def compiled_constructor
|
2012-07-11 21:32:03 -04:00
|
|
|
hierarchy_instance.constructor
|
2012-01-20 13:04:48 -05:00
|
|
|
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
|
|
|
|
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
|
2012-07-11 21:32:03 -04:00
|
|
|
@hierarchy_class = nil
|
|
|
|
@hierarchy_instance = nil
|
|
|
|
@compiled = false
|
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
|