/
factory.rb
149 lines (125 loc) · 3.63 KB
/
factory.rb
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
require 'active_support/core_ext/hash/keys'
require 'active_support/inflector'
module FactoryGirl
# @api private
class Factory
attr_reader :name, :definition
def initialize(name, options = {})
assert_valid_options(options)
@name = name.is_a?(Symbol) ? name : 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,
:defined_traits, :inherit_traits, :processing_order, to: :@definition
def build_class
@build_class ||= if class_name.is_a? Class
class_name
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(build_class, strategy, overrides.symbolize_keys)
attribute_assigner = AttributeAssigner.new(evaluator, build_class, &constructor)
evaluation = Evaluation.new(attribute_assigner, to_create)
evaluation.add_observer(CallbacksObserver.new(callbacks, evaluator))
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
#
# FactoryGirl.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
#
# FactoryGirl.create(:post).author.class
# # => User
def names
[name] + @aliases
end
def compile
unless @compiled
parent.compile
parent.defined_traits.each {|trait| define_trait(trait) }
@definition.compile
@compiled = true
end
end
def with_traits(traits)
self.clone.tap do |factory_with_traits|
factory_with_traits.inherit_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|
processing_order.each do |factory|
list.apply_attributes factory.attributes
end
end
end
def callbacks
parent.callbacks + processing_order.map {|factory| factory.callbacks }.flatten
end
def constructor
@constructor ||=
if @definition.custom_constructor?
@definition.constructor
else
parent.constructor
end
end
private
def assert_valid_options(options)
options.assert_valid_keys(:class, :parent, :aliases, :traits)
end
def parent
if @parent
FactoryGirl.factory_by_name(@parent)
else
NullFactory.new
end
end
def initialize_copy(source)
super
@definition = @definition.clone
@evaluator_class = nil
end
end
end