module ActiveAdmin::BatchActions::ResourceExtension

Public Class Methods

new(*) click to toggle source
Calls superclass method
# File lib/active_admin/batch_actions/resource_extension.rb, line 5
def initialize(*)
  super
  @batch_actions = {}
  add_default_batch_action
end

Public Instance Methods

add_batch_action(sym, title, options = {}, &block) click to toggle source

Add a new batch item to a resource @param [String] title @param [Hash] options

> :if is a proc that will be called to determine if the BatchAction should be displayed

> :sort_order is used to sort the batch actions ascending

> :confirm is a string to prompt the user with (or a boolean to use the default message)

> :form is a Hash of form fields you want the user to fill out

# File lib/active_admin/batch_actions/resource_extension.rb, line 37
def add_batch_action(sym, title, options = {}, &block)
  @batch_actions[sym] = ActiveAdmin::BatchAction.new(sym, title, options, &block)
end
batch_action_path(params = {}) click to toggle source

Path to the batch action itself

# File lib/active_admin/batch_actions/resource_extension.rb, line 55
def batch_action_path(params = {})
  path = [route_collection_path(params), "batch_action"].join("/")
  query = params.slice(:q, :scope)
  query = query.permit!.to_h if query.respond_to? :permit!
  [path, query.to_param].reject(&:blank?).join("?")
end
batch_actions() click to toggle source

@return [Array] The set of batch actions for this resource

# File lib/active_admin/batch_actions/resource_extension.rb, line 12
def batch_actions
  batch_actions_enabled? ? @batch_actions.values.sort : []
end
batch_actions=(bool) click to toggle source

Disable or Enable batch actions for this resource Set to ‘nil` to inherit the setting from the namespace

# File lib/active_admin/batch_actions/resource_extension.rb, line 25
def batch_actions=(bool)
  @batch_actions_enabled = bool
end
batch_actions_enabled?() click to toggle source

@return [Boolean] If batch actions are enabled for this resource

# File lib/active_admin/batch_actions/resource_extension.rb, line 17
def batch_actions_enabled?
  # If the resource config has been set, use it. Otherwise
  # return the namespace setting
  @batch_actions_enabled.nil? ? namespace.batch_actions : @batch_actions_enabled
end
clear_batch_actions!() click to toggle source

Clears all the existing batch actions for this resource

# File lib/active_admin/batch_actions/resource_extension.rb, line 50
def clear_batch_actions!
  @batch_actions = {}
end
remove_batch_action(sym) click to toggle source

Remove a batch action @param [Symbol] sym @return [ActiveAdmin::BatchAction] the batch action, if it was present

# File lib/active_admin/batch_actions/resource_extension.rb, line 45
def remove_batch_action(sym)
  @batch_actions.delete(sym.to_sym)
end

Private Instance Methods

add_default_batch_action() click to toggle source

@return [ActiveAdmin::BatchAction] The default “delete” action

# File lib/active_admin/batch_actions/resource_extension.rb, line 65
def add_default_batch_action
  destroy_options = {
    priority: 100,
    confirm: proc{ I18n.t('active_admin.batch_actions.delete_confirmation', plural_model: active_admin_config.plural_resource_label.downcase) },
    if: proc{ controller.action_methods.include?('destroy') && authorized?(ActiveAdmin::Auth::DESTROY, active_admin_config.resource_class) }
  }

  add_batch_action :destroy, proc { I18n.t('active_admin.delete') }, destroy_options do |selected_ids|
    batch_action_collection.find(selected_ids).each do |record|
      authorize! ActiveAdmin::Auth::DESTROY, record
      destroy_resource(record)
    end

    redirect_to active_admin_config.route_collection_path(params),
                notice: I18n.t("active_admin.batch_actions.succesfully_destroyed",
                                  count: selected_ids.count,
                                  model: active_admin_config.resource_label.downcase,
                                  plural_model: active_admin_config.plural_resource_label(count: selected_ids.count).downcase)
  end
end