1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #38319 from kamipo/make_default_scoped_public

Expose `klass.default_scoped` as public API
This commit is contained in:
Ryuta Kamizono 2020-01-28 05:53:13 +09:00 committed by GitHub
commit 95bd55eca3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View file

@ -30,8 +30,7 @@ module ActiveRecord
ActiveSupport::Deprecation.warn(<<~MSG.squish)
Class level methods will no longer inherit scoping from `#{scope._deprecated_scope_source}`
in Rails 6.1. To continue using the scoped relation, pass it into the block directly.
To instead access the full set of models, as Rails 6.1 will, use `#{name}.unscoped`,
or `#{name}.default_scoped` if a model has default scopes.
To instead access the full set of models, as Rails 6.1 will, use `#{name}.default_scoped`.
MSG
end
@ -53,7 +52,8 @@ module ActiveRecord
end
end
def default_scoped(scope = relation) # :nodoc:
# Returns a scope for the model with default scopes.
def default_scoped(scope = relation)
build_default_scope(scope) || scope
end

View file

@ -95,6 +95,20 @@ class RelationScopingTest < ActiveRecord::TestCase
end
end
def test_scoped_unscoped
DeveloperOrderedBySalary.where("salary = 9000").scoping do
assert_equal 11, DeveloperOrderedBySalary.first.id
assert_equal 1, DeveloperOrderedBySalary.unscoped.first.id
end
end
def test_scoped_default_scoped
DeveloperOrderedBySalary.where("salary = 9000").scoping do
assert_equal 11, DeveloperOrderedBySalary.first.id
assert_equal 2, DeveloperOrderedBySalary.default_scoped.first.id
end
end
def test_scoped_find_all
Developer.where("name = 'David'").scoping do
assert_equal [developers(:david)], Developer.all