Class: AdminSuite::ResourcesController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ApplicationController
- AdminSuite::ResourcesController
- Includes:
- Pagy::Backend, Pagy::Frontend
- Defined in:
- app/controllers/admin_suite/resources_controller.rb
Instance Method Summary collapse
-
#bulk_action ⇒ Object
POST /:portal/:resource_name/bulk_action/:action_name.
-
#create ⇒ Object
POST /:portal/:resource_name.
-
#destroy ⇒ Object
DELETE /:portal/:resource_name/:id.
-
#edit ⇒ Object
GET /:portal/:resource_name/:id/edit.
-
#execute_action ⇒ Object
POST /:portal/:resource_name/:id/execute_action/:action_name.
-
#index ⇒ Object
GET /:portal/:resource_name.
-
#new ⇒ Object
GET /:portal/:resource_name/new.
-
#search ⇒ Object
GET /:portal/:resource_name/search?q=term.
-
#show ⇒ Object
GET /:portal/:resource_name/:id.
-
#toggle ⇒ Object
POST /:portal/:resource_name/:id/toggle.
-
#update ⇒ Object
PATCH/PUT /:portal/:resource_name/:id.
Instance Method Details
#bulk_action ⇒ Object
POST /:portal/:resource_name/bulk_action/:action_name
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 107 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. else redirect_to collection_url, alert: result. end end |
#create ⇒ Object
POST /:portal/:resource_name
65 66 67 68 69 70 71 72 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 65 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 |
#destroy ⇒ Object
DELETE /:portal/:resource_name/:id
84 85 86 87 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 84 def destroy @resource.destroy! redirect_to collection_url, notice: "#{resource_config.human_name} was successfully deleted.", status: :see_other end |
#edit ⇒ Object
GET /:portal/:resource_name/:id/edit
61 62 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 61 def edit end |
#execute_action ⇒ Object
POST /:portal/:resource_name/:id/execute_action/:action_name
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 90 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. else redirect_to resource_url(@resource), alert: result. end end |
#index ⇒ Object
GET /:portal/:resource_name
24 25 26 27 28 29 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 24 def index query = AdminSuite::Query.new(resource_config: resource_config, params: params) scope = query.scope @stats = calculate_stats(scope) if resource_config&.index_config&.stats_list&.any? @pagy, @collection = pagy(scope, limit: query.per_page) end |
#new ⇒ Object
GET /:portal/:resource_name/new
56 57 58 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 56 def new @resource = resource_class.new end |
#search ⇒ Object
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.
51 52 53 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 51 def search render json: search_results.first(SEARCH_RESULT_LIMIT).map { |record| search_result_json(record) } end |
#show ⇒ Object
GET /:portal/:resource_name/:id
32 33 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 32 def show end |
#toggle ⇒ Object
POST /:portal/:resource_name/:id/toggle
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 157 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 131 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 |
#update ⇒ Object
PATCH/PUT /:portal/:resource_name/:id
75 76 77 78 79 80 81 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 75 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 |