Class: ActiveAdmin::BatchAction

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/active_admin/batch_actions/resource_extension.rb

Constant Summary

DEFAULT_CONFIRM_MESSAGE =
proc{ I18n.t 'active_admin.batch_actions.default_confirmation' }

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (BatchAction) initialize(sym, title, options = {}, &block)

Create a Batch Action

Examples:

BatchAction.new :flag

> Will create an action that appears in the action list popover

BatchAction.new(:flag) { |selection| redirect_to collection_path, notice: "#{selection.length} users flagged" }

> Will create an action that uses a block to process the request (which receives one paramater of the selected objects)

BatchAction.new("Perform Long Operation on") { |selection| }

> You can create batch actions with a title instead of a Symbol

BatchAction.new(:flag, if: proc{ can? :flag, AdminUser }) { |selection| }

> You can provide an `:if` proc to choose when the batch action should be displayed

BatchAction.new :flag, confirm: true

> You can pass `true` to `:confirm` to use the default confirm message.

BatchAction.new(:flag, confirm: "Are you sure?") { |selection| }

> You can pass a custom confirmation message through `:confirm`

BatchAction.new(:flag, form: {foo: :text, bar: :checkbox}) { |selection, inputs| }

> You can pass a hash of options to `:form` that will be rendered as form input fields for the user to fill out.



116
117
118
119
# File 'lib/active_admin/batch_actions/resource_extension.rb', line 116

def initialize(sym, title, options = {}, &block)
  @sym, @title, @options, @block, @confirm = sym, title, options, block, options[:confirm]
  @block ||= proc {}
end

Instance Attribute Details

- (Object) block (readonly)

Returns the value of attribute block



87
88
89
# File 'lib/active_admin/batch_actions/resource_extension.rb', line 87

def block
  @block
end

- (Object) sym (readonly)

Returns the value of attribute sym



87
88
89
# File 'lib/active_admin/batch_actions/resource_extension.rb', line 87

def sym
  @sym
end

- (Object) title (readonly)

Returns the value of attribute title



87
88
89
# File 'lib/active_admin/batch_actions/resource_extension.rb', line 87

def title
  @title
end

Instance Method Details

- (Object) <=>(other)

sort operator



148
149
150
# File 'lib/active_admin/batch_actions/resource_extension.rb', line 148

def <=>(other)
  self.priority <=> other.priority
end

- (Object) confirm



121
122
123
124
125
126
127
128
129
# File 'lib/active_admin/batch_actions/resource_extension.rb', line 121

def confirm
  if @confirm == true
    DEFAULT_CONFIRM_MESSAGE
  elsif !@confirm && @options[:form]
    DEFAULT_CONFIRM_MESSAGE
  else
    @confirm
  end
end

- (Object) display_if_block

Returns the display if block. If the block was not explicitly defined a default block always returning true will be returned.



138
139
140
# File 'lib/active_admin/batch_actions/resource_extension.rb', line 138

def display_if_block
  @options[:if] || proc{ true }
end

- (Object) inputs



131
132
133
134
# File 'lib/active_admin/batch_actions/resource_extension.rb', line 131

def inputs
  HashWithIndifferentAccess.new \
    @options[:form].is_a?(Proc) ? @options[:form].call : @options[:form]
end

- (Object) priority

Used for sorting



143
144
145
# File 'lib/active_admin/batch_actions/resource_extension.rb', line 143

def priority
  @options[:priority] || 10
end