Skip to content

Commit

Permalink
Added config to enable and disable Batch Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
gregbell authored and pcreux committed May 15, 2012
1 parent f83d1aa commit dfb5bb7
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 15 deletions.
24 changes: 24 additions & 0 deletions docs/9-batch-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ to operate on. The array should contain at least one ID.
end
end

### Disabling Batch Actions

You can disable batch actions at the application or namespace level in
`config/initializers/active_admin.rb`:

ActiveAdmin.setup do |config|

# Disable all batch actions
config.batch_actions = false


# Or disable for a given namespace
config.namespace :admin do |admin|
admin.batch_actions = false
end
end

You can disable batch actions on any given resource using:

ActiveAdmin.register Post do
config.batch_actions = false
end


### Modifying a Previously Registered Batch Action

If you wanted to modify the behavior of the provided "Delete" batch action, you can override by:
Expand Down
38 changes: 25 additions & 13 deletions features/index/batch_actions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,45 @@ Feature: Batch Actions
And an index configuration of:
"""
ActiveAdmin.register Post do
batch_action( :flag ) do
redirect_to collection_path, :notice => "Successfully flagged 10 posts"
end
batch_action(:flag) do
redirect_to collection_path, :notice => "Successfully flagged 10 posts"
end
end
"""
When I check the 1st record
Given I submit the batch action form with "flag"
Then I should see a flash with "Successfully flagged 10 posts"

Scenario: Disabling batch actions
Scenario: Disabling batch actions for a resource
Given 10 posts exist
And an index configuration of:
"""
ActiveAdmin.register Post do
batch_action :destroy, false
config.batch_actions = false
end
"""
Then I should not see the batch actions selector
And I should not see checkboxes in the table

Scenario: Disabling the default destroy batch action
Given 10 posts exist
And an index configuration of:
"""
ActiveAdmin.register Post do
batch_action :destroy, false
batch_action(:flag) {}
end
"""
Then I should see the batch action "Flag Selected"
And I should not see the batch action "Delete Selected"

Scenario: Optional display of batch actions
Given 10 posts exist
And an index configuration of:
"""
ActiveAdmin.register Post do
batch_action( :flag, :if => proc { true } ) {}
batch_action( :unflag, :if => proc { false } ) {}
batch_action(:flag, :if => proc { true }) {}
batch_action(:unflag, :if => proc { false }) {}
end
"""
Then I should see the batch action "Flag Selected"
Expand All @@ -63,9 +75,9 @@ Feature: Batch Actions
And an index configuration of:
"""
ActiveAdmin.register Post do
batch_action( :test, :priority => 3 ) {}
batch_action( :flag, :priority => 2 ) {}
batch_action( :unflag, :priority => 1 ) {}
batch_action(:test, :priority => 3) {}
batch_action(:flag, :priority => 2) {}
batch_action(:unflag, :priority => 1) {}
end
"""
Then the 4th batch action should be "Delete Selected"
Expand All @@ -78,8 +90,8 @@ Feature: Batch Actions
And an index configuration of:
"""
ActiveAdmin.register Post do
batch_action( "Very Complex and Time Consuming" ) {}
batch_action( :passing_a_symbol ) {}
batch_action("Very Complex and Time Consuming") {}
batch_action(:passing_a_symbol) {}
end
"""
Then I should see the batch action :very_complex_and_time_consuming "Very Complex and Time Consuming Selected"
Expand Down
3 changes: 3 additions & 0 deletions lib/active_admin/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def self.inheritable_setting(name, default)
# The method to use when generating the link for user logout
inheritable_setting :logout_link_method, :get

# Whether the batch actions are enabled or not
inheritable_setting :batch_actions, true

# Active Admin makes educated guesses when displaying objects, this is
# the list of methods it tries calling in order
setting :display_name_methods, [ :display_name,
Expand Down
16 changes: 14 additions & 2 deletions lib/active_admin/batch_actions/resource_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module ActiveAdmin

module BatchActions
module ResourceExtension

def initialize(*)
super
@batch_actions = {}
Expand All @@ -11,7 +10,20 @@ def initialize(*)

# @return [Array] The set of batch actions for this resource
def batch_actions
@batch_actions.values.sort
batch_actions_enabled? ? @batch_actions.values.sort : []
end

# @return [Boolean] If batch actions are enabled for this resource
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

# Disable or Enable batch actions for this resource
# Set to `nil` to inherit the setting from the namespace
def batch_actions=(bool)
@batch_actions_enabled = bool
end

# Add a new batch item to a resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ ActiveAdmin.setup do |config|
# end


# == Batch Actions
#
# Enable and disable Batch Actions
#
# Default:
# config.batch_actions = true


# == Controller Filters
#
# You can add before, after and around filters to all of your
Expand Down
57 changes: 57 additions & 0 deletions spec/unit/batch_actions/settings_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'spec_helper'

describe "Batch Actions Settings" do
let(:app) { ActiveAdmin::Application.new }
let(:ns) { ActiveAdmin::Namespace.new(app, "Admin") }
let(:post_resource) { ns.register Post }

it "should be enabled globally by default" do
app.batch_actions.should be_true
ns.batch_actions.should be_true
post_resource.batch_actions.should be_true
end

it "should be settable to false" do
app.batch_actions = false
app.batch_actions.should == false
end

it "should be an inheritable_setting" do
app.batch_actions = false
ns.batch_actions.should == false
end

it "should be settable at the namespace level" do
app.batch_actions = false
ns.batch_actions = true

app.batch_actions.should == false
ns.batch_actions.should == true
end

it "should be settable at the resource level" do
post_resource.batch_actions_enabled?.should == true
post_resource.batch_actions = false
post_resource.batch_actions_enabled?.should == false
end

it "should inherit the setting on the resource from the namespace" do
ns.batch_actions = false
post_resource.batch_actions_enabled?.should == false
post_resource.batch_actions.should be_empty

post_resource.batch_actions = true
post_resource.batch_actions_enabled?.should == true
post_resource.batch_actions.should_not be_empty
end

it "should inherit the setting from the namespace when set to nil" do
post_resource.batch_actions = false
post_resource.batch_actions_enabled?.should == false
post_resource.batch_actions.should be_empty

post_resource.batch_actions = nil
post_resource.batch_actions_enabled?.should == true # inherited from namespace
post_resource.batch_actions.should_not be_empty
end
end

0 comments on commit dfb5bb7

Please sign in to comment.