aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2018-04-06 13:16:01 -0400
committerGitHub <noreply@github.com>2018-04-06 13:16:01 -0400
commit7fac2b496a52f46c76090d02624f3ff53ae4bc48 (patch)
tree570675e6e46ef438121ef54548bee69c9bdc88f1 /activerecord/lib
parent00f2dbd09b676f0efb38f4d1dc2d9f047d838309 (diff)
parent0bfeb481a0aa35eeca1c5230938fa61fee0faef2 (diff)
downloadrails-7fac2b496a52f46c76090d02624f3ff53ae4bc48.tar.gz
rails-7fac2b496a52f46c76090d02624f3ff53ae4bc48.tar.bz2
rails-7fac2b496a52f46c76090d02624f3ff53ae4bc48.zip
Merge pull request #32355 from kamipo/delegate_to_klass_in_a_scope
Bring back private class methods accessibility in named scope
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/relation.rb9
-rw-r--r--activerecord/lib/active_record/relation/delegation.rb5
-rw-r--r--activerecord/lib/active_record/scoping/named.rb2
3 files changed, 15 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 34e643b2de..38a364ff88 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -30,6 +30,7 @@ module ActiveRecord
@offsets = {}
@loaded = false
@predicate_builder = predicate_builder
+ @delegate_to_klass = false
end
def initialize_copy(other)
@@ -313,6 +314,13 @@ module ActiveRecord
klass.current_scope = previous
end
+ def _exec_scope(*args, &block) # :nodoc:
+ @delegate_to_klass = true
+ instance_exec(*args, &block) || self
+ ensure
+ @delegate_to_klass = false
+ end
+
# Updates all records in the current relation with details given. This method constructs a single SQL UPDATE
# statement and sends it straight to the database. It does not instantiate the involved models and it does not
# trigger Active Record callbacks or validations. However, values passed to #update_all will still go through
@@ -446,6 +454,7 @@ module ActiveRecord
end
def reset
+ @delegate_to_klass = false
@to_sql = @arel = @loaded = @should_eager_load = nil
@records = [].freeze
@offsets = {}
diff --git a/activerecord/lib/active_record/relation/delegation.rb b/activerecord/lib/active_record/relation/delegation.rb
index 4b16b49cdf..488f71cdde 100644
--- a/activerecord/lib/active_record/relation/delegation.rb
+++ b/activerecord/lib/active_record/relation/delegation.rb
@@ -82,6 +82,11 @@ module ActiveRecord
if @klass.respond_to?(method)
self.class.delegate_to_scoped_klass(method)
scoping { @klass.public_send(method, *args, &block) }
+ elsif @delegate_to_klass && @klass.respond_to?(method, true)
+ ActiveSupport::Deprecation.warn \
+ "Delegating missing #{method} method to #{@klass}. " \
+ "Accessibility of private/protected class methods in :scope is deprecated and will be removed in Rails 6.0."
+ @klass.send(method, *args, &block)
elsif arel.respond_to?(method)
ActiveSupport::Deprecation.warn \
"Delegating #{method} to arel is deprecated and will be removed in Rails 6.0."
diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb
index 752655aa05..a784001587 100644
--- a/activerecord/lib/active_record/scoping/named.rb
+++ b/activerecord/lib/active_record/scoping/named.rb
@@ -183,7 +183,7 @@ module ActiveRecord
if body.respond_to?(:to_proc)
singleton_class.send(:define_method, name) do |*args|
scope = all
- scope = scope.instance_exec(*args, &body) || scope
+ scope = scope._exec_scope(*args, &body)
scope = scope.extending(extension) if extension
scope
end