mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
5ccc564923
If a new trait is defined with attributes, check if attributes matches the name of the trait. Raise an error if so, to avoid a recursive call to the trait. Co-authored-by: Daniel Colsen <daniel.j.colson@thoughtbot.com> Co-authored-by: Paras Sanghavi <sanghaviparas@gmail.com>
38 lines
947 B
Ruby
38 lines
947 B
Ruby
module FactoryBot
|
|
class Declaration
|
|
# @api private
|
|
class Implicit < Declaration
|
|
def initialize(name, factory = nil, ignored = false)
|
|
super(name, ignored)
|
|
@factory = factory
|
|
end
|
|
|
|
def ==(other)
|
|
self.class == other.class &&
|
|
name == other.name &&
|
|
factory == other.factory &&
|
|
ignored == other.ignored
|
|
end
|
|
|
|
protected
|
|
|
|
attr_reader :factory
|
|
|
|
private
|
|
|
|
def build
|
|
if FactoryBot.factories.registered?(name)
|
|
[Attribute::Association.new(name, name, {})]
|
|
elsif FactoryBot::Internal.sequences.registered?(name)
|
|
[Attribute::Sequence.new(name, name, @ignored)]
|
|
elsif @factory.name.to_s == name.to_s
|
|
message = "Self-referencing trait '#{@name}'"
|
|
raise TraitDefinitionError, message
|
|
else
|
|
@factory.inherit_traits([name])
|
|
[]
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|