2010-06-24 09:45:57 -04:00
|
|
|
module FactoryGirl
|
|
|
|
# Raised when a factory is defined that attempts to instantiate itself.
|
|
|
|
class AssociationDefinitionError < RuntimeError
|
2009-01-07 11:48:29 -05:00
|
|
|
end
|
2008-05-28 18:54:54 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
# Raised when a callback is defined that has an invalid name
|
|
|
|
class InvalidCallbackNameError < RuntimeError
|
2008-05-28 21:07:51 -04:00
|
|
|
end
|
2010-06-07 15:51:18 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
# Raised when a factory is defined with the same name as a previously-defined factory.
|
|
|
|
class DuplicateDefinitionError < RuntimeError
|
2009-01-08 11:43:07 -05:00
|
|
|
end
|
2008-05-28 21:07:51 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
class Factory
|
2010-10-02 00:00:58 -04:00
|
|
|
attr_reader :name #:nodoc:
|
2010-06-24 09:45:57 -04:00
|
|
|
attr_reader :attributes #:nodoc:
|
2011-08-12 10:35:41 -04:00
|
|
|
|
2010-10-02 00:00:58 -04:00
|
|
|
def factory_name
|
|
|
|
puts "WARNING: factory.factory_name is deprecated. Use factory.name instead."
|
|
|
|
name
|
|
|
|
end
|
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
def class_name #:nodoc:
|
2010-10-02 00:00:58 -04:00
|
|
|
@options[:class] || name
|
2009-01-07 11:48:29 -05:00
|
|
|
end
|
2008-05-28 20:00:46 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
def build_class #:nodoc:
|
|
|
|
@build_class ||= class_for(class_name)
|
2009-01-06 15:57:37 -05:00
|
|
|
end
|
2010-06-07 15:51:18 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
def default_strategy #:nodoc:
|
|
|
|
@options[:default_strategy] || :create
|
2009-10-10 00:46:19 -04:00
|
|
|
end
|
2010-06-07 15:51:18 -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)
|
2010-10-02 00:00:58 -04:00
|
|
|
@name = factory_name_for(name)
|
2011-08-10 18:19:25 -04:00
|
|
|
@parent = options[:parent]
|
2010-10-02 00:00:58 -04:00
|
|
|
@options = options
|
|
|
|
@attributes = []
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
2008-10-24 12:14:13 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
def inherit_from(parent) #:nodoc:
|
|
|
|
@options[:class] ||= parent.class_name
|
|
|
|
@options[:default_strategy] ||= parent.default_strategy
|
2010-09-03 12:07:37 -04:00
|
|
|
|
2011-08-12 10:35:41 -04:00
|
|
|
apply_attributes(parent.attributes)
|
|
|
|
sort_attributes!
|
2011-08-09 20:29:02 -04:00
|
|
|
end
|
2011-08-12 10:35:41 -04:00
|
|
|
|
2011-08-09 20:29:02 -04:00
|
|
|
def apply_attribute_groups(groups)
|
2011-08-12 10:35:41 -04:00
|
|
|
groups.reverse.map { |name| attribute_group_by_name(name) }.each do |group|
|
|
|
|
apply_attributes(group.attributes)
|
2011-08-09 20:29:02 -04:00
|
|
|
end
|
2011-08-12 10:35:41 -04:00
|
|
|
sort_attributes!
|
2008-08-20 10:20:41 -04:00
|
|
|
end
|
2008-05-28 22:09:30 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
def define_attribute(attribute)
|
|
|
|
name = attribute.name
|
|
|
|
# TODO: move these checks into Attribute
|
|
|
|
if attribute_defined?(name)
|
|
|
|
raise AttributeDefinitionError, "Attribute already defined: #{name}"
|
|
|
|
end
|
2010-10-02 00:00:58 -04:00
|
|
|
if attribute.respond_to?(:factory) && attribute.factory == self.name
|
|
|
|
raise AssociationDefinitionError, "Self-referencing association '#{name}' in factory '#{self.name}'"
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
|
|
|
@attributes << attribute
|
|
|
|
end
|
2011-08-12 10:35:41 -04:00
|
|
|
|
2011-08-09 20:29:02 -04:00
|
|
|
def define_attribute_group(group)
|
2011-08-10 14:11:53 -04:00
|
|
|
attribute_groups.add group
|
2011-08-09 20:29:02 -04:00
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
|
|
|
|
def add_callback(name, &block)
|
|
|
|
unless [:after_build, :after_create, :after_stub].include?(name.to_sym)
|
|
|
|
raise InvalidCallbackNameError, "#{name} is not a valid callback name. Valid callback names are :after_build, :after_create, and :after_stub"
|
2008-07-30 13:46:06 -04:00
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
@attributes << Attribute::Callback.new(name.to_sym, block)
|
2008-05-31 18:16:59 -04:00
|
|
|
end
|
|
|
|
|
2010-11-12 16:21:16 -05:00
|
|
|
def run(proxy_class, overrides) #:nodoc:
|
2010-06-24 09:45:57 -04:00
|
|
|
proxy = proxy_class.new(build_class)
|
|
|
|
overrides = symbolize_keys(overrides)
|
|
|
|
@attributes.each do |attribute|
|
2011-08-08 10:03:13 -04:00
|
|
|
factory_overrides = overrides.select { |attr, val| attribute.aliases_for?(attr) }
|
|
|
|
if factory_overrides.empty?
|
2010-06-24 09:45:57 -04:00
|
|
|
attribute.add_to(proxy)
|
2011-08-08 10:03:13 -04:00
|
|
|
else
|
|
|
|
factory_overrides.each { |attr, val| proxy.set(attr, val) }
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
|
|
|
end
|
2011-08-08 10:03:13 -04:00
|
|
|
overrides.each { |attr, val| proxy.set(attr, val) }
|
2010-11-12 16:21:16 -05:00
|
|
|
proxy.result(@to_create_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
|
|
|
|
names.map {|name| name.to_s.gsub('_', ' ') }
|
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-09 20:29:02 -04:00
|
|
|
def attribute_group_by_name(name)
|
2011-08-10 14:11:53 -04:00
|
|
|
return attribute_groups.find(name) if attribute_groups.registered?(name)
|
2011-08-12 10:35:41 -04:00
|
|
|
|
|
|
|
if @parent
|
2011-08-10 18:19:25 -04:00
|
|
|
FactoryGirl.factory_by_name(@parent).attribute_group_by_name(name)
|
2011-08-12 10:35:41 -04:00
|
|
|
else
|
|
|
|
FactoryGirl.attribute_group_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
|
|
|
|
[name] + (@options[: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
|
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
private
|
2009-09-15 15:47:47 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
def class_for (class_or_to_s)
|
|
|
|
if class_or_to_s.respond_to?(:to_sym)
|
|
|
|
class_name = variable_name_to_class_name(class_or_to_s)
|
|
|
|
class_name.split('::').inject(Object) do |object, string|
|
|
|
|
object.const_get(string)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
class_or_to_s
|
2010-03-17 18:49:35 -04:00
|
|
|
end
|
2008-07-29 16:20:44 -04:00
|
|
|
end
|
|
|
|
|
2010-10-02 00:00:58 -04:00
|
|
|
def factory_name_for(class_or_to_s)
|
2010-06-24 09:45:57 -04:00
|
|
|
if class_or_to_s.respond_to?(:to_sym)
|
|
|
|
class_or_to_s.to_sym
|
|
|
|
else
|
|
|
|
class_name_to_variable_name(class_or_to_s).to_sym
|
|
|
|
end
|
2008-07-29 16:20:44 -04:00
|
|
|
end
|
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
def attribute_defined? (name)
|
|
|
|
!@attributes.detect {|attr| attr.name == name && !attr.is_a?(Attribute::Callback) }.nil?
|
|
|
|
end
|
2008-07-30 14:04:43 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
def assert_valid_options(options)
|
2011-08-10 22:23:36 -04:00
|
|
|
invalid_keys = options.keys - [:class, :parent, :default_strategy, :aliases, :attribute_groups]
|
2010-06-24 09:45:57 -04:00
|
|
|
unless invalid_keys == []
|
|
|
|
raise ArgumentError, "Unknown arguments: #{invalid_keys.inspect}"
|
|
|
|
end
|
2011-01-19 19:26:27 -05:00
|
|
|
if options[:default_strategy]
|
|
|
|
assert_valid_strategy(options[:default_strategy])
|
|
|
|
puts "WARNING: default_strategy is deprecated."
|
|
|
|
puts "Override to_create if you need to prevent a call to #save!."
|
|
|
|
end
|
2008-12-11 15:54:33 -05:00
|
|
|
end
|
2010-06-07 15:51:18 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
def assert_valid_strategy(strategy)
|
|
|
|
unless Proxy.const_defined? variable_name_to_class_name(strategy)
|
|
|
|
raise ArgumentError, "Unknown strategy: #{strategy}"
|
|
|
|
end
|
2009-01-08 11:43:07 -05:00
|
|
|
end
|
2008-12-11 15:54:33 -05:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
# 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
|
2008-12-11 15:54:33 -05:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
# Based on ActiveSupport's camelize inflector
|
|
|
|
def variable_name_to_class_name(name)
|
|
|
|
name.to_s.
|
|
|
|
gsub(/\/(.?)/) { "::#{$1.upcase}" }.
|
|
|
|
gsub(/(?:^|_)(.)/) { $1.upcase }
|
|
|
|
end
|
2008-12-11 15:54:33 -05:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
# From ActiveSupport
|
|
|
|
def symbolize_keys(hash)
|
|
|
|
hash.inject({}) do |options, (key, value)|
|
|
|
|
options[(key.to_sym rescue key) || key] = value
|
|
|
|
options
|
|
|
|
end
|
2008-12-11 15:54:33 -05:00
|
|
|
end
|
2011-08-12 10:35:41 -04:00
|
|
|
|
|
|
|
def apply_attributes(attributes_to_apply)
|
|
|
|
new_attributes=[]
|
|
|
|
|
|
|
|
attributes_to_apply.each do |attribute|
|
|
|
|
if attribute_defined?(attribute.name)
|
|
|
|
@attributes.delete_if do |attrib|
|
|
|
|
new_attributes << attrib.clone if attrib.name == attribute.name
|
|
|
|
end
|
|
|
|
else
|
|
|
|
new_attributes << attribute.clone
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@attributes.unshift *new_attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort_attributes!
|
|
|
|
@attributes = @attributes.partition {|attr| attr.priority.zero? }.flatten
|
|
|
|
end
|
|
|
|
|
2011-08-10 14:11:53 -04:00
|
|
|
def attribute_groups
|
|
|
|
@attribute_groups ||= Registry.new
|
|
|
|
end
|
2010-06-24 09:45:57 -04:00
|
|
|
end
|
2008-05-28 18:20:25 -04:00
|
|
|
end
|