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.
-
#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
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 76 def bulk_action action = params[:action_name].to_s.to_sym 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
32 33 34 35 36 37 38 39 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 32 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
51 52 53 54 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 51 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
28 29 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 28 def edit end |
#execute_action ⇒ Object
POST /:portal/:resource_name/:id/execute_action/:action_name
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 57 def execute_action action = params[:action_name].to_s.to_sym action_def = find_action(action) unless action_def redirect_to resource_url(@resource), alert: "Action not found." return end executor = Admin::Base::ActionExecutor.new(resource_config, action, admin_suite_actor) result = executor.execute_member(@resource, params.to_unsafe_h) if result.success? redirect_to resource_url(@resource), notice: result. else redirect_to resource_url(@resource), alert: result. end end |
#index ⇒ Object
GET /:portal/:resource_name
13 14 15 16 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 13 def index @stats = calculate_stats if resource_config&.index_config&.stats_list&.any? @pagy, @collection = paginate_collection(filtered_collection) end |
#new ⇒ Object
GET /:portal/:resource_name/new
23 24 25 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 23 def new @resource = resource_class.new end |
#show ⇒ Object
GET /:portal/:resource_name/:id
19 20 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 19 def show end |
#toggle ⇒ Object
POST /:portal/:resource_name/:id/toggle
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 97 def toggle field = params[:field].presence&.to_sym 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, :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
42 43 44 45 46 47 48 |
# File 'app/controllers/admin_suite/resources_controller.rb', line 42 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 |