From 8e7e3393231791820f4ec214e7ec5308c6083ad6 Mon Sep 17 00:00:00 2001 From: Daniel Colson Date: Fri, 26 Apr 2019 11:41:51 -0400 Subject: [PATCH] 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. --- lib/factory_bot/definition_proxy.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/factory_bot/definition_proxy.rb b/lib/factory_bot/definition_proxy.rb index 323eaf2..32a0d1d 100644 --- a/lib/factory_bot/definition_proxy.rb +++ b/lib/factory_bot/definition_proxy.rb @@ -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