From 4f39382a2fad5e299fce73ae0ba60f28dfddb21a Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 6 Apr 2008 22:26:15 +0000 Subject: Ensure that respond_to? considers dynamic finder methods. Closes #11538. [floehopper] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9235 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/base.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 92417a429f..8bef5ed2ae 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1263,6 +1263,13 @@ module ActiveRecord #:nodoc: defined?(@abstract_class) && @abstract_class == true end + def respond_to?(method_id, include_private = false) + if match = matches_dynamic_finder?(method_id) || matches_dynamic_finder_with_initialize_or_create?(method_id) + return true if all_attributes_exists?(extract_attribute_names_from_match(match)) + end + super + end + private def find_initial(options) options.update(:limit => 1) unless options[:include] @@ -1568,7 +1575,7 @@ module ActiveRecord #:nodoc: # Each dynamic finder or initializer/creator 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) - if match = /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(method_id.to_s) + if match = matches_dynamic_finder?(method_id) finder = determine_finder(match) attribute_names = extract_attribute_names_from_match(match) @@ -1592,7 +1599,7 @@ module ActiveRecord #:nodoc: end }, __FILE__, __LINE__ send(method_id, *arguments) - elsif match = /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/.match(method_id.to_s) + elsif match = matches_dynamic_finder_with_initialize_or_create?(method_id) instantiator = determine_instantiator(match) attribute_names = extract_attribute_names_from_match(match) super unless all_attributes_exists?(attribute_names) @@ -1630,6 +1637,14 @@ module ActiveRecord #:nodoc: end end + def matches_dynamic_finder?(method_id) + /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(method_id.to_s) + end + + def matches_dynamic_finder_with_initialize_or_create?(method_id) + /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/.match(method_id.to_s) + end + def determine_finder(match) match.captures.first == 'all_by' ? :find_every : :find_initial end -- cgit v1.2.3