Class: Admin::Base::Resource

Inherits:
Object
  • Object
show all
Extended by:
AdminSuite::Deprecation
Defined in:
lib/admin/base/resource.rb

Overview

Base class for admin resource definitions

Provides a declarative DSL for defining admin resources with:

  • Index configuration (columns, filters, search, sort, stats)
  • Form configuration (fields with various types)
  • Actions (single and bulk)
  • Show page sections
  • Export capabilities

Examples:

class Admin::Resources::CompanyResource < Admin::Base::Resource
  model Company
  portal :ops
  section :content

  index do
    searchable :name, :website
    sortable :name, :created_at, default: :name

    columns do
      column :name
      column :job_listings, -> (c) { c.job_listings.count }
    end

    filters do
      filter :search, type: :text
      filter :status, type: :select, options: %w[active inactive]
    end
  end

  form do
    field :name, required: true
    field :website, type: :url
    field :is_active, type: :toggle
  end
end

Defined Under Namespace

Classes: ActionDefinition, ActionsConfig, ColumnDefinition, ColumnsBuilder, FieldDefinition, FilterDefinition, FiltersBuilder, FormConfig, IndexConfig, RowDefinition, SectionDefinition, ShowConfig, ShowSectionDefinition, StatDefinition, StatsBuilder

Constant Summary collapse

SectionEnd =
Class.new
RowEnd =
Class.new

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from AdminSuite::Deprecation

reset_deprecation_notices!, warn_once, warn_once_sink

Class Attribute Details

.actions_config ⇒ Object (readonly)

Actions configuration



58
59
60
# File 'lib/admin/base/resource.rb', line 58

def actions_config
  @actions_config
end

.form_config ⇒ Object (readonly)

Form configuration



52
53
54
# File 'lib/admin/base/resource.rb', line 52

def form_config
  @form_config
end

.index_config ⇒ Object (readonly)

Index configuration



49
50
51
# File 'lib/admin/base/resource.rb', line 49

def index_config
  @index_config
end

.model_class ⇒ Object (readonly)

Model configuration



46
47
48
# File 'lib/admin/base/resource.rb', line 46

def model_class
  @model_class
end

Model configuration



46
47
48
# File 'lib/admin/base/resource.rb', line 46

def nav_icon
  @nav_icon
end

Model configuration



46
47
48
# File 'lib/admin/base/resource.rb', line 46

def nav_label
  @nav_label
end

Model configuration



46
47
48
# File 'lib/admin/base/resource.rb', line 46

def nav_order
  @nav_order
end

.portal_name ⇒ Object (readonly)

Model configuration



46
47
48
# File 'lib/admin/base/resource.rb', line 46

def portal_name
  @portal_name
end

.section_name ⇒ Object (readonly)

Model configuration



46
47
48
# File 'lib/admin/base/resource.rb', line 46

def section_name
  @section_name
end

.show_config ⇒ Object (readonly)

Show configuration



55
56
57
# File 'lib/admin/base/resource.rb', line 55

def show_config
  @show_config
end

Class Method Details

.actions { ... } ⇒ void

This method returns an undefined value.

Configures actions

Yields:

  • Block for actions configuration



154
155
156
157
# File 'lib/admin/base/resource.rb', line 154

def actions(&block)
  @actions_config = ActionsConfig.new
  @actions_config.instance_eval(&block) if block_given?
end

.form { ... } ⇒ void

This method returns an undefined value.

Configures the form (new/edit)

Yields:

  • Block for form configuration



136
137
138
139
# File 'lib/admin/base/resource.rb', line 136

def form(&block)
  @form_config = FormConfig.new
  @form_config.instance_eval(&block) if block_given?
end

.human_name ⇒ String

Returns the human-readable name

Returns:

  • (String)


196
197
198
# File 'lib/admin/base/resource.rb', line 196

def human_name
  model_class&.model_name&.human || resource_name.humanize
end

.human_name_plural ⇒ String

Returns the human-readable plural name

Returns:

  • (String)


203
204
205
# File 'lib/admin/base/resource.rb', line 203

def human_name_plural
  model_class&.model_name&.human(count: 2) || resource_name.pluralize.humanize
end

.icon(name = nil) ⇒ String, ...

Convenience setter/getter for nav icon.

Parameters:

  • name (String, Symbol, nil) (defaults to: nil)

Returns:

  • (String, Symbol, nil)


100
101
102
103
# File 'lib/admin/base/resource.rb', line 100

def icon(name = nil)
  @nav_icon = name if name.present?
  @nav_icon
end

.index { ... } ⇒ void

This method returns an undefined value.

Configures the index view

Yields:

  • Block for index configuration



127
128
129
130
# File 'lib/admin/base/resource.rb', line 127

def index(&block)
  @index_config = IndexConfig.new
  @index_config.instance_eval(&block) if block_given?
end

.inherited(subclass) ⇒ Object

Called when a subclass is created



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/admin/base/resource.rb', line 222

