aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-04-06 22:26:15 +0000
committerPratik Naik <pratiknaik@gmail.com>2008-04-06 22:26:15 +0000
commit4f39382a2fad5e299fce73ae0ba60f28dfddb21a (patch)
tree6b035a7f46206219bb0221917647f6182fe930b3 /activerecord/lib
parent917423d664038d6791738a73ad1446437dbb71df (diff)
downloadrails-4f39382a2fad5e299fce73ae0ba60f28dfddb21a.tar.gz
rails-4f39382a2fad5e299fce73ae0ba60f28dfddb21a.tar.bz2
rails-4f39382a2fad5e299fce73ae0ba60f28dfddb21a.zip
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
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/base.rb19
1 files changed, 17 insertions, 2 deletions
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