Class: IronAdmin::ResourcesController

Inherits:
ApplicationController show all
Includes:
Concerns::ActionExecutable, Concerns::Filterable, Concerns::JsonParamsCoercion, Concerns::NestedPermittable, Concerns::Scopeable, Concerns::Searchable
Defined in:
app/controllers/iron_admin/resources_controller.rb

Overview

Main controller handling CRUD operations for all admin resources.

This controller dynamically handles requests for any registered resource, providing index, show, new, create, edit, update, and destroy actions. It also handles custom actions, bulk actions, and autocomplete.

Routes are structured as:

  • GET /admin/:resource_name -> index
  • GET /admin/:resource_name/new -> new
  • POST /admin/:resource_name -> create
  • GET /admin/:resource_name/:id -> show
  • GET /admin/:resource_name/:id/edit -> edit
  • PATCH /admin/:resource_name/:id -> update
  • DELETE /admin/:resource_name/:id -> destroy

See Also:

Instance Method Summary collapse

Instance Method Details

#action_formvoid

This method returns an undefined value.

Renders the action form for a single record action with form_fields.



117
118
119
120
121
122
123
124
125
# File 'app/controllers/iron_admin/resources_controller.rb', line 117

def action_form
  @action = find_single_record_action
  return unless @action

  return unless prepare_action_record?(@action)

  @form_fields = @action[:form_fields]
  @form_url = resource_action_path(@resource_class.resource_name, @record, @action[:name])
end

#autocompletevoid

This method returns an undefined value.

Returns autocomplete results for belongs_to fields.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/controllers/iron_admin/resources_controller.rb', line 196

def autocomplete
  query = params[:q].to_s.strip
  return render json: [] if query.blank?

  display = @resource_class.display_attribute
  display = searchable_display_attribute unless adapter.has_column?(display)
  return render json: [] unless display

  scope = adapter.search_column(base_scope, display, query)
  records = adapter.limit(scope, 20)
    .map { |record| { id: record.to_param.to_s, label: record.public_send(display).to_s } }

  render json: records
end

#bulk_action_formvoid

This method returns an undefined value.

Renders the action form for a bulk action with form_fields.



130
131
132
133
134
135
136
137
138
# File 'app/controllers/iron_admin/resources_controller.rb', line 130

def bulk_action_form
  action = find_bulk_action
  return head(:not_found) unless action
  return head(:forbidden) unless action_authorized?(action[:name])

  @action = action
  @form_fields = action[:form_fields]
  @form_url = resource_bulk_action_path(@resource_class.resource_name, action[:name])
end

#createvoid

This method returns an undefined value.

Creates a new record.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/iron_admin/resources_controller.rb', line 71

def create
  @record = adapter.build(resource_params)

  if adapter.save(@record)
    emit_event(:create, @record)
    redirect_to resource_path(@resource_class.resource_name, @record),
                notice: I18n.t("iron_admin.resources.create.success", model: adapter.human_name)
  else
    @fields = form_fields
    render :new, status: :unprocessable_content
  end
end

#destroyvoid

This method returns an undefined value.

Deletes a record.

Raises:



106
107
108
109
110
111
112
# File 'app/controllers/iron_admin/resources_controller.rb', line 106

def destroy
  @record = find_record(record_scope, params[:id])
  adapter.destroy!(@record)
  emit_event(:destroy, @record)
  redirect_to resources_path(@resource_class.resource_name),
              notice: I18n.t("iron_admin.resources.destroy.success", model: adapter.human_name)
end

#editvoid

This method returns an undefined value.

Renders the edit form for an existing record.

Raises:



63
64
65
66
# File 'app/controllers/iron_admin/resources_controller.rb', line 63

def edit
  @record = find_record(record_scope, params[:id])
  @fields = form_fields
end

#execute_actionvoid

This method returns an undefined value.

Executes a custom action on a single record.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/controllers/iron_admin/resources_controller.rb', line 143

def execute_action
  action = find_single_record_action
  return unless action

  return unless prepare_action_record?(action)

  collected = action_form_params(action)

  adapter.transaction do
    adapter.wrap_rollback do
      result = call_action_block(action[:block], @record, collected)
      emit_event(params[:action_name], @record)
      raise IronAdmin::Rollback if result == false
    end
  end

  redirect_to resource_path(@resource_class.resource_name, @record),
              notice: I18n.t("iron_admin.resources.action.success")
rescue IronAdmin::RecordNotFound
  head(:not_found)
rescue StandardError => e
  redirect_to resources_path(@resource_class.resource_name),
              alert: I18n.t("iron_admin.resources.action.failure", error: e.message)
end

#execute_bulk_actionvoid

This method returns an undefined value.

Executes a bulk action on multiple selected records.

All records are processed within a database transaction. If the action block returns false, the transaction is rolled back.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/controllers/iron_admin/resources_controller.rb', line 174

def execute_bulk_action
  ids = bulk_action_ids
  return redirect_bulk(:alert, I18n.t("iron_admin.resources.bulk_action.no_records")) if ids.empty?

  records = adapter.filter(base_scope, :id, ids)
  action = find_bulk_action

  return head(:not_found) unless action
  return head(:forbidden) unless action_authorized?(action[:name])
  unless all_records_accessible?(records, ids)
    return redirect_bulk(:alert, I18n.t("iron_admin.resources.bulk_action.inaccessible"))
  end

  run_bulk_action_in_transaction(action, records)
  redirect_bulk(:notice, I18n.t("iron_admin.resources.bulk_action.success"))
rescue StandardError => e
  redirect_bulk(:alert, I18n.t("iron_admin.resources.bulk_action.failure", error: e.message))
end

#indexvoid

This method returns an undefined value.

Lists all records for the resource with filtering, sorting, and pagination.



36
37
38
39
40
# File 'app/controllers/iron_admin/resources_controller.rb', line 36

def index
  @pagy, @records = pagy(collection_scope, limit: IronAdmin.configuration.per_page)
  @fields = index_fields
  @current_scope = current_scope_name
end

#newvoid

This method returns an undefined value.

Renders the new record form.



54
55
56
57
# File 'app/controllers/iron_admin/resources_controller.rb', line 54

def new
  @record = adapter.build
  @fields = form_fields
end

#showvoid

This method returns an undefined value.

Shows a single record.

Raises:



46
47
48
49
# File 'app/controllers/iron_admin/resources_controller.rb', line 46

def show
  @record = find_record(record_scope, params[:id])
  @fields = @resource_class.resolved_fields
end

#updatevoid

This method returns an undefined value.

Updates an existing record.

Raises:



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/iron_admin/resources_controller.rb', line 88

def update
  @record = find_record(record_scope, params[:id])
  purge_attachments(@record)

  if adapter.update(@record, resource_params)
    emit_event(:update, @record)
    redirect_to resource_path(@resource_class.resource_name, @record),
                notice: I18n.t("iron_admin.resources.update.success", model: adapter.human_name)
  else
    @fields = form_fields
    render :edit, status: :unprocessable_content
  end
end