Skip to content
This repository
Browse code

Merge pull request #2128 from sikachu/master-dynamic_finder

Raise an ArgumentError if user passing less number of argument in the dynamic finder
  • Loading branch information...
commit 50941ec07ccea65757c0c9884bedfc9ff8af193a 2 parents 1e452f1 + 4443905
Jon Leighton jonleighton authored
14 activerecord/CHANGELOG
... ... @@ -1,3 +1,17 @@
  1 +*Rails 3.2.0 (unreleased)*
  2 +
  3 +* Active Record's dynamic finder will now raise the error if you passing in less number of arguments than what you call in method signature.
  4 +
  5 + So if you were doing this and expecting the second argument to be nil:
  6 +
  7 + User.find_by_username_and_group("sikachu")
  8 +
  9 + You'll now get `ArgumentError: wrong number of arguments (1 for 2).` You'll then have to do this:
  10 +
  11 + User.find_by_username_and_group("sikachu", nil)
  12 +
  13 + [Prem Sichanugrist]
  14 +
1 15 *Rails 3.1.0 (unreleased)*
2 16
3 17 * ActiveRecord::MacroReflection::AssociationReflection#build_record has a new method signature.
23 activerecord/lib/active_record/base.rb
@@ -1052,20 +1052,15 @@ def compute_table_name
1052 1052 # Each dynamic finder using <tt>scoped_by_*</tt> is also defined in the class after it
1053 1053 # is first invoked, so that future attempts to use it do not run through method_missing.
1054 1054 def method_missing(method_id, *arguments, &block)
1055   - if match = DynamicFinderMatch.match(method_id)
  1055 + if match = (DynamicFinderMatch.match(method_id) || DynamicScopeMatch.match(method_id))
1056 1056 attribute_names = match.attribute_names
1057 1057 super unless all_attributes_exists?(attribute_names)
1058   - if match.finder?
1059   - options = arguments.extract_options!
1060   - relation = options.any? ? scoped(options) : scoped
1061   - relation.send :find_by_attributes, match, attribute_names, *arguments, &block
1062   - elsif match.instantiator?
1063   - scoped.send :find_or_instantiator_by_attributes, match, attribute_names, *arguments, &block
  1058 + if arguments.size < attribute_names.size
  1059 + method_trace = "#{__FILE__}:#{__LINE__}:in `#{method_id}'"
  1060 + backtrace = [method_trace] + caller
  1061 + raise ArgumentError, "wrong number of arguments (#{arguments.size} for #{attribute_names.size})", backtrace
1064 1062 end
1065   - elsif match = DynamicScopeMatch.match(method_id)
1066   - attribute_names = match.attribute_names
1067   - super unless all_attributes_exists?(attribute_names)
1068   - if match.scope?
  1063 + if match.respond_to?(:scope?) && match.scope?
1069 1064 self.class_eval <<-METHOD, __FILE__, __LINE__ + 1
1070 1065 def self.#{method_id}(*args) # def self.scoped_by_user_name_and_password(*args)
1071 1066 attributes = Hash[[:#{attribute_names.join(',:')}].zip(args)] # attributes = Hash[[:user_name, :password].zip(args)]
@@ -1074,6 +1069,12 @@ def self.#{method_id}(*args) # def self.scope
1074 1069 end # end
1075 1070 METHOD
1076 1071 send(method_id, *arguments)
  1072 + elsif match.finder?
  1073 + options = arguments.extract_options!
  1074 + relation = options.any? ? scoped(options) : scoped
  1075 + relation.send :find_by_attributes, match, attribute_names, *arguments, &block
  1076 + elsif match.instantiator?
  1077 + scoped.send :find_or_instantiator_by_attributes, match, attribute_names, *arguments, &block
1077 1078 end
1078 1079 else
1079 1080 super
8 activerecord/test/cases/finder_test.rb
@@ -666,6 +666,10 @@ def test_find_by_two_attributes
666 666 assert_nil Topic.find_by_title_and_author_name("The First Topic", "Mary")
667 667 end
668 668
  669 + def test_find_by_two_attributes_but_passing_only_one
  670 + assert_raise(ArgumentError) { Topic.find_by_title_and_author_name("The First Topic") }
  671 + end
  672 +
669 673 def test_find_last_by_one_attribute
670 674 assert_equal Topic.last, Topic.find_last_by_title(Topic.last.title)
671 675 assert_nil Topic.find_last_by_title("A title with no matches")
@@ -947,6 +951,10 @@ def test_find_or_initialize_from_two_attributes
947 951 assert !another.persisted?
948 952 end
949 953
  954 + def test_find_or_initialize_from_two_attributes_but_passing_only_one
  955 + assert_raise(ArgumentError) { Topic.find_or_initialize_by_title_and_author_name("Another topic") }
  956 + end
  957 +
950 958 def test_find_or_initialize_from_one_aggregate_attribute_and_one_not
951 959 new_customer = Customer.find_or_initialize_by_balance_and_name(Money.new(123), "Elizabeth")
952 960 assert_equal 123, new_customer.balance.amount
20 activerecord/test/cases/named_scope_test.rb
@@ -497,14 +497,24 @@ def test_scoped_by
497 497 class DynamicScopeTest < ActiveRecord::TestCase
498 498 fixtures :posts
499 499
  500 + def setup
  501 + @test_klass = Class.new(Post) do
  502 + def self.name; "Post"; end
  503 + end
  504 + end
  505 +
500 506 def test_dynamic_scope
501   - assert_equal Post.scoped_by_author_id(1).find(1), Post.find(1)
502   - assert_equal Post.scoped_by_author_id_and_title(1, "Welcome to the weblog").first, Post.find(:first, :conditions => { :author_id => 1, :title => "Welcome to the weblog"})
  507 + assert_equal @test_klass.scoped_by_author_id(1).find(1), @test_klass.find(1)
  508 + assert_equal @test_klass.scoped_by_author_id_and_title(1, "Welcome to the weblog").first, @test_klass.find(:first, :conditions => { :author_id => 1, :title => "Welcome to the weblog"})
503 509 end
504 510
505 511 def test_dynamic_scope_should_create_methods_after_hitting_method_missing
506   - assert_blank Developer.methods.grep(/scoped_by_created_at/)
507   - Developer.scoped_by_created_at(nil)
508   - assert_present Developer.methods.grep(/scoped_by_created_at/)
  512 + assert_blank @test_klass.methods.grep(/scoped_by_type/)
  513 + @test_klass.scoped_by_type(nil)
  514 + assert_present @test_klass.methods.grep(/scoped_by_type/)
  515 + end
  516 +
  517 + def test_dynamic_scope_with_less_number_of_arguments
  518 + assert_raise(ArgumentError){ @test_klass.scoped_by_author_id_and_title(1) }
509 519 end
510 520 end

0 comments on commit 50941ec

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