lib/wcc/contentful/model_api.rb in wcc-contentful-1.1.1 vs lib/wcc/contentful/model_api.rb in wcc-contentful-1.1.2

- old
+ new

@@ -11,13 +11,12 @@ # override class-level _instrumentation from WCC::Contentful::Instrumentation def self._instrumentation services.instrumentation end - # We use a class var here because this is a global registry for all subclasses - # of this namespace - @@registry = {} # rubocop:disable Style/ClassVars + # Set the registry at the top of the namespace + @registry = {} end class_methods do attr_reader :configuration @@ -87,11 +86,11 @@ # Accepts a content type ID as a string and returns the Ruby constant # stored in the registry that represents this content type. def resolve_constant(content_type) raise ArgumentError, 'content_type cannot be nil' unless content_type - const = @@registry[content_type] + const = _registry[content_type] return const if const const_name = WCC::Contentful::Helpers.constant_from_content_type(content_type).to_s # #parent renamed to #module_parent in Rails 6 parent = try(:module_parent) || self.parent @@ -141,18 +140,18 @@ klass ||= self raise ArgumentError, "#{klass} must be a class constant!" unless klass.respond_to?(:new) content_type ||= WCC::Contentful::Helpers.content_type_from_constant(klass) - @@registry[content_type] = klass + _registry[content_type] = klass end # Returns the current registry of content type names to constants. def registry - return {} unless @@registry + return {} unless _registry - @@registry.dup.freeze + _registry.dup.freeze end def reload! registry = self.registry registry.each do |(content_type, klass)| @@ -174,9 +173,17 @@ # Checks if a content type has already been registered to a class and returns # that class. If nil, the generated WCC::Contentful::Model::{content_type} class # will be resolved for this content type. def registered?(content_type) - @@registry[content_type] + _registry[content_type] + end + + protected + + def _registry + # If calling register_for_content_type in a subclass, look up the superclass + # chain until we get to the model namespace which defines the registry + @registry || superclass._registry end end end