From d1249c1a91df614ceb10167155b0265b9578835e Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 28 May 2017 10:12:18 +0900 Subject: Refactor `default_scoped` to avoid creating extra relation and merging --- activerecord/lib/active_record/scoping/named.rb | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'activerecord/lib/active_record/scoping/named.rb') diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index 27cdf8cb7e..d03c23fe5c 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -30,13 +30,8 @@ module ActiveRecord end def default_scoped # :nodoc: - scope = build_default_scope - - if scope - relation.spawn.merge!(scope) - else - relation - end + scope = relation + build_default_scope(scope) || scope end # Adds a class method for retrieving and querying objects. -- cgit v1.2.3 From 5632f73042bc543d59e6f3e913e0d2cd44b54a65 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Tue, 30 May 2017 04:37:25 +0900 Subject: Extract `default_extensions` to avoid `klass.all` As @matthewd's suggestion, if `klass` has no default scope, it will more faster. --- activerecord/lib/active_record/scoping/named.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/lib/active_record/scoping/named.rb') diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index d03c23fe5c..93c6f1d87e 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -34,6 +34,14 @@ module ActiveRecord build_default_scope(scope) || scope end + def default_extensions # :nodoc: + if scope = current_scope || build_default_scope + scope.extensions + else + [] + end + end + # Adds a class method for retrieving and querying objects. # The method is intended to return an ActiveRecord::Relation # object, which is composable with other scopes. -- cgit v1.2.3