Class: Admin::Base::Resource

Inherits:
Object
  • Object
show all
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

Class Attribute Details

.actions_configObject (readonly)

Actions configuration



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

def actions_config
  @actions_config
end

.export_formatsObject (readonly)

Export configuration



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

def export_formats
  @export_formats
end

.form_configObject (readonly)

Form configuration



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

def form_config
  @form_config
end

.index_configObject (readonly)

Index configuration



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

def index_config
  @index_config
end

.model_classObject (readonly)

Model configuration



44
45
46
# File 'lib/admin/base/resource.rb', line 44

def model_class
  @model_class
end

Model configuration



44
45
46
# File 'lib/admin/base/resource.rb', line 44

def nav_icon
  @nav_icon
end

Model configuration



44
45
46
# File 'lib/admin/base/resource.rb', line 44

def nav_label
  @nav_label
end

Model configuration



44
45
46
# File 'lib/admin/base/resource.rb', line 44

def nav_order
  @nav_order
end

.portal_nameObject (readonly)

Model configuration



44
45
46
# File 'lib/admin/base/resource.rb', line 44

def portal_name
  @portal_name
end

.section_nameObject (readonly)

Model configuration



44
45
46
# File 'lib/admin/base/resource.rb', line 44

def section_name
  @section_name
end

.show_configObject (readonly)

Show configuration



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

def show_config
  @show_config
end

Class Method Details

.actions { ... } ⇒ void

This method returns an undefined value.

Configures actions

Yields:

  • Block for actions configuration



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

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

.exportable(*formats) ⇒ void

This method returns an undefined value.

Configures export formats

Parameters:

  • formats (Array<Symbol>)

    Export formats (:json, :csv)



164
165
166
# File 'lib/admin/base/resource.rb', line 164

def exportable(*formats)
  @export_formats = formats
end

.form { ... } ⇒ void

This method returns an undefined value.

Configures the form (new/edit)

Yields:

  • Block for form configuration



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

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

.human_nameString

Returns the human-readable name

Returns:

  • (String)


185
186
187
# File 'lib/admin/base/resource.rb', line 185

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

.human_name_pluralString

Returns the human-readable plural name

Returns:

  • (String)


192
193
194
# File 'lib/admin/base/resource.rb', line 192

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)


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

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



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

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



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/admin/base/resource.rb', line 211

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)


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

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

.model(klass) ⇒ void

This method returns an undefined value.

Sets the model class for this resource

Parameters:

  • klass (Class)

    The ActiveRecord model class



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

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



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

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)


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

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)



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

def portal(name)
  @portal_name = name
end

.registered_resourcesArray<Class>

Returns all registered resources

Returns:

  • (Array<Class>)


199
200
201
# File 'lib/admin/base/resource.rb', line 199

def registered_resources
  @registered_resources ||= []
end

.reset_registry!void

This method returns an undefined value.

Clears the registry (useful for development reloads).



206
207
208
# File 'lib/admin/base/resource.rb', line 206

def reset_registry!
  @registered_resources = []
end

.resource_nameString

Returns the resource name derived from class name

Returns:

  • (String)


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

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

.resource_name_pluralString

Returns the plural resource name

Returns:

  • (String)


178
179
180
# File 'lib/admin/base/resource.rb', line 178

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>)


227
228
229
# File 'lib/admin/base/resource.rb', line 227

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>)


236
237
238
# File 'lib/admin/base/resource.rb', line 236

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



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

def section(name)
  @section_name = name
end

.show { ... } ⇒ void

This method returns an undefined value.

Configures the show view

Yields:

  • Block for show configuration



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

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