2011-09-23 15:00:00 -04:00
|
|
|
require "active_support/core_ext/hash/keys"
|
2011-10-14 22:34:51 -04:00
|
|
|
require "active_support/core_ext/module/delegation"
|
2011-09-23 15:00:00 -04:00
|
|
|
require "active_support/inflector"
|
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
module FactoryGirl
|
|
|
|
class Factory
|
2010-10-02 00:00:58 -04:00
|
|
|
attr_reader :name #: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-09-23 16:33:39 -04:00
|
|
|
@name = name.to_s.underscore.to_sym
|
|
|
|
@parent = options[:parent]
|
|
|
|
@aliases = options[:aliases] || []
|
2011-09-23 16:57:56 -04:00
|
|
|
@traits = options[:traits] || []
|
2011-09-23 16:33:39 -04:00
|
|
|
@class_name = options[:class]
|
|
|
|
@default_strategy = options[:default_strategy]
|
|
|
|
@defined_traits = []
|
|
|
|
@attribute_list = AttributeList.new
|
|
|
|
@compiled = false
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
|
2011-10-14 22:50:37 -04:00
|
|
|
delegate :overridable?, :declarations, :declare_attribute, :add_callback, :to => :@attribute_list
|
2011-10-14 22:34:51 -04:00
|
|
|
|
2011-10-07 16:15:15 -04:00
|
|
|
def factory_name
|
2011-10-09 17:03:22 -04:00
|
|
|
$stderr.puts "DEPRECATION WARNING: factory.factory_name is deprecated; use factory.name instead."
|
2011-10-07 16:15:15 -04:00
|
|
|
name
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_class #:nodoc:
|
|
|
|
@build_class ||= class_name.to_s.camelize.constantize
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_strategy #:nodoc:
|
|
|
|
@default_strategy || (parent && parent.default_strategy) || :create
|
|
|
|
end
|
|
|
|
|
2011-09-02 12:05:00 -04:00
|
|
|
def allow_overrides
|
2011-10-07 15:00:38 -04:00
|
|
|
@compiled = false
|
2011-09-02 12:05:00 -04:00
|
|
|
@attribute_list.overridable
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-08-12 16:16:17 -04:00
|
|
|
def define_trait(trait)
|
2011-09-23 13:14:02 -04:00
|
|
|
@defined_traits << trait
|
2011-08-09 20:29:02 -04:00
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
|
2011-10-12 11:35:10 -04:00
|
|
|
def run(proxy_class, overrides, &block) #:nodoc:
|
2011-09-23 17:57:20 -04:00
|
|
|
ensure_compiled
|
2011-10-20 11:29:57 -04:00
|
|
|
|
|
|
|
runner_options = {
|
|
|
|
:attributes => attributes,
|
|
|
|
:callbacks => callbacks,
|
|
|
|
:to_create => @to_create_block,
|
|
|
|
:build_class => build_class,
|
|
|
|
:proxy_class => proxy_class
|
|
|
|
}
|
|
|
|
|
|
|
|
result = Runner.new(runner_options).run(overrides)
|
2011-10-12 11:35:10 -04:00
|
|
|
|
|
|
|
block ? block.call(result) : result
|
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
|
2011-05-19 10:56:45 -04:00
|
|
|
attributes.select {|attribute| attribute.association? }
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
2009-09-15 16:56:20 -04:00
|
|
|
|
2011-08-12 16:16:17 -04:00
|
|
|
def trait_by_name(name)
|
|
|
|
if existing_attribute = trait_for(name)
|
2011-08-12 10:46:15 -04:00
|
|
|
existing_attribute
|
2011-10-07 15:00:38 -04:00
|
|
|
elsif parent
|
|
|
|
parent.trait_by_name(name)
|
2011-08-12 10:35:41 -04:00
|
|
|
else
|
2011-08-12 16:16:17 -04:00
|
|
|
FactoryGirl.trait_by_name(name)
|
2011-08-09 20:29:02 -04:00
|
|
|
end
|
|
|
|
end
|
2011-08-12 10:35:41 -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:
|
|
|
|
#
|
|
|
|
# factory :user, :aliases => [:author] do
|
|
|
|
# # ...
|
|
|
|
# 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:
|
|
|
|
#
|
|
|
|
# factory :user, :aliases => [:author] do
|
|
|
|
# # ...
|
|
|
|
# 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
|
|
|
|
|
2010-11-12 16:21:16 -05:00
|
|
|
def to_create(&block)
|
|
|
|
@to_create_block = block
|
|
|
|
end
|
|
|
|
|
2011-10-07 15:00:38 -04:00
|
|
|
def ensure_compiled
|
|
|
|
compile unless @compiled
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def class_name #:nodoc:
|
2011-10-07 15:15:04 -04:00
|
|
|
@class_name || (parent && parent.class_name) || name
|
2011-10-07 15:00:38 -04:00
|
|
|
end
|
|
|
|
|
2011-10-07 16:43:36 -04:00
|
|
|
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
|
|
|
|
|
2011-10-07 15:00:38 -04:00
|
|
|
private
|
|
|
|
|
2011-10-07 16:06:28 -04:00
|
|
|
def callbacks
|
|
|
|
attributes.callbacks
|
|
|
|
end
|
|
|
|
|
2011-09-23 13:14:02 -04:00
|
|
|
def compile
|
2011-10-07 15:00:38 -04:00
|
|
|
inherit_factory(parent) if parent
|
2011-09-23 17:57:20 -04:00
|
|
|
|
2011-09-23 13:14:02 -04:00
|
|
|
declarations.each do |declaration|
|
|
|
|
declaration.to_attributes.each do |attribute|
|
|
|
|
define_attribute(attribute)
|
|
|
|
end
|
|
|
|
end
|
2011-09-23 17:21:31 -04:00
|
|
|
|
2011-09-23 13:14:02 -04:00
|
|
|
@compiled = true
|
|
|
|
end
|
|
|
|
|
2011-09-23 17:57:20 -04:00
|
|
|
def inherit_factory(parent) #:nodoc:
|
|
|
|
parent.ensure_compiled
|
2011-10-14 22:34:51 -04:00
|
|
|
allow_overrides if parent.overridable?
|
2011-09-23 13:14:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def define_attribute(attribute)
|
|
|
|
if attribute.respond_to?(:factory) && attribute.factory == self.name
|
|
|
|
raise AssociationDefinitionError, "Self-referencing association '#{attribute.name}' in factory '#{self.name}'"
|
|
|
|
end
|
|
|
|
|
|
|
|
@attribute_list.define_attribute(attribute)
|
2011-09-02 12:05:00 -04:00
|
|
|
end
|
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
def assert_valid_options(options)
|
2011-09-23 15:00:00 -04:00
|
|
|
options.assert_valid_keys(:class, :parent, :default_strategy, :aliases, :traits)
|
|
|
|
|
2011-01-19 19:26:27 -05:00
|
|
|
if options[:default_strategy]
|
2011-10-20 16:21:50 -04:00
|
|
|
Proxy.ensure_strategy_exists!(options[:default_strategy])
|
2011-10-09 17:03:22 -04:00
|
|
|
$stderr.puts "DEPRECATION WARNING: default_strategy is deprecated."
|
|
|
|
$stderr.puts "Override to_create if you need to prevent a call to #save!."
|
2011-01-19 19:26:27 -05:00
|
|
|
end
|
2008-12-11 15:54:33 -05:00
|
|
|
end
|
2010-06-07 15:51:18 -04:00
|
|
|
|
2011-08-12 16:16:17 -04:00
|
|
|
def trait_for(name)
|
2011-09-23 13:14:02 -04:00
|
|
|
@defined_traits.detect {|trait| trait.name == name }
|
|
|
|
end
|
2011-10-07 15:00:38 -04:00
|
|
|
|
|
|
|
def parent
|
|
|
|
return unless @parent
|
|
|
|
FactoryGirl.factory_by_name(@parent)
|
|
|
|
end
|
2011-10-20 11:29:57 -04:00
|
|
|
|
|
|
|
class Runner
|
|
|
|
def initialize(options = {})
|
|
|
|
@attributes = options[:attributes]
|
|
|
|
@callbacks = options[:callbacks]
|
|
|
|
@to_create = options[:to_create]
|
|
|
|
@build_class = options[:build_class]
|
|
|
|
@proxy_class = options[:proxy_class]
|
|
|
|
|
|
|
|
@overrides = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(overrides = {})
|
|
|
|
@overrides = overrides.symbolize_keys
|
|
|
|
|
|
|
|
apply_attributes
|
2011-10-20 12:00:01 -04:00
|
|
|
apply_remaining_overrides
|
2011-10-20 11:29:57 -04:00
|
|
|
|
|
|
|
proxy.result(@to_create)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def apply_attributes
|
|
|
|
@attributes.each do |attribute|
|
|
|
|
if overrides_for_attribute(attribute).any?
|
2011-10-20 12:00:01 -04:00
|
|
|
handle_attribute_with_overrides(attribute)
|
2011-10-20 11:29:57 -04:00
|
|
|
else
|
|
|
|
handle_attribute_without_overrides(attribute)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-20 12:00:01 -04:00
|
|
|
def apply_remaining_overrides
|
|
|
|
@overrides.each { |attr, val| proxy.set(attr, val) }
|
|
|
|
end
|
|
|
|
|
2011-10-20 11:29:57 -04:00
|
|
|
def overrides_for_attribute(attribute)
|
|
|
|
@overrides.select { |attr, val| attribute.aliases_for?(attr) }
|
|
|
|
end
|
|
|
|
|
2011-10-20 12:00:01 -04:00
|
|
|
def handle_attribute_with_overrides(attribute)
|
2011-10-20 11:29:57 -04:00
|
|
|
overrides_for_attribute(attribute).each do |attr, val|
|
|
|
|
if attribute.ignored
|
|
|
|
proxy.set_ignored(attr, val)
|
|
|
|
else
|
|
|
|
proxy.set(attr, val)
|
|
|
|
end
|
|
|
|
|
|
|
|
@overrides.delete(attr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_attribute_without_overrides(attribute)
|
|
|
|
attribute.add_to(proxy)
|
|
|
|
end
|
|
|
|
|
|
|
|
def proxy
|
2011-10-20 14:19:09 -04:00
|
|
|
@proxy ||= @proxy_class.new(@build_class, @callbacks)
|
2011-10-20 11:29:57 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
2008-05-28 18:20:25 -04:00
|
|
|
end
|