diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-10-19 15:07:44 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-10-19 15:07:44 -0700 |
commit | e68f339aae4d3bc1bcf46b65cb8dcddc0ad2a435 (patch) | |
tree | e6e2beee0e388075041519eb12cb80a9bd761b84 | |
parent | b1b26af9a2f1c2037f7c2167d747ed33cc639763 (diff) | |
download | rails-e68f339aae4d3bc1bcf46b65cb8dcddc0ad2a435.tar.gz rails-e68f339aae4d3bc1bcf46b65cb8dcddc0ad2a435.tar.bz2 rails-e68f339aae4d3bc1bcf46b65cb8dcddc0ad2a435.zip |
default scope can accept any object that responds to #call
-rw-r--r-- | activerecord/lib/active_record/base.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/relation_scoping_test.rb | 18 |
2 files changed, 19 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 879f02ff6a..6720f0687a 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -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 diff --git a/activerecord/test/cases/relation_scoping_test.rb b/activerecord/test/cases/relation_scoping_test.rb index 965bdacc1a..689cce8746 100644 --- a/activerecord/test/cases/relation_scoping_test.rb +++ b/activerecord/test/cases/relation_scoping_test.rb @@ -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 |