diff --git a/docs/9-batch-actions.md b/docs/9-batch-actions.md index e7d6bd0a80d..b756a9033c3 100644 --- a/docs/9-batch-actions.md +++ b/docs/9-batch-actions.md @@ -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: diff --git a/features/index/batch_actions.feature b/features/index/batch_actions.feature index c31254ee844..a2f504b009c 100644 --- a/features/index/batch_actions.feature +++ b/features/index/batch_actions.feature @@ -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" @@ -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" @@ -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" diff --git a/lib/active_admin/application.rb b/lib/active_admin/application.rb index 80a32ae555c..a46997ca029 100644 --- a/lib/active_admin/application.rb +++ b/lib/active_admin/application.rb @@ -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, diff --git a/lib/active_admin/batch_actions/resource_extension.rb b/lib/active_admin/batch_actions/resource_extension.rb index c799b263348..c2cc2999a46 100644 --- a/lib/active_admin/batch_actions/resource_extension.rb +++ b/lib/active_admin/batch_actions/resource_extension.rb @@ -2,7 +2,6 @@ module ActiveAdmin module BatchActions module ResourceExtension - def initialize(*) super @batch_actions = {} @@ -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 diff --git a/lib/generators/active_admin/install/templates/active_admin.rb.erb b/lib/generators/active_admin/install/templates/active_admin.rb.erb index 075b7a50b96..45906edb000 100644 --- a/lib/generators/active_admin/install/templates/active_admin.rb.erb +++ b/lib/generators/active_admin/install/templates/active_admin.rb.erb @@ -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 diff --git a/spec/unit/batch_actions/settings_spec.rb b/spec/unit/batch_actions/settings_spec.rb new file mode 100644 index 00000000000..fcc992b16c3 --- /dev/null +++ b/spec/unit/batch_actions/settings_spec.rb @@ -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