factory_bot 6.6.0 → 6.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/GETTING_STARTED.md +1 -1
- data/NAME.md +2 -0
- data/NEWS.md +23 -0
- data/README.md +0 -2
- data/lib/factory_bot/attribute/dynamic.rb +2 -2
- data/lib/factory_bot/attribute/sequence.rb +2 -2
- data/lib/factory_bot/attribute.rb +3 -3
- data/lib/factory_bot/attribute_assigner.rb +10 -10
- data/lib/factory_bot/attribute_list.rb +4 -4
- data/lib/factory_bot/declaration/dynamic.rb +4 -4
- data/lib/factory_bot/declaration/implicit.rb +4 -4
- data/lib/factory_bot/declaration.rb +3 -3
- data/lib/factory_bot/decorator/invocation_tracker.rb +1 -2
- data/lib/factory_bot/definition.rb +86 -40
- data/lib/factory_bot/definition_proxy.rb +4 -4
- data/lib/factory_bot/factory.rb +12 -2
- data/lib/factory_bot/registry.rb +1 -13
- data/lib/factory_bot/version.rb +1 -1
- metadata +19 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ca1ce72134e514e89291d6dba94f8528bbf577158388be6c41d8520486fe9664
|
|
4
|
+
data.tar.gz: b8ca00a35aae7090ef7815069298e1944ed232c65601452bd32384cd66c5da12
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5ef1f74f2f1a736423a821ebc224de8ac1e74fa36463686dd3b685dda589826c016a2037f666e7d4bf447318b8546209b9d2cbe72d2b35966e29f3ae8d99e0af
|
|
7
|
+
data.tar.gz: 3794deb1612fef498f8755c786723fe676fb66ca4d3967f0433c749044cef9d7efb486d60eeed359bb44cca05c9499a3d2b849836921eddab1e85a89a1f84a92
|
data/GETTING_STARTED.md
CHANGED
|
@@ -364,7 +364,7 @@ Aliases
|
|
|
364
364
|
-------
|
|
365
365
|
|
|
366
366
|
factory\_bot allows you to define aliases to existing factories to make them
|
|
367
|
-
easier to
|
|
367
|
+
easier to reuse. This could come in handy when, for example, your Post object
|
|
368
368
|
has an author attribute that actually refers to an instance of a User class.
|
|
369
369
|
While normally factory\_bot can infer the factory name from the association name,
|
|
370
370
|
in this case it will look for an author factory in vain. So, alias your user
|
data/NAME.md
CHANGED
|
@@ -16,3 +16,5 @@ name.
|
|
|
16
16
|
The name "Factory Girl" was confusing to some developers who encountered this
|
|
17
17
|
library, and offensive or problematic to others. In October 2017 we [renamed the library](https://robots.thoughtbot.com/factory_bot)
|
|
18
18
|
to "Factory Bot".
|
|
19
|
+
**If you are still upgrading from factory_girl, see the
|
|
20
|
+
[upgrade guide](https://github.com/thoughtbot/factory_bot/blob/v4.9.0/UPGRADE_FROM_FACTORY_GIRL.md).**
|
data/NEWS.md
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
# News
|
|
2
2
|
|
|
3
|
+
## 6.7.0 (November 26, 2026)
|
|
4
|
+
|
|
5
|
+
* Feat: Added the `automatically_define_enum_traits` factory option to override the global setting for a factory and its children
|
|
6
|
+
* Change: Drop support for Ruby 3.0, which reached end of life in April 2024. Ruby 3.1 is now the minimum required version
|
|
7
|
+
* Change: Drop support for ActiveSupport 6.1, which is no longer tested in CI. ActiveSupport 7.0 is now the minimum required version
|
|
8
|
+
* Bump Ruby to 4.0.10 and track latest 4.0 in workflows by @neilvcarvalho in [#1858](https://github.com/thoughtbot/factory_bot/pull/1858)
|
|
9
|
+
* Declare cucumber as a development dependency by @neilvcarvalho in [#1856](https://github.com/thoughtbot/factory_bot/pull/1856)
|
|
10
|
+
* Updated Readme by @SergiuLupaiescu in [#1854](https://github.com/thoughtbot/factory_bot/pull/1854)
|
|
11
|
+
* Measure factory compilation in compile_factory span by @neilvcarvalho in [#1850](https://github.com/thoughtbot/factory_bot/pull/1850)
|
|
12
|
+
* Run truffleruby against Rails main in CI by @neilvcarvalho in [#1846](https://github.com/thoughtbot/factory_bot/pull/1846)
|
|
13
|
+
* Require ActiveSupport 7.0 or newer by @neilvcarvalho in [#1845](https://github.com/thoughtbot/factory_bot/pull/1845)
|
|
14
|
+
* Require Ruby 3.1 or newer by @neilvcarvalho in [#1843](https://github.com/thoughtbot/factory_bot/pull/1843)
|
|
15
|
+
* Update CI matrix for Rails 8.1 and JRuby 10 by @neilvcarvalho in [#1842](https://github.com/thoughtbot/factory_bot/pull/1842)
|
|
16
|
+
* Fix CI failures on main by @neilvcarvalho in [#1841](https://github.com/thoughtbot/factory_bot/pull/1841)
|
|
17
|
+
* Pin transient attribute redefinition behavior by @neilvcarvalho in [#1840](https://github.com/thoughtbot/factory_bot/pull/1840)
|
|
18
|
+
* Refactor remaining internal ignored state to transient terminology by @taljeon in [#1834](https://github.com/thoughtbot/factory_bot/pull/1834)
|
|
19
|
+
* Avoid `ruby2_keywords` usage by @Earlopain in [#1830](https://github.com/thoughtbot/factory_bot/pull/1830)
|
|
20
|
+
* Issue 1825 - Refactor internal references to ignored (attributes) to transient attributes by @cath-porter in [#1828](https://github.com/thoughtbot/factory_bot/pull/1828)
|
|
21
|
+
* Expanded trait error message to include list of defined traits. by @iannorriswork in [#1824](https://github.com/thoughtbot/factory_bot/pull/1824)
|
|
22
|
+
* Fix typos in comments and documentation by @maxtaran2010 in [#1823](https://github.com/thoughtbot/factory_bot/pull/1823)
|
|
23
|
+
* Bump actions/checkout from 6 to 7 by @app/dependabot in [#1822](https://github.com/thoughtbot/factory_bot/pull/1822)
|
|
24
|
+
* Add per model automatically_define_enum_traits option by @mikespokefire in [#1598](https://github.com/thoughtbot/factory_bot/pull/1598)
|
|
25
|
+
|
|
3
26
|
## 6.6.0 (May 4, 2026)
|
|
4
27
|
|
|
5
28
|
* Feat: Added `factory_bot.before_run_factory` instrumentation event
|
data/README.md
CHANGED
|
@@ -9,8 +9,6 @@ If you want to use factory_bot with Rails, see
|
|
|
9
9
|
|
|
10
10
|
Interested in the history of the project name? You can find the history [here](https://github.com/thoughtbot/factory_bot/blob/main/NAME.md)
|
|
11
11
|
|
|
12
|
-
Transitioning from factory\_girl? Check out the [upgrade guide](https://github.com/thoughtbot/factory_bot/blob/v4.9.0/UPGRADE_FROM_FACTORY_GIRL.md).
|
|
13
|
-
|
|
14
12
|
## Documentation
|
|
15
13
|
|
|
16
14
|
See our extensive reference, guides, and cookbook in [the factory_bot book][].
|
|
@@ -5,11 +5,11 @@ require "factory_bot/attribute/sequence"
|
|
|
5
5
|
module FactoryBot
|
|
6
6
|
# @api private
|
|
7
7
|
class Attribute
|
|
8
|
-
attr_reader :name, :
|
|
8
|
+
attr_reader :name, :transient
|
|
9
9
|
|
|
10
|
-
def initialize(name,
|
|
10
|
+
def initialize(name, transient)
|
|
11
11
|
@name = name.to_sym
|
|
12
|
-
@
|
|
12
|
+
@transient = transient
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def to_proc
|
|
@@ -75,19 +75,19 @@ module FactoryBot
|
|
|
75
75
|
def attribute_names_to_assign
|
|
76
76
|
@attribute_names_to_assign ||= begin
|
|
77
77
|
# start a list of candidates containing non-transient attributes and overrides
|
|
78
|
-
assignment_candidates =
|
|
78
|
+
assignment_candidates = non_transient_attribute_names + override_names
|
|
79
79
|
# then remove any transient attributes (potentially reintroduced by the overrides),
|
|
80
80
|
# and remove ignorable aliased attributes from the candidate list
|
|
81
|
-
assignment_candidates -
|
|
81
|
+
assignment_candidates - transient_attribute_names - attribute_names_overriden_by_alias
|
|
82
82
|
end
|
|
83
83
|
end
|
|
84
84
|
|
|
85
|
-
def
|
|
86
|
-
@attribute_list.
|
|
85
|
+
def non_transient_attribute_names
|
|
86
|
+
@attribute_list.non_transient.names
|
|
87
87
|
end
|
|
88
88
|
|
|
89
|
-
def
|
|
90
|
-
@attribute_list.
|
|
89
|
+
def transient_attribute_names
|
|
90
|
+
@attribute_list.transient.names
|
|
91
91
|
end
|
|
92
92
|
|
|
93
93
|
def association_names
|
|
@@ -109,7 +109,7 @@ module FactoryBot
|
|
|
109
109
|
# Builds a list of attribute names which are slated to be interrupted by an override.
|
|
110
110
|
def attribute_names_overriden_by_alias
|
|
111
111
|
@attribute_list
|
|
112
|
-
.
|
|
112
|
+
.non_transient
|
|
113
113
|
.flat_map { |attribute|
|
|
114
114
|
override_names.map do |override|
|
|
115
115
|
attribute.name if ignorable_alias?(attribute, override)
|
|
@@ -120,7 +120,7 @@ module FactoryBot
|
|
|
120
120
|
|
|
121
121
|
# Is the attribute an ignorable alias of the override?
|
|
122
122
|
# An attribute is ignorable when it is an alias of the override AND it is
|
|
123
|
-
# either interrupting an
|
|
123
|
+
# either interrupting an association OR is not the name of another attribute
|
|
124
124
|
#
|
|
125
125
|
# @note An "alias" is currently an overloaded term for two distinct cases:
|
|
126
126
|
# (1) attributes which are aliases and reference the same value
|
|
@@ -157,8 +157,8 @@ module FactoryBot
|
|
|
157
157
|
# Does this override match the name of any declared attribute?
|
|
158
158
|
#
|
|
159
159
|
# @note Checking against the names of all attributes, resolves any issues with having both
|
|
160
|
-
# <attribute> and <attribute>_id in the same factory. This also takes into account
|
|
161
|
-
# attributes that should not be assigned
|
|
160
|
+
# <attribute> and <attribute>_id in the same factory. This also takes into account transient
|
|
161
|
+
# attributes that should not be assigned.
|
|
162
162
|
#
|
|
163
163
|
# @param [Symbol] override the name of an override
|
|
164
164
|
def override_matches_declared_attribute?(override)
|
|
@@ -27,12 +27,12 @@ module FactoryBot
|
|
|
27
27
|
AttributeList.new(@name, select(&:association?))
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
-
def
|
|
31
|
-
AttributeList.new(@name, select(&:
|
|
30
|
+
def transient
|
|
31
|
+
AttributeList.new(@name, select(&:transient))
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
-
def
|
|
35
|
-
AttributeList.new(@name, reject(&:
|
|
34
|
+
def non_transient
|
|
35
|
+
AttributeList.new(@name, reject(&:transient))
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
def apply_attributes(attributes_to_apply)
|
|
@@ -2,15 +2,15 @@ module FactoryBot
|
|
|
2
2
|
class Declaration
|
|
3
3
|
# @api private
|
|
4
4
|
class Dynamic < Declaration
|
|
5
|
-
def initialize(name,
|
|
6
|
-
super(name,
|
|
5
|
+
def initialize(name, transient = false, block = nil)
|
|
6
|
+
super(name, transient)
|
|
7
7
|
@block = block
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
def ==(other)
|
|
11
11
|
self.class == other.class &&
|
|
12
12
|
name == other.name &&
|
|
13
|
-
|
|
13
|
+
transient == other.transient &&
|
|
14
14
|
block == other.block
|
|
15
15
|
end
|
|
16
16
|
|
|
@@ -21,7 +21,7 @@ module FactoryBot
|
|
|
21
21
|
private
|
|
22
22
|
|
|
23
23
|
def build
|
|
24
|
-
[Attribute::Dynamic.new(name, @
|
|
24
|
+
[Attribute::Dynamic.new(name, @transient, @block)]
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
end
|
|
@@ -2,8 +2,8 @@ module FactoryBot
|
|
|
2
2
|
class Declaration
|
|
3
3
|
# @api private
|
|
4
4
|
class Implicit < Declaration
|
|
5
|
-
def initialize(name, factory = nil,
|
|
6
|
-
super(name,
|
|
5
|
+
def initialize(name, factory = nil, transient = false)
|
|
6
|
+
super(name, transient)
|
|
7
7
|
@factory = factory
|
|
8
8
|
end
|
|
9
9
|
|
|
@@ -11,7 +11,7 @@ module FactoryBot
|
|
|
11
11
|
self.class == other.class &&
|
|
12
12
|
name == other.name &&
|
|
13
13
|
factory == other.factory &&
|
|
14
|
-
|
|
14
|
+
transient == other.transient
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
protected
|
|
@@ -24,7 +24,7 @@ module FactoryBot
|
|
|
24
24
|
if FactoryBot.factories.registered?(name)
|
|
25
25
|
[Attribute::Association.new(name, name, {})]
|
|
26
26
|
elsif FactoryBot::Internal.sequences.registered?(name)
|
|
27
|
-
[Attribute::Sequence.new(name, name, @
|
|
27
|
+
[Attribute::Sequence.new(name, name, @transient)]
|
|
28
28
|
elsif @factory.name.to_s == name.to_s
|
|
29
29
|
message = "Self-referencing trait '#{@name}'"
|
|
30
30
|
raise TraitDefinitionError, message
|
|
@@ -7,9 +7,9 @@ module FactoryBot
|
|
|
7
7
|
class Declaration
|
|
8
8
|
attr_reader :name
|
|
9
9
|
|
|
10
|
-
def initialize(name,
|
|
10
|
+
def initialize(name, transient = false)
|
|
11
11
|
@name = name
|
|
12
|
-
@
|
|
12
|
+
@transient = transient
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def to_attributes
|
|
@@ -18,6 +18,6 @@ module FactoryBot
|
|
|
18
18
|
|
|
19
19
|
protected
|
|
20
20
|
|
|
21
|
-
attr_reader :
|
|
21
|
+
attr_reader :transient
|
|
22
22
|
end
|
|
23
23
|
end
|
|
@@ -6,11 +6,10 @@ module FactoryBot
|
|
|
6
6
|
@invoked_methods = []
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
def method_missing(name,
|
|
9
|
+
def method_missing(name, ...) # rubocop:disable Style/MissingRespondToMissing
|
|
10
10
|
@invoked_methods << name
|
|
11
11
|
super
|
|
12
12
|
end
|
|
13
|
-
ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
|
|
14
13
|
|
|
15
14
|
def __invoked_methods__
|
|
16
15
|
@invoked_methods.uniq
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
module FactoryBot
|
|
2
2
|
# @api private
|
|
3
3
|
class Definition
|
|
4
|
-
attr_reader :defined_traits, :declarations, :name, :registered_enums, :uri_manager
|
|
4
|
+
attr_reader :defined_traits, :declarations, :name, :registered_enums, :uri_manager,
|
|
5
|
+
:automatically_define_enum_traits
|
|
5
6
|
attr_accessor :klass
|
|
6
7
|
|
|
7
8
|
def initialize(name, base_traits = [], **opts)
|
|
@@ -17,6 +18,7 @@ module FactoryBot
|
|
|
17
18
|
@constructor = nil
|
|
18
19
|
@attributes = nil
|
|
19
20
|
@compiled = false
|
|
21
|
+
@automatically_define_enum_traits = opts[:automatically_define_enum_traits]
|
|
20
22
|
@expanded_enum_traits = false
|
|
21
23
|
end
|
|
22
24
|
|
|
@@ -49,25 +51,24 @@ module FactoryBot
|
|
|
49
51
|
|
|
50
52
|
def compile(klass = nil)
|
|
51
53
|
unless @compiled
|
|
52
|
-
|
|
54
|
+
ActiveSupport::Notifications.instrument "factory_bot.compile_factory", {name: name} do |payload|
|
|
55
|
+
expand_enum_traits(klass) unless klass.nil?
|
|
53
56
|
|
|
54
|
-
|
|
57
|
+
declarations.attributes
|
|
55
58
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
self.klass ||= klass
|
|
60
|
+
defined_traits.each do |defined_trait|
|
|
61
|
+
defined_trait.klass ||= klass
|
|
62
|
+
base_trait_names.each { |bt| bt.define_trait defined_trait }
|
|
63
|
+
additional_trait_names.each { |at| at.define_trait defined_trait }
|
|
64
|
+
end
|
|
62
65
|
|
|
63
|
-
|
|
66
|
+
@compiled = true
|
|
64
67
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
class: klass || self.klass
|
|
70
|
-
}
|
|
68
|
+
payload[:attributes] = declarations.attributes
|
|
69
|
+
payload[:traits] = defined_traits
|
|
70
|
+
payload[:class] = klass || self.klass
|
|
71
|
+
end
|
|
71
72
|
end
|
|
72
73
|
end
|
|
73
74
|
|
|
@@ -80,6 +81,12 @@ module FactoryBot
|
|
|
80
81
|
@base_traits += new_traits
|
|
81
82
|
end
|
|
82
83
|
|
|
84
|
+
def inherit_automatically_define_enum_traits(parent_definition)
|
|
85
|
+
if @automatically_define_enum_traits.nil?
|
|
86
|
+
@automatically_define_enum_traits = parent_definition.automatically_define_enum_traits
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
83
90
|
def append_traits(new_traits)
|
|
84
91
|
@additional_traits += new_traits
|
|
85
92
|
end
|
|
@@ -124,39 +131,73 @@ module FactoryBot
|
|
|
124
131
|
|
|
125
132
|
private
|
|
126
133
|
|
|
127
|
-
def
|
|
134
|
+
def base_trait_names
|
|
128
135
|
@base_traits.map { |name| trait_by_name(name) }
|
|
129
136
|
rescue KeyError => error
|
|
130
137
|
raise error_with_definition_name(error)
|
|
131
138
|
end
|
|
132
139
|
|
|
140
|
+
def additional_trait_names
|
|
141
|
+
@additional_traits.map { |name| trait_by_name(name) }
|
|
142
|
+
rescue KeyError => error
|
|
143
|
+
raise error_with_definition_name(error)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def all_registered_trait_names
|
|
147
|
+
(defined_traits_names + Internal.traits.map(&:name)).uniq
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def error_options(error)
|
|
151
|
+
if error.respond_to?(:key) && error.respond_to?(:receiver)
|
|
152
|
+
receiver = error.receiver
|
|
153
|
+
if receiver.is_a?(Hash) || receiver.is_a?(ActiveSupport::HashWithIndifferentAccess)
|
|
154
|
+
receiver = receiver.dup
|
|
155
|
+
defined_traits_names.each do |trait_name|
|
|
156
|
+
receiver[trait_name] = nil unless receiver.key?(trait_name)
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
{key: error.key, receiver: receiver}
|
|
160
|
+
else
|
|
161
|
+
{}
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Before Ruby 3.2, did_you_mean embeds its suggestions in the message itself.
|
|
166
|
+
def original_message(error)
|
|
167
|
+
error.message.partition("\nDid you mean?").first
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def registered_trait_message(all_registered_traits)
|
|
171
|
+
if all_registered_traits.empty?
|
|
172
|
+
"No registered traits"
|
|
173
|
+
else
|
|
174
|
+
"Registered traits: #{all_registered_traits.map(&:to_sym).sort.inspect}"
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def error_with_definition_name(error)
|
|
179
|
+
message = original_message(error).rstrip
|
|
180
|
+
message += "." unless message.end_with?(".")
|
|
181
|
+
message += " #{registered_trait_message(all_registered_trait_names)}."
|
|
182
|
+
message += " Referenced within \"#{name}\" definition"
|
|
183
|
+
|
|
184
|
+
new_error(error, message).tap { |new_error| new_error.set_backtrace(error.backtrace) }
|
|
185
|
+
end
|
|
186
|
+
|
|
133
187
|
# detailed_message introduced in Ruby 3.2 for cleaner integration with
|
|
134
188
|
# did_you_mean. See https://bugs.ruby-lang.org/issues/18564
|
|
135
189
|
if KeyError.method_defined?(:detailed_message)
|
|
136
|
-
def
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
error.class.new(message, key: error.key, receiver: error.receiver)
|
|
140
|
-
.tap { |new_error| new_error.set_backtrace(error.backtrace) }
|
|
190
|
+
def new_error(error, message)
|
|
191
|
+
error.class.new(message, **error_options(error))
|
|
141
192
|
end
|
|
142
193
|
else
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
" referenced within \"#{name}\" definition"
|
|
148
|
-
)
|
|
149
|
-
|
|
150
|
-
error.class.new(message).tap do |new_error|
|
|
151
|
-
new_error.set_backtrace(error.backtrace)
|
|
152
|
-
end
|
|
194
|
+
# Embed the suggestions so Exception#original_message includes them too.
|
|
195
|
+
def new_error(error, message)
|
|
196
|
+
options = error_options(error)
|
|
197
|
+
error.class.new(error.class.new(message, **options).message, **options)
|
|
153
198
|
end
|
|
154
199
|
end
|
|
155
200
|
|
|
156
|
-
def additional_traits
|
|
157
|
-
@additional_traits.map { |name| trait_by_name(name) }
|
|
158
|
-
end
|
|
159
|
-
|
|
160
201
|
def trait_by_name(name)
|
|
161
202
|
trait_for(name) || Internal.trait_by_name(name, klass)
|
|
162
203
|
end
|
|
@@ -177,9 +218,9 @@ module FactoryBot
|
|
|
177
218
|
compile
|
|
178
219
|
|
|
179
220
|
[
|
|
180
|
-
|
|
221
|
+
base_trait_names.map(&method_name),
|
|
181
222
|
instance_exec(&block),
|
|
182
|
-
|
|
223
|
+
additional_trait_names.map(&method_name)
|
|
183
224
|
].flatten.compact
|
|
184
225
|
end
|
|
185
226
|
|
|
@@ -203,8 +244,13 @@ module FactoryBot
|
|
|
203
244
|
end
|
|
204
245
|
|
|
205
246
|
def automatically_register_defined_enums?(klass)
|
|
206
|
-
|
|
207
|
-
|
|
247
|
+
automatically_define_enum_traits? && klass.respond_to?(:defined_enums)
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def automatically_define_enum_traits?
|
|
251
|
+
return FactoryBot.automatically_define_enum_traits if @automatically_define_enum_traits.nil?
|
|
252
|
+
|
|
253
|
+
@automatically_define_enum_traits
|
|
208
254
|
end
|
|
209
255
|
end
|
|
210
256
|
end
|
|
@@ -23,9 +23,9 @@ module FactoryBot
|
|
|
23
23
|
|
|
24
24
|
attr_reader :child_factories
|
|
25
25
|
|
|
26
|
-
def initialize(definition,
|
|
26
|
+
def initialize(definition, transient = false)
|
|
27
27
|
@definition = definition
|
|
28
|
-
@
|
|
28
|
+
@transient = transient
|
|
29
29
|
@child_factories = []
|
|
30
30
|
end
|
|
31
31
|
|
|
@@ -45,7 +45,7 @@ module FactoryBot
|
|
|
45
45
|
# The name of this attribute. This will be assigned using "name=" for
|
|
46
46
|
# generated instances.
|
|
47
47
|
def add_attribute(name, &block)
|
|
48
|
-
declaration = Declaration::Dynamic.new(name, @
|
|
48
|
+
declaration = Declaration::Dynamic.new(name, @transient, block)
|
|
49
49
|
@definition.declare_attribute(declaration)
|
|
50
50
|
end
|
|
51
51
|
|
|
@@ -246,7 +246,7 @@ module FactoryBot
|
|
|
246
246
|
|
|
247
247
|
def __declare_attribute__(name, block)
|
|
248
248
|
if block.nil?
|
|
249
|
-
declaration = Declaration::Implicit.new(name, @definition, @
|
|
249
|
+
declaration = Declaration::Implicit.new(name, @definition, @transient)
|
|
250
250
|
@definition.declare_attribute(declaration)
|
|
251
251
|
else
|
|
252
252
|
add_attribute(name, &block)
|
data/lib/factory_bot/factory.rb
CHANGED
|
@@ -13,7 +13,12 @@ module FactoryBot
|
|
|
13
13
|
@aliases = options[:aliases] || []
|
|
14
14
|
@class_name = options[:class]
|
|
15
15
|
@uri_manager = FactoryBot::UriManager.new(names)
|
|
16
|
-
@definition = Definition.new(
|
|
16
|
+
@definition = Definition.new(
|
|
17
|
+
@name,
|
|
18
|
+
options[:traits] || [],
|
|
19
|
+
automatically_define_enum_traits: options[:automatically_define_enum_traits],
|
|
20
|
+
uri_manager: @uri_manager
|
|
21
|
+
)
|
|
17
22
|
@compiled = false
|
|
18
23
|
end
|
|
19
24
|
|
|
@@ -92,6 +97,7 @@ module FactoryBot
|
|
|
92
97
|
unless @compiled
|
|
93
98
|
parent.compile
|
|
94
99
|
inherit_parent_traits
|
|
100
|
+
@definition.inherit_automatically_define_enum_traits(parent.definition)
|
|
95
101
|
@definition.compile(build_class)
|
|
96
102
|
build_hierarchy
|
|
97
103
|
@compiled = true
|
|
@@ -148,7 +154,11 @@ module FactoryBot
|
|
|
148
154
|
private
|
|
149
155
|
|
|
150
156
|
def assert_valid_options(options)
|
|
151
|
-
options.assert_valid_keys(:class, :parent, :aliases, :traits)
|
|
157
|
+
options.assert_valid_keys(:class, :parent, :aliases, :traits, :automatically_define_enum_traits)
|
|
158
|
+
|
|
159
|
+
unless [nil, true, false].include?(options[:automatically_define_enum_traits])
|
|
160
|
+
raise ArgumentError, "automatically_define_enum_traits must be true or false"
|
|
161
|
+
end
|
|
152
162
|
end
|
|
153
163
|
|
|
154
164
|
def parent
|
data/lib/factory_bot/registry.rb
CHANGED
|
@@ -39,21 +39,9 @@ module FactoryBot
|
|
|
39
39
|
|
|
40
40
|
def key_error_with_custom_message(key_error)
|
|
41
41
|
message = key_error.message.sub("key not found", "#{@name} not registered")
|
|
42
|
-
|
|
42
|
+
KeyError.new(message, key: key_error.key, receiver: key_error.receiver).tap do |error|
|
|
43
43
|
error.set_backtrace(key_error.backtrace)
|
|
44
44
|
end
|
|
45
45
|
end
|
|
46
|
-
|
|
47
|
-
# detailed_message introduced in Ruby 3.2 for cleaner integration with
|
|
48
|
-
# did_you_mean. See https://bugs.ruby-lang.org/issues/18564
|
|
49
|
-
if KeyError.method_defined?(:detailed_message)
|
|
50
|
-
def new_key_error(message, key_error)
|
|
51
|
-
KeyError.new(message, key: key_error.key, receiver: key_error.receiver)
|
|
52
|
-
end
|
|
53
|
-
else
|
|
54
|
-
def new_key_error(message, _)
|
|
55
|
-
KeyError.new(message)
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
46
|
end
|
|
59
47
|
end
|
data/lib/factory_bot/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: factory_bot
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 6.
|
|
4
|
+
version: 6.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Josh Clayton
|
|
@@ -16,14 +16,14 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version:
|
|
19
|
+
version: 7.0.0
|
|
20
20
|
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
24
|
- - ">="
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version:
|
|
26
|
+
version: 7.0.0
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: activerecord
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -66,6 +66,20 @@ dependencies:
|
|
|
66
66
|
- - ">="
|
|
67
67
|
- !ruby/object:Gem::Version
|
|
68
68
|
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: cucumber
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '10'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '10'
|
|
69
83
|
- !ruby/object:Gem::Dependency
|
|
70
84
|
name: mutex_m
|
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -260,14 +274,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
260
274
|
requirements:
|
|
261
275
|
- - ">="
|
|
262
276
|
- !ruby/object:Gem::Version
|
|
263
|
-
version: 3.
|
|
277
|
+
version: 3.1.0
|
|
264
278
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
265
279
|
requirements:
|
|
266
280
|
- - ">="
|
|
267
281
|
- !ruby/object:Gem::Version
|
|
268
282
|
version: '0'
|
|
269
283
|
requirements: []
|
|
270
|
-
rubygems_version: 4.0.
|
|
284
|
+
rubygems_version: 4.0.20
|
|
271
285
|
specification_version: 4
|
|
272
286
|
summary: factory_bot provides a framework and DSL for defining and using model instance
|
|
273
287
|
factories.
|