ActiveAdmin 组件的重用

Reuse of ActiveAdmin components

我有很多模型,其中很多需要相同的设置(操作按钮、成员操作、面板等)。我怎样才能在许多地方重用代码并保持代码干燥而不是 copy/pasting 周围都是相同的代码块。

例如我需要在每个资源上使用此代码:

member_action :purge_cache, method: :post do
    CacheManager.new(resource).purge_all
end

在 ActiveAdmin::Resource、check how they are doing it

中创建模块

我找到了另一种方法:

创建/app/admin/concerns/shared_stuff.rb:

module SharedStuff
  def self.extended(base)
    base.instance_eval do
      member_action :purge_cache, method: :post do
        CacheManager.new(resource).purge_all
      end
    end
  end
end

只要你想要相同的动作,你可以使用:

ActiveAdmin.register Website do
  extend SharedStuff

  ...

您可以使用这种方式轻松添加面板、过滤器、批处理操作等...

致谢名单:http://tmichel.github.io/2015/02/22/sharing-code-between-activeadmin-resources/