aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-03-30 09:28:12 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-03-30 09:55:21 +0900
commit0bfeb481a0aa35eeca1c5230938fa61fee0faef2 (patch)
tree705aebd8bd1f5f1344ee556ed26f1dadc2073980
parentb9be64cc3e2959c974c3036a0eb5a37fc8c7d580 (diff)
downloadrails-0bfeb481a0aa35eeca1c5230938fa61fee0faef2.tar.gz
rails-0bfeb481a0aa35eeca1c5230938fa61fee0faef2.tar.bz2
rails-0bfeb481a0aa35eeca1c5230938fa61fee0faef2.zip
Deprecate accessibility of private/protected class methods in named scope
-rw-r--r--activerecord/lib/active_record/relation.rb2
-rw-r--r--activerecord/lib/active_record/relation/delegation.rb6
-rw-r--r--activerecord/test/cases/scoping/named_scoping_test.rb7
-rw-r--r--activerecord/test/models/topic.rb5
4 files changed, 16 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 38b720ffc8..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)
@@ -453,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 27450ab8b3..488f71cdde 100644
--- a/activerecord/lib/active_record/relation/delegation.rb
+++ b/activerecord/lib/active_record/relation/delegation.rb
@@ -82,8 +82,10 @@ module ActiveRecord
if @klass.respond_to?(method)
self.class.delegate_to_scoped_klass(method)
scoping { @klass.public_send(method, *args, &block) }
- elsif defined?(@delegate_to_klass) &&
- @delegate_to_klass && @klass.respond_to?(method, true)
+ 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 \
diff --git a/activerecord/test/cases/scoping/named_scoping_test.rb b/activerecord/test/cases/scoping/named_scoping_test.rb
index ea71a5ce28..03f8a4f7c9 100644
--- a/activerecord/test/cases/scoping/named_scoping_test.rb
+++ b/activerecord/test/cases/scoping/named_scoping_test.rb
@@ -303,6 +303,13 @@ class NamedScopingTest < ActiveRecord::TestCase
assert_equal "lifo", topic.author_name
end
+ def test_deprecated_delegating_private_method
+ assert_deprecated do
+ scope = Topic.all.by_private_lifo
+ assert_not scope.instance_variable_get(:@delegate_to_klass)
+ end
+ end
+
def test_reserved_scope_names
klass = Class.new(ActiveRecord::Base) do
self.table_name = "topics"
diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb
index a924cf86ae..fa50eeb6a4 100644
--- a/activerecord/test/models/topic.rb
+++ b/activerecord/test/models/topic.rb
@@ -12,12 +12,13 @@ class Topic < ActiveRecord::Base
scope :scope_with_lambda, lambda { all }
- scope :by_lifo, -> { where(author_name: author_name) }
+ scope :by_private_lifo, -> { where(author_name: private_lifo) }
+ scope :by_lifo, -> { where(author_name: "lifo") }
scope :replied, -> { where "replies_count > 0" }
class << self
private
- def author_name
+ def private_lifo
"lifo"
end
end