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 10
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 16
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 52
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 42
def action_items_for(action, render_context = nil)
  action_items.select { |item| item.display_on? action, render_context }.sort_by(&:priority)
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.
:priority: A single integer value. To control the display order. Default is 10.
# File lib/active_admin/resource/action_items.rb, line 29
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 47
def clear_action_items!
  @action_items = []
end
remove_action_item(name) click to toggle source
# File lib/active_admin/resource/action_items.rb, line 33
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 59
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 76
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
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 66
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
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 86
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