1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
require "active_support/core_ext/hash/keys"
require "active_support/inflector"
module FactoryBot
# @api private
class Factory
attr_reader :name, :definition
def initialize(name, options = {})
assert_valid_options(options)
@name = name.respond_to?(:to_sym) ? name.to_sym : name.to_s.underscore.to_sym
@parent = options[:parent]
@aliases = options[:aliases] || []
@class_name = options[:class]
@definition = Definition.new(@name, options[:traits] || [])
@compiled = false
end
delegate :add_callback, :declare_attribute, :to_create, :define_trait, :constructor,
:defined_traits, :defined_traits_names, :inherit_traits, :append_traits,
to: :@definition
def build_class
@build_class ||= if class_name.is_a? Class
class_name
elsif class_name.to_s.safe_constantize
class_name.to_s.safe_constantize
else
class_name.to_s.camelize.constantize
end
end
def run(build_strategy, overrides, &block)
block ||= ->(result) { result }
compile
strategy = StrategyCalculator.new(build_strategy).strategy.new
evaluator = evaluator_class.new(strategy, overrides.symbolize_keys)
attribute_assigner = AttributeAssigner.new(evaluator, build_class, &compiled_constructor)
observer = CallbacksObserver.new(callbacks, evaluator)
evaluation =
Evaluation.new(evaluator, attribute_assigner, compiled_to_create, observer)
strategy.result(evaluation).tap(&block)
end
def human_names
names.map { |name| name.to_s.humanize.downcase }
end
def associations
evaluator_class.attribute_list.associations
end
# Names for this factory, including aliases.
#
# Example:
#
# factory :user, aliases: [:author] do
# # ...
# end
#
# FactoryBot.create(:author).class
# # => 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
#
# FactoryBot.create(:post).author.class
# # => User
def names
[name] + @aliases
end
def compile
unless @compiled
parent.compile
inherit_parent_traits
@definition.compile(build_class)
build_hierarchy
@compiled = true
end
end
def with_traits(traits)
clone.tap do |factory_with_traits|
factory_with_traits.append_traits traits
end
end
protected
def class_name
@class_name || parent.class_name || name
end
def evaluator_class
@evaluator_class ||= EvaluatorClassDefiner.new(attributes, parent.evaluator_class).evaluator_class
end
def attributes
compile
AttributeList.new(@name).tap do |list|
list.apply_attributes definition.attributes
end
end
def hierarchy_class
@hierarchy_class ||= Class.new(parent.hierarchy_class)
end
def hierarchy_instance
@hierarchy_instance ||= hierarchy_class.new
end
def build_hierarchy
hierarchy_class.build_from_definition definition
end
def callbacks
hierarchy_instance.callbacks
end
def compiled_to_create
hierarchy_instance.to_create
end
def compiled_constructor
hierarchy_instance.constructor
end
private
def assert_valid_options(options)
options.assert_valid_keys(:class, :parent, :aliases, :traits)
end
def parent
if @parent
FactoryBot::Internal.factory_by_name(@parent)
else
NullFactory.new
end
end
def inherit_parent_traits
parent.defined_traits.each do |trait|
next if defined_traits_names.include?(trait.name)
define_trait(trait.clone)
end
end
def initialize_copy(source)
super
@definition = @definition.clone
@evaluator_class = nil
@hierarchy_class = nil
@hierarchy_instance = nil
@compiled = false
end
end
end
|