port5432
port5432

Reputation: 6381

Override actions in ActiveAdmin

ActiveAdmin allows for the actions method, which adds view, edit and delete to the index view.

I am overriding this on each index view with something like this:

  actions defaults: false do |word|
      item "View", admin_word_path(word), class: 'edit_link member_link'
  end

This works fine, but I'd like to just have a global override for all index views which doesn't require me writing this code for each.

How would I do this?

Upvotes: 1

Views: 1144

Answers (1)

Sjors Branderhorst
Sjors Branderhorst

Reputation: 2182

A way is to dynamically open up the IndexTableFor class within module ActiveAdmin. Risky, because this might eventually break with ActiveAdmin upgrades. paste the following snippet at the end of your config/initializers/active_admin.rb file. I tested this with activeadmin 1.1, rails 5.1 and ruby 2.4.2 and it works.

module ActiveAdmin
  module Views
    class IndexAsTable < ActiveAdmin::Component
      class IndexTableFor < ::ActiveAdmin::Views::TableFor
        def defaults(resource, options = {})
          if controller.action_methods.include?('show') && authorized?(ActiveAdmin::Auth::READ, resource)
            item I18n.t('active_admin.view'), resource_path(resource), class: "view_link #{options[:css_class]}", title: I18n.t('active_admin.view')
          end
          if controller.action_methods.include?('edit') && authorized?(ActiveAdmin::Auth::UPDATE, resource)
            item I18n.t('active_admin.edit'), edit_resource_path(resource), class: "edit_link #{options[:css_class]}", title: I18n.t('active_admin.edit')
          end
          if controller.action_methods.include?('destroy') && authorized?(ActiveAdmin::Auth::DESTROY, resource)
            item I18n.t('active_admin.delete'), resource_path(resource), class: "delete_link #{options[:css_class]}", title: I18n.t('active_admin.delete'),
              method: :delete, data: {confirm:    I18n.t('active_admin.delete_confirmation')}
          end
        end
      end
    end

You can now tweak this the way you would like. Remember to restart your rails server as it is an initializer.

Good luck!

Upvotes: 3

Related Questions