Class: IronAdmin::ResourceRegistry
- Inherits:
-
Object
- Object
- IronAdmin::ResourceRegistry
- Defined in:
- lib/iron_admin/resource_registry.rb
Overview
Registry that tracks all registered resource classes.
Resources are automatically registered when they are defined (via the inherited hook in Resource). This registry provides methods to look up resources by name and retrieve them for the sidebar menu.
Class Method Summary collapse
-
.all ⇒ Array<Class>
Returns all registered resource classes.
-
.finalize! ⇒ void
Runs every post-load registration step on every registered resource.
-
.find(resource_name) ⇒ Class?
Finds a resource class by its name.
-
.grouped ⇒ Hash{String => Array<Class>}
Returns resources grouped by their menu group.
-
.register(resource_class) ⇒ Class
Registers a resource class in the registry.
-
.reset! ⇒ void
private
Clears all registered resources.
-
.sorted ⇒ Array<Class>
Returns resources sorted by menu priority.
Class Method Details
.all ⇒ Array<Class>
Returns all registered resource classes.
91 92 93 |
# File 'lib/iron_admin/resource_registry.rb', line 91 def all resources.values end |
.finalize! ⇒ void
This method returns an undefined value.
Runs every post-load registration step on every registered resource.
Called by the engine's to_prepare block once all resource classes
have been eager-loaded and their bodies have fully evaluated. By
then adapter_class, model_class_override, etc. are set
correctly, so model introspection through the adapter is safe.
Soft-delete registration is idempotent; resources that succeeded eagerly during register are skipped here. Failures are logged and swallowed per resource so a single broken resource (missing model class, unreachable DB, etc.) doesn't take down the boot.
The cached @adapter instance is invalidated for every resource
before re-running registration. The eager Resource.adapter call
at inheritance time memoized an Adapters::ActiveRecord.new(model)
— built with whatever adapter_class defaulted to before the
class body's self.adapter_class = :mongoid (or :http) had
taken effect. Without this invalidation, every subsequent
Resource.adapter would keep returning that stale AR adapter
and Mongoid/HTTP resources would 500 with undefined method 'columns' for <MongoidModel>.
79 80 81 82 83 84 85 86 |
# File 'lib/iron_admin/resource_registry.rb', line 79 def finalize! all.each do |resource_class| resource_class.instance_variable_set(:@adapter, nil) resource_class.register_soft_delete_features rescue StandardError => e warn_finalize_failure(resource_class, e) end end |
.find(resource_name) ⇒ Class?
Finds a resource class by its name.
103 104 105 |
# File 'lib/iron_admin/resource_registry.rb', line 103 def find(resource_name) resources[resource_name] end |
.grouped ⇒ Hash{String => Array<Class>}
Returns resources grouped by their menu group.
Resources without a group are placed in "Resources".
119 120 121 |
# File 'lib/iron_admin/resource_registry.rb', line 119 def grouped all.group_by { |r| r.[:group] || "Resources" } end |
.register(resource_class) ⇒ Class
Registers a resource class in the registry.
Called automatically by Resource.inherited. The class is added to
the registry hash before any model introspection so that even
if soft-delete registration raises, the resource is still
discoverable (and will be retried by finalize!).
The eager soft-delete call fires with whatever adapter_class is
set at inheritance time, which for non-AR resources is the parent
default (:active_record) — that's wrong but expected. The error
is swallowed and finalize! re-runs with the correct adapter
after the class body has finished evaluating.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/iron_admin/resource_registry.rb', line 37 def register(resource_class) # Stage the registry key first so the resource is discoverable # even if `register_soft_delete_features` raises. We use a key # derived from the class name (no adapter/model lookup) so HTTP # resources without a Ruby model class — and any other case where # `resource_name` would touch a not-yet-loaded adapter — still # land in the registry. resources[registry_key_for(resource_class)] = resource_class begin resource_class.register_soft_delete_features rescue StandardError # Swallow eager-registration failures silently — `finalize!` # retries with the correct adapter once the class body has # fully evaluated, and logs there. nil end resource_class end |
.reset! ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Clears all registered resources.
137 138 139 |
# File 'lib/iron_admin/resource_registry.rb', line 137 def reset! @resources = {} end |
.sorted ⇒ Array<Class>
Returns resources sorted by menu priority.
Lower priority values appear first. Resources without a priority default to 999.
129 130 131 |
# File 'lib/iron_admin/resource_registry.rb', line 129 def sorted all.sort_by { |r| r.[:priority] || 999 } end |