2008-05-28 18:20:25 -04:00
|
|
|
class Factory
|
2009-01-06 15:57:37 -05:00
|
|
|
|
2009-02-17 16:38:15 -05:00
|
|
|
# Raised when a factory is defined that attempts to instantiate itself.
|
2009-01-06 15:57:37 -05:00
|
|
|
class AssociationDefinitionError < RuntimeError
|
|
|
|
end
|
2008-07-22 19:46:01 -04:00
|
|
|
|
2008-12-11 15:54:33 -05:00
|
|
|
class << self
|
|
|
|
attr_accessor :factories #:nodoc:
|
|
|
|
|
|
|
|
# An Array of strings specifying locations that should be searched for
|
|
|
|
# factory definitions. By default, factory_girl will attempt to require
|
|
|
|
# "factories," "test/factories," and "spec/factories." Only the first
|
|
|
|
# existing file will be loaded.
|
|
|
|
attr_accessor :definition_file_paths
|
|
|
|
end
|
2008-05-28 18:54:54 -04:00
|
|
|
|
2008-12-11 15:54:33 -05:00
|
|
|
self.factories = {}
|
2008-08-20 10:20:41 -04:00
|
|
|
self.definition_file_paths = %w(factories test/factories spec/factories)
|
|
|
|
|
2009-02-17 16:38:15 -05:00
|
|
|
attr_reader :factory_name #:nodoc:
|
2009-01-02 15:45:34 -05:00
|
|
|
attr_reader :attributes #:nodoc:
|
2008-05-28 18:54:54 -04:00
|
|
|
|
2008-05-31 18:16:59 -04:00
|
|
|
# Defines a new factory that can be used by the build strategies (create and
|
|
|
|
# build) to build new objects.
|
2008-05-28 18:54:54 -04:00
|
|
|
#
|
|
|
|
# Arguments:
|
2009-02-17 16:38:15 -05:00
|
|
|
# * name: +Symbol+ or +String+
|
|
|
|
# A unique name used to identify this factory.
|
|
|
|
# * options: +Hash+
|
2008-05-28 18:54:54 -04:00
|
|
|
#
|
2009-02-17 16:38:15 -05:00
|
|
|
# Options:
|
|
|
|
# * class: +Symbol+, +Class+, or +String+
|
|
|
|
# The class that will be used when generating instances for this factory. If not specified, the class will be guessed from the factory name.
|
|
|
|
# * parent: +Symbol+
|
|
|
|
# The parent factory. If specified, the attributes from the parent
|
|
|
|
# factory will be copied to the current one with an ability to override
|
|
|
|
# them.
|
|
|
|
# * default_strategy: +Symbol+
|
|
|
|
# The strategy that will be used by the Factory shortcut method.
|
|
|
|
# Defaults to :create.
|
|
|
|
#
|
|
|
|
# Yields: +Factory+
|
|
|
|
# The newly created factory.
|
2008-05-28 21:07:51 -04:00
|
|
|
def self.define (name, options = {})
|
|
|
|
instance = Factory.new(name, options)
|
2008-05-28 18:54:54 -04:00
|
|
|
yield(instance)
|
2009-01-07 11:48:29 -05:00
|
|
|
if parent = options.delete(:parent)
|
|
|
|
instance.inherit_from(Factory.factory_by_name(parent))
|
|
|
|
end
|
2008-07-29 14:22:18 -04:00
|
|
|
self.factories[instance.factory_name] = instance
|
2008-05-28 18:54:54 -04:00
|
|
|
end
|
2009-01-07 11:48:29 -05:00
|
|
|
|
2009-02-17 16:38:15 -05:00
|
|
|
def class_name #:nodoc:
|
2009-01-07 11:48:29 -05:00
|
|
|
@options[:class] || factory_name
|
|
|
|
end
|
2008-05-28 18:54:54 -04:00
|
|
|
|
2008-05-29 01:11:33 -04:00
|
|
|
def build_class #:nodoc:
|
2009-01-07 11:48:29 -05:00
|
|
|
@build_class ||= class_for(class_name)
|
2008-05-28 21:07:51 -04:00
|
|
|
end
|
2009-01-08 11:43:07 -05:00
|
|
|
|
|
|
|
def default_strategy #:nodoc:
|
|
|
|
@options[:default_strategy] || :create
|
|
|
|
end
|
2008-05-28 21:07:51 -04:00
|
|
|
|
|
|
|
def initialize (name, options = {}) #:nodoc:
|
2008-12-11 15:54:33 -05:00
|
|
|
assert_valid_options(options)
|
2008-07-29 16:20:44 -04:00
|
|
|
@factory_name = factory_name_for(name)
|
2009-01-07 11:48:29 -05:00
|
|
|
@options = options
|
2008-07-30 13:46:06 -04:00
|
|
|
@attributes = []
|
2008-05-28 20:00:46 -04:00
|
|
|
end
|
2009-01-07 11:48:29 -05:00
|
|
|
|
2009-01-08 12:55:18 -05:00
|
|
|
def inherit_from(parent) #:nodoc:
|
2009-02-24 12:05:57 -05:00
|
|
|
@options[:class] ||= parent.class_name
|
2009-01-07 11:48:29 -05:00
|
|
|
parent.attributes.each do |attribute|
|
|
|
|
unless attribute_defined?(attribute.name)
|
|
|
|
@attributes << attribute.clone
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-05-28 20:00:46 -04:00
|
|
|
|
2008-05-31 18:16:59 -04:00
|
|
|
# Adds an attribute that should be assigned on generated instances for this
|
|
|
|
# factory.
|
2008-05-28 20:00:46 -04:00
|
|
|
#
|
|
|
|
# This method should be called with either a value or block, but not both. If
|
|
|
|
# called with a block, the attribute will be generated "lazily," whenever an
|
|
|
|
# instance is generated. Lazy attribute blocks will not be called if that
|
|
|
|
# attribute is overriden for a specific instance.
|
|
|
|
#
|
2009-01-02 16:39:24 -05:00
|
|
|
# When defining lazy attributes, an instance of Factory::Proxy will
|
2008-05-31 18:16:59 -04:00
|
|
|
# be yielded, allowing associations to be built using the correct build
|
|
|
|
# strategy.
|
|
|
|
#
|
2008-05-28 20:00:46 -04:00
|
|
|
# Arguments:
|
2009-02-17 16:38:15 -05:00
|
|
|
# * name: +Symbol+ or +String+
|
|
|
|
# The name of this attribute. This will be assigned using :"#{name}=" for
|
|
|
|
# generated instances.
|
|
|
|
# * value: +Object+
|
|
|
|
# If no block is given, this value will be used for this attribute.
|
2008-05-28 20:00:46 -04:00
|
|
|
def add_attribute (name, value = nil, &block)
|
2009-01-02 17:33:00 -05:00
|
|
|
if block_given?
|
|
|
|
if value
|
|
|
|
raise AttributeDefinitionError, "Both value and block given"
|
|
|
|
else
|
|
|
|
attribute = Attribute::Dynamic.new(name, block)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
attribute = Attribute::Static.new(name, value)
|
|
|
|
end
|
2008-07-30 14:04:43 -04:00
|
|
|
|
|
|
|
if attribute_defined?(attribute.name)
|
|
|
|
raise AttributeDefinitionError, "Attribute already defined: #{name}"
|
|
|
|
end
|
|
|
|
|
2008-07-30 13:46:06 -04:00
|
|
|
@attributes << attribute
|
2008-05-28 20:00:46 -04:00
|
|
|
end
|
|
|
|
|
2008-05-28 21:14:57 -04:00
|
|
|
# Calls add_attribute using the missing method name as the name of the
|
|
|
|
# attribute, so that:
|
|
|
|
#
|
|
|
|
# Factory.define :user do |f|
|
|
|
|
# f.name 'Billy Idol'
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# and:
|
|
|
|
#
|
|
|
|
# Factory.define :user do |f|
|
2008-06-10 15:38:18 -04:00
|
|
|
# f.add_attribute :name, 'Billy Idol'
|
2008-05-28 21:14:57 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# are equivilent.
|
|
|
|
def method_missing (name, *args, &block)
|
|
|
|
add_attribute(name, *args, &block)
|
|
|
|
end
|
|
|
|
|
2008-07-29 15:53:47 -04:00
|
|
|
# Adds an attribute that builds an association. The associated instance will
|
|
|
|
# be built using the same build strategy as the parent instance.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# Factory.define :user do |f|
|
|
|
|
# f.name 'Joey'
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Factory.define :post do |f|
|
|
|
|
# f.association :author, :factory => :user
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Arguments:
|
2009-02-17 16:38:15 -05:00
|
|
|
# * name: +Symbol+
|
|
|
|
# The name of this attribute.
|
|
|
|
# * options: +Hash+
|
|
|
|
#
|
|
|
|
# Options:
|
|
|
|
# * factory: +Symbol+ or +String+
|
|
|
|
# The name of the factory to use when building the associated instance.
|
|
|
|
# If no name is given, the name of the attribute is assumed to be the
|
|
|
|
# name of the factory. For example, a "user" association will by
|
|
|
|
# default use the "user" factory.
|
2008-07-29 15:53:47 -04:00
|
|
|
def association (name, options = {})
|
2009-01-02 18:00:07 -05:00
|
|
|
factory_name = options.delete(:factory) || name
|
2009-01-06 15:57:37 -05:00
|
|
|
if factory_name_for(factory_name) == self.factory_name
|
|
|
|
raise AssociationDefinitionError, "Self-referencing association '#{name}' in factory '#{self.factory_name}'"
|
|
|
|
end
|
2009-01-02 18:00:07 -05:00
|
|
|
@attributes << Attribute::Association.new(name, factory_name, options)
|
2008-07-29 15:53:47 -04:00
|
|
|
end
|
2009-02-17 16:38:15 -05:00
|
|
|
|
2009-01-06 17:31:44 -05:00
|
|
|
# Adds an attribute that will have unique values generated by a sequence with
|
|
|
|
# a specified format.
|
2009-02-17 16:38:15 -05:00
|
|
|
#
|
2009-01-06 17:31:44 -05:00
|
|
|
# The result of:
|
2009-02-17 16:38:15 -05:00
|
|
|
# Factory.define :user do |f|
|
|
|
|
# f.sequence(:email) { |n| "person#{n}@example.com" }
|
|
|
|
# end
|
2009-01-06 17:31:44 -05:00
|
|
|
#
|
|
|
|
# Is equal to:
|
2009-02-17 16:38:15 -05:00
|
|
|
# Factory.sequence(:email) { |n| "person#{n}@example.com" }
|
2009-01-06 17:31:44 -05:00
|
|
|
#
|
2009-02-17 16:38:15 -05:00
|
|
|
# Factory.define :user do |f|
|
|
|
|
# f.email { Factory.next(:email) }
|
|
|
|
# end
|
2009-01-06 17:31:44 -05:00
|
|
|
#
|
2009-02-17 16:38:15 -05:00
|
|
|
# Except that no globally available sequence will be defined.
|
2009-01-06 17:31:44 -05:00
|
|
|
def sequence (name, &block)
|
|
|
|
s = Sequence.new(&block)
|
|
|
|
add_attribute(name) { s.next }
|
|
|
|
end
|
2009-01-08 11:43:07 -05:00
|
|
|
|
2008-12-30 16:36:26 -05:00
|
|
|
# Generates and returns a Hash of attributes from this factory. Attributes
|
|
|
|
# can be individually overridden by passing in a Hash of attribute => value
|
|
|
|
# pairs.
|
|
|
|
#
|
|
|
|
# Arguments:
|
2009-02-17 16:38:15 -05:00
|
|
|
# * name: +Symbol+ or +String+
|
|
|
|
# The name of the factory that should be used.
|
|
|
|
# * overrides: +Hash+
|
|
|
|
# Attributes to overwrite for this set.
|
2008-12-30 16:36:26 -05:00
|
|
|
#
|
2009-02-17 16:38:15 -05:00
|
|
|
# Returns: +Hash+
|
|
|
|
# A set of attributes that can be used to build an instance of the class
|
|
|
|
# this factory generates.
|
2009-01-02 15:45:34 -05:00
|
|
|
def self.attributes_for (name, overrides = {})
|
2009-01-02 16:39:24 -05:00
|
|
|
factory_by_name(name).run(Proxy::AttributesFor, overrides)
|
2008-12-30 16:36:26 -05:00
|
|
|
end
|
2008-05-28 22:09:30 -04:00
|
|
|
|
2008-12-30 16:36:26 -05:00
|
|
|
# Generates and returns an instance from this factory. Attributes can be
|
|
|
|
# individually overridden by passing in a Hash of attribute => value pairs.
|
|
|
|
#
|
|
|
|
# Arguments:
|
2009-02-17 16:38:15 -05:00
|
|
|
# * name: +Symbol+ or +String+
|
|
|
|
# The name of the factory that should be used.
|
|
|
|
# * overrides: +Hash+
|
|
|
|
# Attributes to overwrite for this instance.
|
2008-12-30 16:36:26 -05:00
|
|
|
#
|
2009-02-17 16:38:15 -05:00
|
|
|
# Returns: +Object+
|
|
|
|
# An instance of the class this factory generates, with generated attributes
|
|
|
|
# assigned.
|
2009-01-02 15:45:34 -05:00
|
|
|
def self.build (name, overrides = {})
|
2009-01-02 16:39:24 -05:00
|
|
|
factory_by_name(name).run(Proxy::Build, overrides)
|
2008-12-30 16:36:26 -05:00
|
|
|
end
|
2008-05-28 22:09:30 -04:00
|
|
|
|
2008-12-30 16:36:26 -05:00
|
|
|
# Generates, saves, and returns an instance from this factory. Attributes can
|
|
|
|
# be individually overridden by passing in a Hash of attribute => value
|
|
|
|
# pairs.
|
|
|
|
#
|
2009-02-17 16:38:15 -05:00
|
|
|
# Instances are saved using the +save!+ method, so ActiveRecord models will
|
|
|
|
# raise ActiveRecord::RecordInvalid exceptions for invalid attribute sets.
|
2008-12-30 16:36:26 -05:00
|
|
|
#
|
|
|
|
# Arguments:
|
2009-02-17 16:38:15 -05:00
|
|
|
# * name: +Symbol+ or +String+
|
|
|
|
# The name of the factory that should be used.
|
|
|
|
# * overrides: +Hash+
|
|
|
|
# Attributes to overwrite for this instance.
|
2008-12-30 16:36:26 -05:00
|
|
|
#
|
2009-02-17 16:38:15 -05:00
|
|
|
# Returns: +Object+
|
|
|
|
# A saved instance of the class this factory generates, with generated
|
|
|
|
# attributes assigned.
|
2009-01-02 15:45:34 -05:00
|
|
|
def self.create (name, overrides = {})
|
2009-01-02 16:39:24 -05:00
|
|
|
factory_by_name(name).run(Proxy::Create, overrides)
|
2008-12-30 16:36:26 -05:00
|
|
|
end
|
2009-01-06 14:26:18 -05:00
|
|
|
|
2009-02-17 16:38:15 -05:00
|
|
|
# Generates and returns an object with all attributes from this factory
|
|
|
|
# stubbed out. Attributes can be individually overridden by passing in a Hash
|
|
|
|
# of attribute => value pairs.
|
2009-01-06 14:26:18 -05:00
|
|
|
#
|
|
|
|
# Arguments:
|
2009-02-17 16:38:15 -05:00
|
|
|
# * name: +Symbol+ or +String+
|
|
|
|
# The name of the factory that should be used.
|
|
|
|
# * overrides: +Hash+
|
|
|
|
# Attributes to overwrite for this instance.
|
2009-01-06 14:26:18 -05:00
|
|
|
#
|
2009-02-17 16:38:15 -05:00
|
|
|
# Returns: +Object+
|
|
|
|
# An object with generated attributes stubbed out.
|
2009-01-06 14:26:18 -05:00
|
|
|
def self.stub (name, overrides = {})
|
|
|
|
factory_by_name(name).run(Proxy::Stub, overrides)
|
2009-01-08 11:43:07 -05:00
|
|
|
end
|
|
|
|
|
2009-02-17 16:38:15 -05:00
|
|
|
# Executes the default strategy for the given factory. This is usually create,
|
|
|
|
# but it can be overridden for each factory.
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# * name: +Symbol+ or +String+
|
|
|
|
# The name of the factory that should be used.
|
|
|
|
# * overrides: +Hash+
|
|
|
|
# Attributes to overwrite for this instance.
|
|
|
|
#
|
|
|
|
# Returns: +Object+
|
|
|
|
# The result of the default strategy.
|
2009-01-08 11:43:07 -05:00
|
|
|
def self.default_strategy (name, overrides = {})
|
|
|
|
self.send(factory_by_name(name).default_strategy, name, overrides)
|
|
|
|
end
|
2008-05-28 22:09:30 -04:00
|
|
|
|
2008-12-30 16:36:26 -05:00
|
|
|
def self.find_definitions #:nodoc:
|
|
|
|
definition_file_paths.each do |path|
|
|
|
|
require("#{path}.rb") if File.exists?("#{path}.rb")
|
2008-10-24 12:14:13 -04:00
|
|
|
|
2008-12-30 16:36:26 -05:00
|
|
|
if File.directory? path
|
|
|
|
Dir[File.join(path, '*.rb')].each do |file|
|
|
|
|
require file
|
2008-08-20 10:20:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-05-28 22:09:30 -04:00
|
|
|
end
|
|
|
|
|
2009-01-02 16:39:24 -05:00
|
|
|
def run (proxy_class, overrides) #:nodoc:
|
|
|
|
proxy = proxy_class.new(build_class)
|
2008-12-30 16:31:26 -05:00
|
|
|
overrides = symbolize_keys(overrides)
|
2009-01-02 16:39:24 -05:00
|
|
|
overrides.each {|attr, val| proxy.set(attr, val) }
|
2008-12-30 16:31:26 -05:00
|
|
|
passed_keys = overrides.keys.collect {|k| Factory.aliases_for(k) }.flatten
|
2008-07-30 13:46:06 -04:00
|
|
|
@attributes.each do |attribute|
|
2008-07-30 15:47:12 -04:00
|
|
|
unless passed_keys.include?(attribute.name)
|
2009-01-02 17:33:00 -05:00
|
|
|
attribute.add_to(proxy)
|
2008-07-30 13:46:06 -04:00
|
|
|
end
|
2008-05-31 18:16:59 -04:00
|
|
|
end
|
2009-01-02 16:39:24 -05:00
|
|
|
proxy.result
|
2008-05-31 18:16:59 -04:00
|
|
|
end
|
|
|
|
|
2009-01-02 13:40:57 -05:00
|
|
|
def self.factory_by_name (name)
|
|
|
|
factories[name.to_sym] or raise ArgumentError.new("No such factory: #{name.to_s}")
|
|
|
|
end
|
2009-09-15 15:47:47 -04:00
|
|
|
|
|
|
|
def human_name(*args, &block)
|
|
|
|
if args.size == 0 && block.nil?
|
|
|
|
factory_name.to_s.gsub('_', ' ')
|
|
|
|
else
|
|
|
|
add_attribute(:human_name, *args, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-15 16:56:20 -04:00
|
|
|
def associations
|
|
|
|
attributes.select {|attribute| attribute.is_a?(Attribute::Association) }
|
|
|
|
end
|
|
|
|
|
2009-09-15 15:47:47 -04:00
|
|
|
private
|
|
|
|
|
2008-07-29 16:20:44 -04:00
|
|
|
def class_for (class_or_to_s)
|
|
|
|
if class_or_to_s.respond_to?(:to_sym)
|
2008-12-11 15:54:33 -05:00
|
|
|
Object.const_get(variable_name_to_class_name(class_or_to_s))
|
2008-07-29 16:20:44 -04:00
|
|
|
else
|
|
|
|
class_or_to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def factory_name_for (class_or_to_s)
|
|
|
|
if class_or_to_s.respond_to?(:to_sym)
|
|
|
|
class_or_to_s.to_sym
|
|
|
|
else
|
2008-12-11 15:54:33 -05:00
|
|
|
class_name_to_variable_name(class_or_to_s).to_sym
|
2008-07-29 16:20:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-30 14:04:43 -04:00
|
|
|
def attribute_defined? (name)
|
|
|
|
!@attributes.detect {|attr| attr.name == name }.nil?
|
|
|
|
end
|
|
|
|
|
2008-12-11 15:54:33 -05:00
|
|
|
def assert_valid_options(options)
|
2009-01-08 11:43:07 -05:00
|
|
|
invalid_keys = options.keys - [:class, :parent, :default_strategy]
|
2008-12-11 15:54:33 -05:00
|
|
|
unless invalid_keys == []
|
|
|
|
raise ArgumentError, "Unknown arguments: #{invalid_keys.inspect}"
|
|
|
|
end
|
2009-01-08 11:43:07 -05:00
|
|
|
assert_valid_strategy(options[:default_strategy]) if options[:default_strategy]
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_valid_strategy(strategy)
|
|
|
|
unless Factory::Proxy.const_defined? variable_name_to_class_name(strategy)
|
|
|
|
raise ArgumentError, "Unknown strategy: #{strategy}"
|
|
|
|
end
|
2008-12-11 15:54:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Based on ActiveSupport's underscore inflector
|
|
|
|
def class_name_to_variable_name(name)
|
|
|
|
name.to_s.gsub(/::/, '/').
|
|
|
|
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
|
|
|
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
|
|
|
tr("-", "_").
|
|
|
|
downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
# Based on ActiveSupport's camelize inflector
|
|
|
|
def variable_name_to_class_name(name)
|
|
|
|
name.to_s.
|
|
|
|
gsub(/\/(.?)/) { "::#{$1.upcase}" }.
|
|
|
|
gsub(/(?:^|_)(.)/) { $1.upcase }
|
|
|
|
end
|
|
|
|
|
|
|
|
# From ActiveSupport
|
|
|
|
def symbolize_keys(hash)
|
|
|
|
hash.inject({}) do |options, (key, value)|
|
|
|
|
options[(key.to_sym rescue key) || key] = value
|
|
|
|
options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-28 18:20:25 -04:00
|
|
|
end
|