From 37bac75c178663c0c60a4691aa8f7bf15213f674 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Thu, 9 Jan 2025 20:41:34 +0100 Subject: [PATCH] Slim `Scoping::Registry` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I know this is mirroring Active Record's `ScopeRegistry` but that code has always bothered me 😂 Here I'm just playing with slimming the code down as much as possible to accomplish what we need to here. --- lib/active_model/relation/scoping.rb | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/lib/active_model/relation/scoping.rb b/lib/active_model/relation/scoping.rb index 56dadb2..6851f58 100644 --- a/lib/active_model/relation/scoping.rb +++ b/lib/active_model/relation/scoping.rb @@ -7,33 +7,17 @@ module Scoping module ClassMethods def current_scope - ScopeRegistry.current_scope(self) + Registry.scopes[name] end def current_scope=(value) - ScopeRegistry.set_current_scope(self, value) + Registry.scopes.store(name, value) end end - class ScopeRegistry - class << self - delegate :current_scope, :set_current_scope, to: :instance - - def instance - ActiveSupport::IsolatedExecutionState[:active_model_scope_registry] ||= new - end - end - - def initialize - @current_scope = {} - end - - def current_scope(model) - @current_scope[model.name] - end - - def set_current_scope(model, value) - @current_scope[model.name] = value + class Registry + def self.scopes + ActiveSupport::IsolatedExecutionState[:active_model_relation_scope_registry] ||= {} end end end