From 9c3afdc327132c7f1f4d05eebc0c05b715442e7d Mon Sep 17 00:00:00 2001 From: Jefferson Lai Date: Mon, 10 Feb 2014 03:00:05 -0800 Subject: Fixes Issue #13466. Changed the call to a scope block to be evaluated with instance_eval. The result is that ScopeRegistry can use the actual class instead of base_class when caching scopes so queries made by classes with a common ancestor won't leak scopes. --- activerecord/lib/active_record/scoping.rb | 4 ++-- activerecord/lib/active_record/scoping/named.rb | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/scoping.rb b/activerecord/lib/active_record/scoping.rb index 3e43591672..fca4f1c9d3 100644 --- a/activerecord/lib/active_record/scoping.rb +++ b/activerecord/lib/active_record/scoping.rb @@ -11,11 +11,11 @@ module ActiveRecord module ClassMethods def current_scope #:nodoc: - ScopeRegistry.value_for(:current_scope, base_class.to_s) + ScopeRegistry.value_for(:current_scope, self.to_s) end def current_scope=(scope) #:nodoc: - ScopeRegistry.set_value_for(:current_scope, base_class.to_s, scope) + ScopeRegistry.set_value_for(:current_scope, self.to_s, scope) end end diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index 49cadb66d0..826b710e92 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -148,9 +148,13 @@ module ActiveRecord extension = Module.new(&block) if block singleton_class.send(:define_method, name) do |*args| - scope = all.scoping { body.call(*args) } + if body.respond_to?(:to_proc) + scope = all.scoping { instance_exec(*args, &body) } + else + # Body is given as an object instead of a block, so invoke call() + scope = all.scoping { body.call(*args) } + end scope = scope.extending(extension) if extension - scope || all end end -- cgit v1.2.3