aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2006-12-27 20:35:19 +0000
committerMichael Koziarski <michael@koziarski.com>2006-12-27 20:35:19 +0000
commit58edaaad0bc32a4b190329dcc8fb0e3bb1869c46 (patch)
tree3cea7a6d25e0b607d49bafcef98a193b8193609e /activerecord
parent014fdde93a7ebdcea79d27667c4a89c65e81cc6b (diff)
downloadrails-58edaaad0bc32a4b190329dcc8fb0e3bb1869c46.tar.gz
rails-58edaaad0bc32a4b190329dcc8fb0e3bb1869c46.tar.bz2
rails-58edaaad0bc32a4b190329dcc8fb0e3bb1869c46.zip
Ensure dynamic finders are anchored to the beginning of the method name to prevent
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5795 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rwxr-xr-xactiverecord/lib/active_record/base.rb4
-rw-r--r--activerecord/test/finder_test.rb7
2 files changed, 9 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 4c51d3f3ee..e4218a1a1c 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1187,7 +1187,7 @@ module ActiveRecord #:nodoc:
# It's even possible to use all the additional parameters to find. For example, the full interface for find_all_by_amount
# is actually find_all_by_amount(amount, options).
def method_missing(method_id, *arguments)
- if match = /find_(all_by|by)_([_a-zA-Z]\w*)/.match(method_id.to_s)
+ if match = /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(method_id.to_s)
finder, deprecated_finder = determine_finder(match), determine_deprecated_finder(match)
attribute_names = extract_attribute_names_from_match(match)
@@ -1219,7 +1219,7 @@ module ActiveRecord #:nodoc:
send(deprecated_finder, sanitize_sql(attributes), *arguments[attribute_names.length..-1])
end
end
- elsif match = /find_or_(initialize|create)_by_([_a-zA-Z]\w*)/.match(method_id.to_s)
+ elsif match = /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/.match(method_id.to_s)
instantiator = determine_instantiator(match)
attribute_names = extract_attribute_names_from_match(match)
super unless all_attributes_exists?(attribute_names)
diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb
index cf2dfcbb24..d31d859b44 100644
--- a/activerecord/test/finder_test.rb
+++ b/activerecord/test/finder_test.rb
@@ -293,6 +293,13 @@ class FinderTest < Test::Unit::TestCase
def test_find_by_one_missing_attribute
assert_raises(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
end
+
+ def test_find_by_invalid_method_syntax
+ assert_raises(NoMethodError) { Topic.fail_to_find_by_title("The First Topic") }
+ assert_raises(NoMethodError) { Topic.find_by_title?("The First Topic") }
+ assert_raises(NoMethodError) { Topic.fail_to_find_or_create_by_title("Nonexistent Title") }
+ assert_raises(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
+ end
def test_find_by_two_attributes
assert_equal topics(:first), Topic.find_by_title_and_author_name("The First Topic", "David")