Skip to content
Browse files

default scope can accept any object that responds to #call

  • Loading branch information...
1 parent b1b26af commit e68f339aae4d3bc1bcf46b65cb8dcddc0ad2a435 @tenderlove tenderlove committed
Showing with 19 additions and 1 deletion.
  1. +1 −1 activerecord/lib/active_record/base.rb
  2. +18 −0 activerecord/test/cases/relation_scoping_test.rb
View
2 activerecord/lib/active_record/base.rb
@@ -1145,7 +1145,7 @@ def default_scope(options = {})
def current_scoped_methods #:nodoc:
method = scoped_methods.last
if method.respond_to?(:call)
- unscoped(&method)
+ relation.scoping { method.call }
else
method
end
View
18 activerecord/test/cases/relation_scoping_test.rb
@@ -322,6 +322,24 @@ def test_default_scope_with_lambda
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

0 comments on commit e68f339

Please sign in to comment.
Something went wrong with that request. Please try again.