forked from thoughtbot/factory_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.rb
235 lines (205 loc) · 6.85 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
module FactoryGirl
# Raised when a factory is defined that attempts to instantiate itself.
class AssociationDefinitionError < RuntimeError
end
# Raised when a callback is defined that has an invalid name
class InvalidCallbackNameError < RuntimeError
end
# Raised when a factory is defined with the same name as a previously-defined factory.
class DuplicateDefinitionError < RuntimeError
end
class Factory
attr_reader :name #:nodoc:
attr_reader :attributes #:nodoc:
attr_reader :attribute_groups #:nodoc:
def factory_name
puts "WARNING: factory.factory_name is deprecated. Use factory.name instead."
name
end
def class_name #:nodoc:
@options[:class] || name
end
def build_class #:nodoc:
@build_class ||= class_for(class_name)
end
def default_strategy #:nodoc:
@options[:default_strategy] || :create
end
def initialize(name, options = {}) #:nodoc:
assert_valid_options(options)
@name = factory_name_for(name)
@options = options
@attributes = []
@attribute_groups = {}
end
def inherit_from(parent) #:nodoc:
@options[:class] ||= parent.class_name
@options[:default_strategy] ||= parent.default_strategy
new_attributes = []
parent.attributes.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
@attributes = @attributes.partition{|attr| attr.priority.zero? }.flatten
end
def apply_attribute_groups(groups)
groups.reverse.map{ |name| attribute_group_by_name(name) }.each do |group|
new_attributes=[]
group.attributes.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
@attributes = @attributes.partition{|attr| attr.priority.zero?}.flatten
end
end
def define_attribute(attribute)
name = attribute.name
# TODO: move these checks into Attribute
if attribute_defined?(name)
raise AttributeDefinitionError, "Attribute already defined: #{name}"
end
if attribute.respond_to?(:factory) && attribute.factory == self.name
raise AssociationDefinitionError, "Self-referencing association '#{name}' in factory '#{self.name}'"
end
@attributes << attribute
end
def define_attribute_group(group)
@attribute_groups[group.name.to_sym] = group
end
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"
end
@attributes << Attribute::Callback.new(name.to_sym, block)
end
def run(proxy_class, overrides) #:nodoc:
proxy = proxy_class.new(build_class)
overrides = symbolize_keys(overrides)
@attributes.each do |attribute|
factory_overrides = overrides.select { |attr, val| attribute.aliases_for?(attr) }
if factory_overrides.empty?
attribute.add_to(proxy)
else
factory_overrides.each { |attr, val| proxy.set(attr, val) }
end
end
overrides.each { |attr, val| proxy.set(attr, val) }
proxy.result(@to_create_block)
end
def human_names
names.map {|name| name.to_s.gsub('_', ' ') }
end
def associations
attributes.select {|attribute| attribute.association? }
end
def attribute_group_by_name(name)
name=name.to_sym
group = @attribute_groups[name]
unless @options[:parent].nil?
group ||= FactoryGirl.factory_by_name(@options[:parent]).attribute_group_by_name(name)
end
group
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] + (@options[:aliases] || [])
end
def to_create(&block)
@to_create_block = block
end
private
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
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
class_name_to_variable_name(class_or_to_s).to_sym
end
end
def attribute_defined? (name)
!@attributes.detect {|attr| attr.name == name && !attr.is_a?(Attribute::Callback) }.nil?
end
def assert_valid_options(options)
invalid_keys = options.keys - [:class, :parent, :default_strategy, :aliases, :attr_groups]
unless invalid_keys == []
raise ArgumentError, "Unknown arguments: #{invalid_keys.inspect}"
end
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
end
def assert_valid_strategy(strategy)
unless Proxy.const_defined? variable_name_to_class_name(strategy)
raise ArgumentError, "Unknown strategy: #{strategy}"
end
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
end
end