Skip to content

Commit

Permalink
Update filters disabled error to include specific action (#6195)
Browse files Browse the repository at this point in the history
* Change filters disabled message to "add or remove"

ActiveAdmin::Filters::Disabled is raised both when attempting add a filter and
when attempting to remove a filter when filters are disabled. Change the
message to suit both use cases.

* Update error to specify action that failed

---------

Co-authored-by: Javier Julio <jjfutbol@gmail.com>
  • Loading branch information
javawizard and javierjulio committed May 11, 2023
1 parent fc6fd8b commit cecfcf5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/active_admin/filters/resource_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module ActiveAdmin
module Filters

class Disabled < RuntimeError
def initialize
super "Can't remove a filter when filters are disabled. Enable filters with 'config.filters = true'"
def initialize(action)
super "Cannot #{action} a filter when filters are disabled. Enable filters with 'config.filters = true'"
end
end

Expand Down Expand Up @@ -61,7 +61,7 @@ def preserve_default_filters?
#
# @param [Symbol] attributes The attributes to not filter on
def remove_filter(*attributes)
raise Disabled unless filters_enabled?
raise Disabled, "remove" unless filters_enabled?

attributes.each { |attribute| (@filters_to_remove ||= []) << attribute.to_sym }
end
Expand All @@ -73,7 +73,7 @@ def remove_filter(*attributes)
# @param [Hash] options The set of options that are passed through to
# ransack for the field definition.
def add_filter(attribute, options = {})
raise Disabled unless filters_enabled?
raise Disabled, "add" unless filters_enabled?

(@filters ||= {})[attribute.to_sym] = options
end
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/filters/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

it "should raise an exception when filters are disabled" do
resource.filters = false
expect { resource.remove_filter :author }.to raise_error ActiveAdmin::Filters::Disabled
expect { resource.remove_filter :author }.to raise_error(ActiveAdmin::Filters::Disabled, /Cannot remove a filter/)
end
end

Expand Down Expand Up @@ -116,7 +116,7 @@

it "should raise an exception when filters are disabled" do
resource.filters = false
expect { resource.add_filter :title }.to raise_error ActiveAdmin::Filters::Disabled
expect { resource.add_filter :title }.to raise_error(ActiveAdmin::Filters::Disabled, /Cannot add a filter/)
end
end

Expand Down

0 comments on commit cecfcf5

Please sign in to comment.