module ActiveAdmin::Resource::ActionItems

Public Class Methods

new(*args) click to toggle source

Adds the default action items to a resource when it’s initialized

Calls superclass method
# File lib/active_admin/resource/action_items.rb, line 9
def initialize(*args)
  super
  add_default_action_items
end

Public Instance Methods

action_items() click to toggle source

@return [Array] The set of action items for this resource

# File lib/active_admin/resource/action_items.rb, line 15
def action_items
  @action_items ||= []
end
action_items?() click to toggle source

Used by active_admin Base view

# File lib/active_admin/resource/action_items.rb, line 50
def action_items?
  !!@action_items && @action_items.any?
end
action_items_for(action, render_context = nil) click to toggle source

Returns a set of action items to display for a specific controller action

@param [String, Symbol] action the action to retrieve action items for

@return [Array] Array of ActionItems for the controller actions

# File lib/active_admin/resource/action_items.rb, line 40
def action_items_for(action, render_context = nil)
  action_items.select{ |item| item.display_on? action, render_context }
end
add_action_item(name, options = {}, &block) click to toggle source

Add a new action item to a resource

@param [Symbol] name @param [Hash] options valid keys include:

:only: A single or array of controller actions to display
        this action item on.
:except: A single or array of controller actions not to
         display this action item on.
# File lib/active_admin/resource/action_items.rb, line 27
def add_action_item(name, options = {}, &block)
  self.action_items << ActiveAdmin::ActionItem.new(name, options, &block)
end
clear_action_items!() click to toggle source

Clears all the existing action items for this resource

# File lib/active_admin/resource/action_items.rb, line 45
def clear_action_items!
  @action_items = []
end
remove_action_item(name) click to toggle source
# File lib/active_admin/resource/action_items.rb, line 31
def remove_action_item(name)
  self.action_items.delete_if { |item| item.name == name }
end

Private Instance Methods

add_default_action_items() click to toggle source

Adds the default action items to each resource

# File lib/active_admin/resource/action_items.rb, line 57
def add_default_action_items
  add_default_new_action_item
  add_default_edit_action_item
  add_default_show_action_item
end
add_default_edit_action_item() click to toggle source

Adds the default Edit link on show

# File lib/active_admin/resource/action_items.rb, line 73
def add_default_edit_action_item
  add_action_item :edit, only: :show do
    if controller.action_methods.include?('edit') && authorized?(ActiveAdmin::Auth::UPDATE, resource)
      link_to I18n.t('active_admin.edit_model', model: active_admin_config.resource_label), edit_resource_path(resource)
    end
  end
end
add_default_new_action_item() click to toggle source

Adds the default New link on index

# File lib/active_admin/resource/action_items.rb, line 64
def add_default_new_action_item
  add_action_item :new, only: :index do
    if controller.action_methods.include?('new') && authorized?(ActiveAdmin::Auth::CREATE, active_admin_config.resource_class)
      link_to I18n.t('active_admin.new_model', model: active_admin_config.resource_label), new_resource_path
    end
  end
end
add_default_show_action_item() click to toggle source

Adds the default Destroy link on show

# File lib/active_admin/resource/action_items.rb, line 82
def add_default_show_action_item
  add_action_item :destroy, only: :show do
    if controller.action_methods.include?('destroy') && authorized?(ActiveAdmin::Auth::DESTROY, resource)
      link_to I18n.t('active_admin.delete_model', model: active_admin_config.resource_label), resource_path(resource),
        method: :delete, data: {confirm: I18n.t('active_admin.delete_confirmation')}
    end
  end
end