module ActiveAdmin::Resource::ActionItems

def action_items

Returns:
  • (Array) - The set of action items for this resource
def action_items
  @action_items ||= []
end

def action_items?

Used by active_admin Base view
def action_items?
  !!@action_items && @action_items.any?
end

def action_items_for(action, render_context = nil)

Returns:
  • (Array) - Array of ActionItems for the controller actions

Parameters:
  • action (String, Symbol) -- the action to retrieve action items for
def action_items_for(action, render_context = nil)
  action_items.select { |item| item.display_on? action, render_context }.sort_by(&:priority)
end

def add_action_item(name, options = {}, &block)

Parameters:
  • options (Hash) -- valid keys include:
  • name (Symbol) --
def add_action_item(name, options = {}, &block)
  self.action_items << ActiveAdmin::ActionItem.new(name, options, &block)
end

def add_default_action_items

Adds the default action items to each resource
def add_default_action_items
  add_default_new_action_item
  add_default_edit_action_item
  add_default_show_action_item
end

def add_default_edit_action_item

Adds the default Edit link on show
def add_default_edit_action_item
  add_action_item :edit, only: :show do
    if controller.action_methods.include?("edit") && authorized?(ActiveAdmin::Auth::EDIT, resource)
      localizer = ActiveAdmin::Localizers.resource(active_admin_config)
      link_to localizer.t(:edit_model), edit_resource_path(resource)
    end
  end
end

def add_default_new_action_item

Adds the default New link on index
def add_default_new_action_item
  add_action_item :new, only: :index do
    if controller.action_methods.include?("new") && authorized?(ActiveAdmin::Auth::NEW, active_admin_config.resource_class)
      localizer = ActiveAdmin::Localizers.resource(active_admin_config)
      link_to localizer.t(:new_model), new_resource_path
    end
  end
end

def add_default_show_action_item

Adds the default Destroy link on show
def add_default_show_action_item
  add_action_item :destroy, only: :show do
    if controller.action_methods.include?("destroy") && authorized?(ActiveAdmin::Auth::DESTROY, resource)
      localizer = ActiveAdmin::Localizers.resource(active_admin_config)
      link_to localizer.t(:delete_model), resource_path(resource), method: :delete,
                                                                   data: { confirm: localizer.t(:delete_confirmation) }
    end
  end
end

def clear_action_items!

Clears all the existing action items for this resource
def clear_action_items!
  @action_items = []
end

def initialize(*args)

Adds the default action items to a resource when it's initialized
def initialize(*args)
  super
  add_default_action_items
end

def remove_action_item(name)

def remove_action_item(name)
  self.action_items.delete_if { |item| item.name == name }
end