default scope can accept any object that responds to #call

This commit is contained in:
Aaron Patterson
2010-10-19 15:07:44 -07:00
parent b1b26af9a2
commit e68f339aae
2 changed files with 19 additions and 1 deletions

View File

@@ -1145,7 +1145,7 @@ MSG
def current_scoped_methods #:nodoc:
method = scoped_methods.last
if method.respond_to?(:call)
unscoped(&method)
relation.scoping { method.call }
else
method
end

View File

@@ -322,6 +322,24 @@ class DefaultScopingTest < ActiveRecord::TestCase
assert_equal expected, received
end
def test_default_scope_with_thing_that_responds_to_call
klass = Class.new(ActiveRecord::Base) do
self.table_name = 'posts'
end
klass.class_eval do
default_scope Class.new(Struct.new(:klass)) {
def call
klass.where(:author_id => 2)
end
}.new(self)
end
records = klass.all
assert_equal 1, records.length
assert_equal 2, records.first.author_id
end
def test_default_scope_is_unscoped_on_find
assert_equal 1, DeveloperCalledDavid.count
assert_equal 11, DeveloperCalledDavid.unscoped.count