mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Name first arg of DefinitionProxy#method_missing
This code was a bit confusing before this change. The only time we care about `args.first` is if we are creating an association, so this PR gives `args.first` the name `association_options` to reflect that. The PR also pulls out a predicate method to give a name to the step of validating that `args.first` are in indeed association options.
This commit is contained in:
parent
f898371f8f
commit
8e7e339323
1 changed files with 10 additions and 4 deletions
|
@ -89,14 +89,16 @@ module FactoryBot
|
|||
#
|
||||
# are equivalent.
|
||||
def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing
|
||||
if args.empty?
|
||||
association_options = args.first
|
||||
|
||||
if association_options.nil?
|
||||
__declare_attribute__(name, block)
|
||||
elsif args.first.respond_to?(:has_key?) && args.first.has_key?(:factory)
|
||||
association(name, *args)
|
||||
elsif __valid_association_options?(association_options)
|
||||
association(name, association_options)
|
||||
else
|
||||
raise NoMethodError.new(<<~MSG)
|
||||
undefined method '#{name}' in '#{@definition.name}' factory
|
||||
Did you mean? '#{name} { #{args.first.inspect} }'
|
||||
Did you mean? '#{name} { #{association_options.inspect} }'
|
||||
MSG
|
||||
end
|
||||
end
|
||||
|
@ -188,5 +190,9 @@ module FactoryBot
|
|||
add_attribute(name, &block)
|
||||
end
|
||||
end
|
||||
|
||||
def __valid_association_options?(options)
|
||||
options.respond_to?(:has_key?) && options.has_key?(:factory)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue