Skip to content
Browse files

Move argument validation into match

  • Loading branch information...
1 parent 1be248e commit fe12497e4d8ad292ffbcb4486a26b8802c19d65d @pwim pwim committed Jan 28, 2012
View
8 activerecord/lib/active_record/dynamic_finder_match.rb
@@ -36,6 +36,10 @@ def instantiator?
def bang?
false
end
+
+ def valid_arguments?(arguments)
+ arguments.size >= @attribute_names.size
+ end
end
class FindBy < DynamicFinderMatch
@@ -65,5 +69,9 @@ def self.match(method)
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
View
2 activerecord/lib/active_record/dynamic_matchers.rb
@@ -25,7 +25,7 @@ def method_missing(method_id, *arguments, &block)
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
View
4 activerecord/lib/active_record/dynamic_scope_match.rb
@@ -19,5 +19,9 @@ def initialize(scope, attribute_names)
attr_reader :scope, :attribute_names
alias :scope? :scope
+
+ def valid_arguments?(arguments)
+ arguments.size >= @attribute_names.size
+ end
end
end

0 comments on commit fe12497

Please sign in to comment.
Something went wrong with that request. Please try again.