Deprecate accessibility of private/protected class methods in named scope

This commit is contained in:
Ryuta Kamizono 2018-03-30 09:28:12 +09:00
parent b9be64cc3e
commit 0bfeb481a0
4 changed files with 16 additions and 4 deletions

View File

@ -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 = {}

View File

@ -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 \

View File

@ -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"

View File

@ -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