Class: Panda::Core::WidgetRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/panda/core/widget_registry.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.widgetsObject (readonly)

Returns the value of attribute widgets.



9
10
11
# File 'lib/panda/core/widget_registry.rb', line 9

def widgets
  @widgets
end

Class Method Details

.build(user) ⇒ Array

Build the widget list for the current user. Combines the legacy admin_dashboard_widgets lambda with registered widgets.

Parameters:

  • user (Object)

    The current user

Returns:

  • (Array)

    Instantiated widget components



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/panda/core/widget_registry.rb', line 30

def build(user)
  # Legacy backward-compatible widgets from the lambda
  legacy = Panda::Core.config.admin_dashboard_widgets&.call(user) || []

  # Registered widgets — filter by visibility, sort by position, instantiate
  registered = @widgets
    .select { |w| w[:visible].nil? || w[:visible].call(user) }
    .sort_by { |w| w[:position] }
    .map { |w| w[:component].call(user) }

  legacy + registered
end

.register(label, component:, visible: nil, position: 0) ⇒ Object

Register a dashboard widget. Duplicate labels are replaced (last registration wins).

Parameters:

  • label (String)

    Widget label (used for identification and deduplication)

  • component (Proc)

    Lambda receiving user, returns an instantiated component

  • visible (Proc, nil) (defaults to: nil)

    Lambda receiving user, hides widget when false

  • position (Integer) (defaults to: 0)

    Sort order (lower numbers appear first)



17
18
19
20
21
22
23
24
# File 'lib/panda/core/widget_registry.rb', line 17

def register(label, component:, visible: nil, position: 0)
  widget = {label: label, component: component, visible: visible, position: position}
  if (index = @widgets.index { |w| w[:label] == label })
    @widgets[index] = widget
  else
    @widgets << widget
  end
end

.reset!Object

Clear all registrations (for test isolation).



44
45
46
# File 'lib/panda/core/widget_registry.rb', line 44

def reset!
  @widgets = []
end