6

我一直在阅读有关自定义操作的 rails_admin wiki 部分,但我对 rails_admin 完全陌生,该文档对我来说有点混乱。

我需要实现的是,当管理员用户单击删除特定员工的选项(员工是我的应用程序中的模型)时,实际删除给定员工的代码不能是 rails_admin 删除的默认方式,而是给定我需要以某种方式提供给 rails_admin 的代码块。

请注意,我仍然希望以通常的方式删除通过 rails_admin 可访问的其余数据模型。只有员工模型需要自定义删除例程。

4

2 回答 2

11

我刚刚遇到了同样的问题,因为我需要将用户标记为删除而不是立即删除。

经过一番折腾,我终于找到了一种方法:覆盖 RailsAdmin 的默认删除操作。

这是我添加自己的自定义代码后的删除操作(不是删除,而是使用自定义通知标记删除,如果用户已被标记为删除,则隐藏删除按钮)。:

# 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

您可以在此处找到原始操作代码:https ://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/actions/delete.rb

于 2013-09-17T09:44:01.117 回答
0

经过数小时的黑客攻击,我找到了一种更简单的自定义动作的方法。您只需要创建一个不同的模块名称(在我的情况下是“MyRailsAdmin”),然后扩展原始操作类,例如。'删除'

# 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
于 2016-03-28T08:26:10.337 回答