fixing deprecation notice for dynamic finders that use hashes. fixes #2404

This commit is contained in:
Aaron Patterson
2011-08-03 15:29:15 -07:00
parent 0c57ae102f
commit c1b85edc2d
2 changed files with 14 additions and 6 deletions

View File

@@ -1057,12 +1057,10 @@ module ActiveRecord #:nodoc:
if match = DynamicFinderMatch.match(method_id)
attribute_names = match.attribute_names
super unless all_attributes_exists?(attribute_names)
if arguments.size < attribute_names.size
ActiveSupport::Deprecation.warn(
"Calling dynamic finder with less number of arguments than the number of attributes in " \
"method name is deprecated and will raise an ArguementError in the next version of Rails. " \
"Please passing `nil' to the argument you want it to be nil."
)
if !arguments.first.is_a?(Hash) && arguments.size < attribute_names.size
ActiveSupport::Deprecation.warn(<<-eowarn)
Calling dynamic finder with less number of arguments than the number of attributes in method name is deprecated and will raise an ArguementError in the next version of Rails. Please passing `nil' to the argument you want it to be nil.
eowarn
end
if match.finder?
options = arguments.extract_options!

View File

@@ -661,6 +661,16 @@ class FinderTest < ActiveRecord::TestCase
assert_raise(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
end
def test_dynamic_finder_with_hash
assert_not_deprecated do
topic = Topic.find_or_create_by_title_and_author_name(
:title => "hi aaron!",
:author_name => "Aaron"
)
assert_equal 'hi aaron!', topic.title
end
end
def test_find_by_two_attributes
assert_equal topics(:first), Topic.find_by_title_and_author_name("The First Topic", "David")
assert_nil Topic.find_by_title_and_author_name("The First Topic", "Mary")