1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Move argument validation into match

This commit is contained in:
Paul McMahon 2012-01-28 10:42:44 +09:00
parent 1be248e246
commit fe12497e4d
3 changed files with 13 additions and 1 deletions

View file

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

View file

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

View file

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