class RailsAiAdmin::Registry
Maps stable slugs to explicit reloadable resources, never arbitrary constants.
Constants
- RESERVED
Public Class Methods
Source
# File lib/rails_ai_admin/registry.rb, line 8 def initialize(names) @entries = {} names.each do |name| resource = name.constantize raise ConfigurationError, "#{name} must inherit Resource" unless resource < Resource key = resource.key.to_s if !key.match?(/\A[a-z][a-z0-9_]*\z/) || RESERVED.include?(key) raise ConfigurationError, "Invalid or reserved resource key #{key}" end raise ConfigurationError, "Duplicate resource key #{key}" if @entries.key?(key) @entries[key] = resource.validate! end # Targets resolve at boot without loading models; reflection agreement # is checked on first use and by doctor. @entries.each_value do |resource| resource.fields.each do |field| next unless Field::REFERENCE_TYPES.include?(field.scalar_type) && !@entries.key?(field.options[:resource].to_s) raise ConfigurationError, "#{resource.name} #{field.scalar_type} #{field.name} names unregistered resource #{field.options[:resource]}" end resource.has_many_declarations.each do |panel| child = @entries[panel.resource.to_s] via = child&.fields&.find { |field| field.name == panel.via(resource) } unless via&.scalar_type == :belongs_to raise ConfigurationError, "#{resource.name} has_many #{panel.name} needs registered resource #{panel.resource} with belongs_to #{panel.via(resource)}" end end end @entries.freeze freeze end
Public Instance Methods
Source
# File lib/rails_ai_admin/registry.rb, line 39 def fetch(key) @entries.fetch(key.to_s) { raise AccessDenied } end
Source
# File lib/rails_ai_admin/registry.rb, line 43 def resources = @entries.values end