Skip to content
Permalink
Browse files

Refactor around scoping

Don't use `false` as special value to skip to find inherited scope, we
could use `skip_inherited_scope = true`, and move `_scoping` back on
Relation.
  • Loading branch information...
kamipo committed Feb 7, 2019
1 parent 973b62d commit 2e018361c7c51e36d1d98bf770b7456d78dee68b
@@ -312,12 +312,12 @@ def cache_key(timestamp_column = :updated_at)
# Please check unscoped if you want to remove all previous scopes (including
# the default_scope) during the execution of a block.
def scoping
@delegate_to_klass && klass.current_scope ? yield : klass._scoping(self) { yield }
@delegate_to_klass && klass.current_scope(true) ? yield : _scoping(self) { yield }
end

def _exec_scope(*args, &block) # :nodoc:
@delegate_to_klass = true
instance_exec(*args, &block) || self
_scoping(nil) { instance_exec(*args, &block) || self }
ensure
@delegate_to_klass = false
end
@@ -632,6 +632,13 @@ def load_records(records)
end

private
def _scoping(scope)
previous, klass.current_scope = klass.current_scope(true), scope
yield
ensure
klass.current_scope = previous
end

def _substitute_values(values)
values.map do |name, value|
attr = arel_attribute(name)
@@ -8,7 +8,7 @@ module ActiveRecord
module SpawnMethods
# This is overridden by Associations::CollectionProxy
def spawn #:nodoc:
@delegate_to_klass && klass.current_scope ? klass.all : clone
@delegate_to_klass && klass.current_scope(true) ? klass.all : clone
end

# Merges in the conditions from <tt>other</tt>, if <tt>other</tt> is an ActiveRecord::Relation.
@@ -27,10 +27,9 @@ def current_scope(skip_inherited_scope = false)
ScopeRegistry.value_for(:current_scope, self, skip_inherited_scope)
end

private
def current_scope=(scope)
ScopeRegistry.set_value_for(:current_scope, self, scope)
end
def current_scope=(scope)
ScopeRegistry.set_value_for(:current_scope, self, scope)
end
end

def populate_with_current_scope_attributes # :nodoc:
@@ -84,7 +83,7 @@ def value_for(scope_type, model, skip_inherited_scope = false)
base = model.base_class
while klass <= base
value = @registry[scope_type][klass.name]
return value || nil unless value.nil?
return value if value
klass = klass.superclass
end
end
@@ -31,14 +31,7 @@ module ClassMethods
# Post.limit(10) # Fires "SELECT * FROM posts LIMIT 10"
# }
def unscoped
block_given? ? _scoping(relation) { yield } : relation
end

def _scoping(relation) # :nodoc:
previous, self.current_scope = current_scope(true), relation
yield
ensure
self.current_scope = previous
block_given? ? relation.scoping { yield } : relation
end

# Are there attributes associated with this scope?
@@ -180,8 +180,7 @@ def scope(name, body, &block)

if body.respond_to?(:to_proc)
singleton_class.define_method(name) do |*args|
scope = all
scope = _scoping(false) { scope._exec_scope(*args, &body) }
scope = all._exec_scope(*args, &body)
scope = scope.extending(extension) if extension
scope
end

0 comments on commit 2e01836

Please sign in to comment.
You can’t perform that action at this time.