Allow calling class methods on a Relation

This commit is contained in:
Pratik Naik
2010-01-22 20:10:29 +05:30
parent c52820ae15
commit ee8c006dac
2 changed files with 11 additions and 0 deletions

View File

@@ -301,6 +301,8 @@ module ActiveRecord
def method_missing(method, *args, &block)
if Array.method_defined?(method)
to_a.send(method, *args, &block)
elsif @klass.respond_to?(method)
@klass.send(:with_scope, self) { @klass.send(method, *args, &block) }
elsif arel.respond_to?(method)
arel.send(method, *args, &block)
elsif match = DynamicFinderMatch.match(method)

View File

@@ -380,6 +380,15 @@ class NamedScopeTest < ActiveRecord::TestCase
assert_deprecated('named_scope has been deprecated') { Topic.named_scope :deprecated_named_scope }
end
def test_named_scopes_on_relations
# Topic.replied
approved_topics = Topic.scoped.approved.order('id DESC')
assert_equal topics(:fourth), approved_topics.first
replied_approved_topics = approved_topics.replied
assert_equal topics(:third), replied_approved_topics.first
end
def test_index_on_named_scope
approved = Topic.approved.order('id ASC')
assert_equal topics(:second), approved[0]