customize rails_admin delete action for a specific model
Asked Answered
I

2

6

I've been reading the rails_admin wiki section about customizing actions but I am totally new to rails_admin and this documentation is a bit confusing for me.

What I need to achieve is that, when an admin user clicks the options to delete a specific Employee (Employee is a model in my app), the code to actually delete the given employee cannot be the default way in which rails_admin deletes, but a given block of code that I need to provide to rails_admin somehow.

Note that I still want the rest of the data models accessible through rails_admin to be deleted in the usual fashion. Only the employee model needs to have the custom delete routine.

Imprinting answered 17/7, 2012 at 15:7 Comment(0)
D
12

I just encountered the same problem because I need users to be marked for deletion and not deleted right away.

After hacking around a bit I've finally found a way : overriding RailsAdmin's default delete action.

Here's the delete action after I added my own custom code (not deleting but marking for deletion with a custom notice, hiding the delete button if the user is already marked for deletion). :

# config/initializers/rails_admin_delete_override.rb
module RailsAdmin
  module Config
    module Actions
      class Delete < RailsAdmin::Config::Actions::Base

        RailsAdmin::Config::Actions.register(self)

        register_instance_option :member do
          true
        end

        register_instance_option :route_fragment do
          'delete'
        end

        register_instance_option :http_methods do
          [:get, :delete]
        end

        register_instance_option :authorization_key do
          :destroy
        end

        register_instance_option :visible? do
          bindings[:object].class.base_class.name != 'User' || !bindings[:object].to_destroy?
        end

        register_instance_option :controller do
          Proc.new do
            if request.get? # DELETE

              respond_to do |format|
                format.html { render @action.template_name }
                format.js   { render @action.template_name, :layout => false }
              end

            elsif request.delete? # DESTROY

              redirect_path = nil
              @auditing_adapter && @auditing_adapter.delete_object(@object, @abstract_model, _current_user)
              if @object.class.base_class.name == 'User'
                @object.to_destroy!
                flash[:success] = t("admin.flash.user_destroy_successful", :name => @model_config.label)
                redirect_path = index_path
              else
                if @object.destroy
                  flash[:success] = t("admin.flash.successful", :name => @model_config.label, :action => t("admin.actions.delete.done"))
                  redirect_path = index_path
                else
                  flash[:error] = t("admin.flash.error", :name => @model_config.label, :action => t("admin.actions.delete.done"))
                  redirect_path = back_or_index
                end
              end

              redirect_to redirect_path

            end
          end
        end

        register_instance_option :link_icon do
          'icon-remove'
        end
      end
    end
  end
end

You can find the original action code here : https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/actions/delete.rb

Dneprodzerzhinsk answered 17/9, 2013 at 9:44 Comment(1)
I need to do something similar and was hoping to find a simpler solution (ala Typus where you can extend individual controllers and override individual actions (or add your own)) but this will do. Thanks for sharing your solution!Leavy
D
0

After hours of hacking, I have found a simpler way of customizing an action. All you need is to create a different module name (In my case is 'MyRailsAdmin'), then extends the original action class, eg. 'Delete'

# config/initializers/rails_admin_delete.rb

require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'

module MyRailsAdmin
    module Config
        module Actions
          class Delete < RailsAdmin::Config::Actions::Delete
            RailsAdmin::Config::Actions::register(self)

            register_instance_option :controller do
                proc do
                    if request.get? # DELETE

                        respond_to do |format|
                            format.html { render @action.template_name }
                            format.js   { render @action.template_name, layout: false }
                        end

                    elsif request.delete? # DESTROY

                        redirect_path = nil
                        @auditing_adapter && @auditing_adapter.delete_object(@object, @abstract_model, _current_user)

                        # [CUSTOM DELETE] mark as deleted (status = 0) rather than actually hard delete it
                        @object.status = 0

                        if @object.save
                            flash[:success] = t('admin.flash.successful', name: @model_config.label, action: t('admin.actions.delete.done'))
                            redirect_path = index_path
                        else
                            flash[:error] = t('admin.flash.error', name: @model_config.label, action: t('admin.actions.delete.done'))
                            redirect_path = back_or_index
                        end

                        redirect_to redirect_path

                    end
                end
            end

        end
    end
end
end
Durant answered 28/3, 2016 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.