Class: AdminSuite::ResourcesController

Inherits:
ApplicationController show all
Includes:
Pagy::Backend, Pagy::Frontend
Defined in:
app/controllers/admin_suite/resources_controller.rb

Instance Method Summary collapse

Instance Method Details

#bulk_actionObject

POST /:portal/:resource_name/bulk_action/:action_name



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/admin_suite/resources_controller.rb', line 106

def bulk_action
  action = params[:action_name].to_s.to_sym
  action_def = find_bulk_action(action)
  return head(:not_found) if action_def.nil?

  ids = params[:ids] || []
  if ids.empty?
    redirect_to collection_url, alert: "No items selected."
    return
  end

  model = resource_class
  records = model.where(id: ids)
  executor = Admin::Base::ActionExecutor.new(resource_config, action, admin_suite_actor)
  result = executor.execute_bulk(records, params.to_unsafe_h)

  if result.success?
    redirect_to collection_url, notice: result.message
  else
    redirect_to collection_url, alert: result.message
  end
end

#createObject

POST /:portal/:resource_name



64
65
66
67
68
69
70
71
# File 'app/controllers/admin_suite/resources_controller.rb', line 64

def create
  @resource = resource_class.new(resource_params)
  if @resource.save
    redirect_to resource_url(@resource), notice: "#{resource_config.human_name} was successfully created."
  else
    render :new, status: :unprocessable_entity
  end
end

#destroyObject

DELETE /:portal/:resource_name/:id



83
84
85
86
# File 'app/controllers/admin_suite/resources_controller.rb', line 83

def destroy
  @resource.destroy!
  redirect_to collection_url, notice: "#{resource_config.human_name} was successfully deleted.", status: :see_other
end

#editObject

GET /:portal/:resource_name/:id/edit



60
61
# File 'app/controllers/admin_suite/resources_controller.rb', line 60

def edit
end

#execute_actionObject

POST /:portal/:resource_name/:id/execute_action/:action_name



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/admin_suite/resources_controller.rb', line 89

def execute_action
  action = params[:action_name].to_s.to_sym
  action_def = find_action(action)
  return head(:not_found) if action_def.nil?

  executor = Admin::Base::ActionExecutor.new(resource_config, action, admin_suite_actor)
  result = executor.execute_member(@resource, params.to_unsafe_h)
  redirect_target = result.redirect_url.presence || resource_url(@resource)

  if result.success?
    redirect_to redirect_target, notice: result.message
  else
    redirect_to resource_url(@resource), alert: result.message
  end
end

#indexObject

GET /:portal/:resource_name



24
25
26
27
28
# File 'app/controllers/admin_suite/resources_controller.rb', line 24

def index
  scope = filtered_collection
  @stats = calculate_stats(scope) if resource_config&.index_config&.stats_list&.any?
  @pagy, @collection = paginate_collection(scope)
end

#newObject

GET /:portal/:resource_name/new



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

def new
  @resource = resource_class.new
end

#searchObject

GET /:portal/:resource_name/search?q=term

Feeds searchable_select_controller.js's fetchOptions, which expects a bare JSON array (not {results: [...]}) of objects each carrying id/value and name/title/label. Matches that contract exactly -- the JS needs no changes.

Reuses FilterBuilder.search_predicate, the same ILIKE-over- searchable_fields logic the index's own search box uses, so this can only ever search the resource's declared searchable whitelist -- never an arbitrary column supplied via q. require_resource_config! and authorize_admin_suite! (both already-registered before_actions, the latter driven by AUTHORIZATION_VERBS["search"] = :read below) gate this exactly like every other action on this controller: unknown resource names 404 before either runs, and a denying config.authorize 403s before any query executes.



50
51
52
# File 'app/controllers/admin_suite/resources_controller.rb', line 50

def search
  render json: search_results.first(SEARCH_RESULT_LIMIT).map { |record| search_result_json(record) }
end

#showObject

GET /:portal/:resource_name/:id



31
32
# File 'app/controllers/admin_suite/resources_controller.rb', line 31

def show
end

#toggleObject

POST /:portal/:resource_name/:id/toggle



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/admin_suite/resources_controller.rb', line 130

def toggle
  field = params[:field].presence&.to_sym
  field ||= toggleable_fields.first if toggleable_fields.one?
  unless field
    head :unprocessable_entity
    return
  end

  unless toggleable_fields.include?(field)
    head :unprocessable_entity
    return
  end

  current_value = !!@resource.public_send(field)
  @resource.update!(field => !current_value)

  respond_to do |format|
    format.turbo_stream do
      render turbo_stream: turbo_stream.replace(
        dom_id(@resource, "#{field}_toggle"),
        partial: "admin_suite/shared/toggle_cell",
        locals: { record: @resource, field: field, toggle_url: resource_toggle_path(portal: current_portal, resource_name: resource_name, id: @resource.to_param, field: field) }
      )
    end
    format.html { redirect_to resource_url(@resource), notice: "#{resource_config.human_name} updated." }
  end
end

#updateObject

PATCH/PUT /:portal/:resource_name/:id



74
75
76
77
78
79
80
# File 'app/controllers/admin_suite/resources_controller.rb', line 74

def update
  if @resource.update(resource_params)
    redirect_to resource_url(@resource), notice: "#{resource_config.human_name} was successfully updated."
  else
    render :edit, status: :unprocessable_entity
  end
end