def inherited(subclass)
  super
  return if subclass.name&.include?("Base")

  existing_idx = registered_resources.index { |r| r.name == subclass.name }
  if existing_idx
    registered_resources[existing_idx] = subclass
  else
    registered_resources << subclass
  end
end

.label(name = nil) ⇒ String?

Convenience setter/getter for nav label.

Parameters:

  • name (String, nil) (defaults to: nil)

Returns:

  • (String, nil)


109
110
111
112
# File 'lib/admin/base/resource.rb', line 109

def label(name = nil)
  @nav_label = name if name.present?
  @nav_label
end

.mcp(enabled) ⇒ Object

Opts this resource out of the model-facing MCP surface.



171
172
173
# File 'lib/admin/base/resource.rb', line 171

def mcp(enabled)
  @mcp_enabled = enabled
end

.mcp_enabled? ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/admin/base/resource.rb', line 175

def mcp_enabled?
  @mcp_enabled.nil? ? true : !!@mcp_enabled
end

.model(klass) ⇒ void

This method returns an undefined value.

Sets the model class for this resource

Parameters:

  • klass (Class) —

    The ActiveRecord model class



64
65
66
# File 'lib/admin/base/resource.rb', line 64

def model(klass)
  @model_class = klass
end

This method returns an undefined value.

Navigation metadata for this resource.

Parameters:

  • label (String, nil) (defaults to: nil) —

    override label used in nav

  • icon (String, Symbol, nil) (defaults to: nil) —

    lucide icon name (or raw svg string)

  • order (Integer, nil) (defaults to: nil) —

    sort order within section



90
91
92
93
94
# File 'lib/admin/base/resource.rb', line 90

def nav(label: nil, icon: nil, order: nil)
  @nav_label = label if label.present?
  @nav_icon = icon if icon.present?
  @nav_order = order unless order.nil?
end

.order(value = nil) ⇒ Integer?

Convenience setter/getter for nav order.

Parameters:

  • value (Integer, nil) (defaults to: nil)

Returns:

  • (Integer, nil)


118
119
120
121
# File 'lib/admin/base/resource.rb', line 118

def order(value = nil)
  @nav_order = value unless value.nil?
  @nav_order
end

.portal(name) ⇒ void

This method returns an undefined value.

Sets the portal this resource belongs to

Parameters:

  • name (Symbol) —

    Portal name (:ops or :ai)



72
73
74
# File 'lib/admin/base/resource.rb', line 72

def portal(name)
  @portal_name = name
end

.read_only(value = true) ⇒ Object

Declares whether the generic resource routes may mutate records. Read-only resources retain index/show access while the controller rejects direct requests to every built-in mutation endpoint.



162
163
164
# File 'lib/admin/base/resource.rb', line 162

def read_only(value = true)
  @read_only = value
end

.read_only? ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/admin/base/resource.rb', line 166

def read_only?
  @read_only == true
end

.registered_resources ⇒ Array<Class>

Returns all registered resources

Returns:

  • (Array<Class>)


210
211
212
# File 'lib/admin/base/resource.rb', line 210

def registered_resources
  @registered_resources ||= []
end

.reset_registry! ⇒ void

This method returns an undefined value.

Clears the registry (useful for development reloads).



217
218
219
# File 'lib/admin/base/resource.rb', line 217

def reset_registry!
  @registered_resources = []
end

.resource_name ⇒ String

Returns the resource name derived from class name

Returns:

  • (String)


182
183
184
# File 'lib/admin/base/resource.rb', line 182

def resource_name
  name.demodulize.sub(/Resource$/, "").underscore
end

.resource_name_plural ⇒ String

Returns the plural resource name

Returns:

  • (String)


189
190
191
# File 'lib/admin/base/resource.rb', line 189

def resource_name_plural
  resource_name.pluralize
end

.resources_for_portal(portal) ⇒ Array<Class>

Returns resources for a specific portal

Parameters:

  • portal (Symbol) —

    Portal name

Returns:

  • (Array<Class>)


238
239
240
# File 'lib/admin/base/resource.rb', line 238

def resources_for_portal(portal)
  registered_resources.select { |r| r.portal_name == portal }
end

.resources_for_section(portal, section) ⇒ Array<Class>

Returns resources for a specific section

Parameters:

  • portal (Symbol) —

    Portal name

  • section (Symbol) —

    Section name

Returns:

  • (Array<Class>)


247
248
249
# File 'lib/admin/base/resource.rb', line 247

def resources_for_section(portal, section)
  registered_resources.select { |r| r.portal_name == portal && r.section_name == section }
end

.section(name) ⇒ void

This method returns an undefined value.

Sets the section within the portal

Parameters:

  • name (Symbol) —

    Section name



80
81
82
# File 'lib/admin/base/resource.rb', line 80

def section(name)
  @section_name = name
end

.show { ... } ⇒ void

This method returns an undefined value.

Configures the show view

Yields:

  • Block for show configuration



145
146
147
148
# File 'lib/admin/base/resource.rb', line 145

def show(&block)
  @show_config = ShowConfig.new
  @show_config.instance_eval(&block) if block_given?
end