diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-02-11 13:40:28 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-02-11 13:46:30 -0700 |
commit | 5e0b555b453ea2ca36986c111512627d806101e7 (patch) | |
tree | 20db60dde260daf1bbc85cf00c8c5de7bf50bb7b /activerecord/lib | |
parent | 42d62de0007b4b4813adc2eb27c3a83e57018ac4 (diff) | |
download | rails-5e0b555b453ea2ca36986c111512627d806101e7.tar.gz rails-5e0b555b453ea2ca36986c111512627d806101e7.tar.bz2 rails-5e0b555b453ea2ca36986c111512627d806101e7.zip |
`current_scope` shouldn't pollute sibling STI classes
It looks like the only reason `current_scope` was thread local on
`base_class` instead of `self` is to ensure that when we call a named
scope created with a proc on the parent class, it correctly uses the
default scope of the subclass. The reason this wasn't happening was
because the proc captured `self` as the parent class, and we're not
actually defining a real method. Using `instance_exec` fixes the
problem.
Fixes #18806
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/scoping.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/scoping/named.rb | 17 |
2 files changed, 15 insertions, 6 deletions
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 a083deafe1..43c7b1c574 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -157,11 +157,20 @@ module ActiveRecord extension = Module.new(&block) if block - singleton_class.send(:define_method, name) do |*args| - scope = all.scoping { body.call(*args) } - scope = scope.extending(extension) if extension + if body.respond_to?(:to_proc) + singleton_class.send(:define_method, name) do |*args| + scope = all.scoping { instance_exec(*args, &body) } + scope = scope.extending(extension) if extension - scope || all + scope || all + end + else + singleton_class.send(:define_method, name) do |*args| + scope = all.scoping { body.call(*args) } + scope = scope.extending(extension) if extension + + scope || all + end end end end |