From fe12497e4d8ad292ffbcb4486a26b8802c19d65d Mon Sep 17 00:00:00 2001 From: Paul McMahon Date: Sat, 28 Jan 2012 10:42:44 +0900 Subject: [PATCH] Move argument validation into match --- activerecord/lib/active_record/dynamic_finder_match.rb | 8 ++++++++ activerecord/lib/active_record/dynamic_matchers.rb | 2 +- activerecord/lib/active_record/dynamic_scope_match.rb | 4 ++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/dynamic_finder_match.rb b/activerecord/lib/active_record/dynamic_finder_match.rb index 0cdc49ae14..2b4f1bbf92 100644 --- a/activerecord/lib/active_record/dynamic_finder_match.rb +++ b/activerecord/lib/active_record/dynamic_finder_match.rb @@ -36,6 +36,10 @@ module ActiveRecord def bang? false end + + def valid_arguments?(arguments) + arguments.size >= @attribute_names.size + end end class FindBy < DynamicFinderMatch @@ -65,5 +69,9 @@ module ActiveRecord new(:first, $2, $1 == 'initialize' ? :new : :create) end end + + def valid_arguments?(arguments) + arguments.size == 1 && arguments.first.is_a?(Hash) || super + end end end diff --git a/activerecord/lib/active_record/dynamic_matchers.rb b/activerecord/lib/active_record/dynamic_matchers.rb index b6b8e24436..60ce3dd4f1 100644 --- a/activerecord/lib/active_record/dynamic_matchers.rb +++ b/activerecord/lib/active_record/dynamic_matchers.rb @@ -25,7 +25,7 @@ module ActiveRecord if match = (DynamicFinderMatch.match(method_id) || DynamicScopeMatch.match(method_id)) attribute_names = match.attribute_names super unless all_attributes_exists?(attribute_names) - if !(match.is_a?(DynamicFinderMatch) && match.instantiator? && arguments.first.is_a?(Hash)) && arguments.size < attribute_names.size + unless match.valid_arguments?(arguments) method_trace = "#{__FILE__}:#{__LINE__}:in `#{method_id}'" backtrace = [method_trace] + caller raise ArgumentError, "wrong number of arguments (#{arguments.size} for #{attribute_names.size})", backtrace diff --git a/activerecord/lib/active_record/dynamic_scope_match.rb b/activerecord/lib/active_record/dynamic_scope_match.rb index c832e927d6..a502155aac 100644 --- a/activerecord/lib/active_record/dynamic_scope_match.rb +++ b/activerecord/lib/active_record/dynamic_scope_match.rb @@ -19,5 +19,9 @@ module ActiveRecord attr_reader :scope, :attribute_names alias :scope? :scope + + def valid_arguments?(arguments) + arguments.size >= @attribute_names.size + end end end