diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-02-08 11:12:08 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2019-02-08 11:12:08 +0900 |
commit | 4cb1438b57067d637c79d49d0662c43b5b4e64c2 (patch) | |
tree | 835ce7c911c86955749b6c33c7e2b4fd03e87cac /activerecord/lib/active_record | |
parent | 17dd5cc367fc985b8562040d75151b85fde87f06 (diff) | |
download | rails-4cb1438b57067d637c79d49d0662c43b5b4e64c2.tar.gz rails-4cb1438b57067d637c79d49d0662c43b5b4e64c2.tar.bz2 rails-4cb1438b57067d637c79d49d0662c43b5b4e64c2.zip |
Refactor extracting `current_scope_restoring_block` into the scoping class
Relation is not best place to do this.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/scoping.rb | 8 |
2 files changed, 9 insertions, 5 deletions
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: |