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:
Daniel Colson 2019-04-26 11:41:51 -04:00
parent f898371f8f
commit 8e7e339323
No known key found for this signature in database
GPG Key ID: 88A364BBE77B1353
1 changed files with 10 additions and 4 deletions

View File

@ -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