From 4cb1438b57067d637c79d49d0662c43b5b4e64c2 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Fri, 8 Feb 2019 11:12:08 +0900 Subject: Refactor extracting `current_scope_restoring_block` into the scoping class Relation is not best place to do this. --- activerecord/lib/active_record/relation.rb | 6 +----- activerecord/lib/active_record/scoping.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 5 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 32f0609798..347d745d19 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -67,11 +67,7 @@ module ActiveRecord # user = users.new { |user| user.name = 'Oscar' } # user.name # => Oscar def new(attributes = nil, &block) - current_scope = klass.current_scope(true) - block = -> record do - klass.current_scope = current_scope - yield record if block_given? - end + block = klass.current_scope_restoring_block(&block) scoping { klass.new(attributes, &block) } end diff --git a/activerecord/lib/active_record/scoping.rb b/activerecord/lib/active_record/scoping.rb index 35e9dcbffc..1142a87d25 100644 --- a/activerecord/lib/active_record/scoping.rb +++ b/activerecord/lib/active_record/scoping.rb @@ -30,6 +30,14 @@ module ActiveRecord def current_scope=(scope) ScopeRegistry.set_value_for(:current_scope, self, scope) end + + def current_scope_restoring_block(&block) + current_scope = self.current_scope(true) + -> *args do + self.current_scope = current_scope + yield(*args) if block_given? + end + end end def populate_with_current_scope_attributes # :nodoc: -- cgit v1.2.3