Class: IronAdmin::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_admin/tool.rb

Overview

Base class for admin tools (custom pages in the admin panel).

Tools are standalone pages that don't map to a database model. External gems can subclass Tool to add custom admin functionality.

Examples:

Simple tool with declarative actions

class CacheManagerTool < IronAdmin::Tool
  menu icon: "server", label: "Cache Manager", group: "System"

  tool_action :clear_all, icon: "trash", confirm: true
  tool_action :flush_key,
    form_fields: [{ name: :cache_key, type: :text, required: true }]

  def clear_all(ctx)
    Rails.cache.clear
    ctx.flash[:notice] = "Cache cleared!"
  end

  def flush_key(ctx)
    key = ctx.action_params(:cache_key)[:cache_key]
    Rails.cache.delete(key)
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#contextToolContext?

Returns Request context, set by ToolsController before action execution.

Returns:

  • (ToolContext, nil)

    Request context, set by ToolsController before action execution



32
33
34
# File 'lib/iron_admin/tool.rb', line 32

def context
  @context
end

Class Method Details

.find_tool_action(name) ⇒ ToolAction?

Finds a declared tool action by name.

Parameters:

  • name (Symbol, String)

    Action name

Returns:



65
66
67
# File 'lib/iron_admin/tool.rb', line 65

def find_tool_action(name)
  defined_tool_actions.find { |a| a.name == name.to_sym }
end

.inherited(subclass) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/iron_admin/tool.rb', line 35

def inherited(subclass)
  super
  subclass.defined_tool_actions = defined_tool_actions.dup
  return if subclass.name.nil?

  begin
    IronAdmin::ToolRegistry.register(subclass)
  rescue NameError
    # ToolRegistry may not be loaded yet during boot
  end
end

.labelObject



77
78
79
# File 'lib/iron_admin/tool.rb', line 77

def label
  menu_options[:label] || tool_name.humanize
end


69
70
71
# File 'lib/iron_admin/tool.rb', line 69

def menu(**options)
  self.menu_options = options
end

.tool_action(name) ⇒ Object

Declares a tool action with metadata.

Parameters:

  • name (Symbol)

    Method name to call when action is executed

  • options (Hash)

    Action options passed to ToolAction.new



57
58
59
# File 'lib/iron_admin/tool.rb', line 57

def tool_action(name, **)
  self.defined_tool_actions = defined_tool_actions + [ToolAction.new(name: name, **)]
end

.tool_nameObject



73
74
75
# File 'lib/iron_admin/tool.rb', line 73

def tool_name
  name.sub(/Tool\z/, "").demodulize.underscore
end