diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-07-18 01:24:33 -0700 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-07-18 01:24:33 -0700 |
commit | 50941ec07ccea65757c0c9884bedfc9ff8af193a (patch) | |
tree | 51f79bca390b3ffdebe669b4741c338f2119a5e2 /activerecord/lib | |
parent | 1e452f18402b1aa4fa90d0e990de1df23e7f1c52 (diff) | |
parent | 4443905169b54845090b7f8f863dfc820513e469 (diff) | |
download | rails-50941ec07ccea65757c0c9884bedfc9ff8af193a.tar.gz rails-50941ec07ccea65757c0c9884bedfc9ff8af193a.tar.bz2 rails-50941ec07ccea65757c0c9884bedfc9ff8af193a.zip |
Merge pull request #2128 from sikachu/master-dynamic_finder
Raise an ArgumentError if user passing less number of argument in the dynamic finder
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/base.rb | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index deff1c65ef..25074c2d4f 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1052,20 +1052,15 @@ module ActiveRecord #:nodoc: # Each dynamic finder using <tt>scoped_by_*</tt> is also defined in the class after it # is first invoked, so that future attempts to use it do not run through method_missing. def method_missing(method_id, *arguments, &block) - if match = DynamicFinderMatch.match(method_id) + if match = (DynamicFinderMatch.match(method_id) || DynamicScopeMatch.match(method_id)) attribute_names = match.attribute_names super unless all_attributes_exists?(attribute_names) - if match.finder? - options = arguments.extract_options! - relation = options.any? ? scoped(options) : scoped - relation.send :find_by_attributes, match, attribute_names, *arguments, &block - elsif match.instantiator? - scoped.send :find_or_instantiator_by_attributes, match, attribute_names, *arguments, &block + if arguments.size < attribute_names.size + method_trace = "#{__FILE__}:#{__LINE__}:in `#{method_id}'" + backtrace = [method_trace] + caller + raise ArgumentError, "wrong number of arguments (#{arguments.size} for #{attribute_names.size})", backtrace end - elsif match = DynamicScopeMatch.match(method_id) - attribute_names = match.attribute_names - super unless all_attributes_exists?(attribute_names) - if match.scope? + if match.respond_to?(:scope?) && match.scope? self.class_eval <<-METHOD, __FILE__, __LINE__ + 1 def self.#{method_id}(*args) # def self.scoped_by_user_name_and_password(*args) attributes = Hash[[:#{attribute_names.join(',:')}].zip(args)] # attributes = Hash[[:user_name, :password].zip(args)] @@ -1074,6 +1069,12 @@ module ActiveRecord #:nodoc: end # end METHOD send(method_id, *arguments) + elsif match.finder? + options = arguments.extract_options! + relation = options.any? ? scoped(options) : scoped + relation.send :find_by_attributes, match, attribute_names, *arguments, &block + elsif match.instantiator? + scoped.send :find_or_instantiator_by_attributes, match, attribute_names, *arguments, &block end else super |