Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise Ransack::InvalidSearchError instead of ArgumentError on unknown conditions #1543

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased
* Rails 8 compatibility
* Drop Rails 6 and 7.0 compatibility
* Raise Ransack::InvalidSearchError instead of ArgumentError on unknown conditions

## 4.2.1 - 2024-8-11

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/going-further/other-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ end

```ruby
Article.ransack(unknown_attr_eq: 'Ernie')
# ArgumentError (Invalid search term unknown_attr_eq)
# Ransack::InvalidSearchError (Invalid search term unknown_attr_eq)
```

As an alternative to setting a global configuration option, the `.ransack!`
class method also raises an error if passed an unknown condition:

```ruby
Article.ransack!(unknown_attr_eq: 'Ernie')
# ArgumentError: Invalid search term unknown_attr_eq
# Ransack::InvalidSearchError: Invalid search term unknown_attr_eq
```

This is equivalent to the `ignore_unknown_conditions` configuration option,
Expand Down
3 changes: 3 additions & 0 deletions lib/ransack/invalid_search_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Ransack
class InvalidSearchError < ArgumentError; end
end
4 changes: 3 additions & 1 deletion lib/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'ransack/invalid_search_error'

module Ransack
module Nodes
class Condition < Node
Expand Down Expand Up @@ -38,7 +40,7 @@ def extract_values_for_condition(key, context = nil)
predicate = Predicate.named(name)

unless predicate || Ransack.options[:ignore_unknown_conditions]
raise ArgumentError, "No valid predicate for #{key}"
raise InvalidSearchError, "No valid predicate for #{key}"
end

if context.present?
Expand Down
5 changes: 3 additions & 2 deletions lib/ransack/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'ransack/nodes/grouping'
require 'ransack/context'
require 'ransack/naming'
require 'ransack/invalid_search_error'

module Ransack
class Search
Expand Down Expand Up @@ -53,7 +54,7 @@ def build(params)
elsif base.attribute_method?(key)
base.send("#{key}=", value)
elsif !Ransack.options[:ignore_unknown_conditions] || !@ignore_unknown_conditions
raise ArgumentError, "Invalid search term #{key}"
raise InvalidSearchError, "Invalid search term #{key}"
end
end
self
Expand All @@ -78,7 +79,7 @@ def sorts=(args)
when String
self.sorts = [args]
else
raise ArgumentError,
raise InvalidSearchError,
"Invalid argument (#{args.class}) supplied to sorts="
end
end
Expand Down
6 changes: 5 additions & 1 deletion spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,14 @@ module ActiveRecord
expect { Person.ransack('') }.to_not raise_error
end

it 'raises exception if ransack! called with unknown condition' do
it 'raises ArgumentError exception if ransack! called with unknown condition' do
expect { Person.ransack!(unknown_attr_eq: 'Ernie') }.to raise_error(ArgumentError)
end

it 'raises InvalidSearchError exception if ransack! called with unknown condition' do
expect { Person.ransack!(unknown_attr_eq: 'Ernie') }.to raise_error(InvalidSearchError)
end

it 'does not modify the parameters' do
params = { name_eq: '' }
expect { Person.ransack(params) }.not_to change { params }
Expand Down
1 change: 1 addition & 0 deletions spec/ransack/nodes/condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ module Nodes
end

specify { expect { subject }.to raise_error ArgumentError }
specify { expect { subject }.to raise_error InvalidSearchError }
end

context "when ignore_unknown_conditions is true" do
Expand Down
14 changes: 14 additions & 0 deletions spec/ransack/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ module Ransack
end

specify { expect { subject }.to raise_error ArgumentError }
specify { expect { subject }.to raise_error InvalidSearchError }
end

context 'when ignore_unknown_conditions configuration option is true' do
Expand Down Expand Up @@ -300,6 +301,7 @@ module Ransack

context 'when ignore_unknown_conditions search parameter is false' do
specify { expect { with_ignore_unknown_conditions_false }.to raise_error ArgumentError }
specify { expect { with_ignore_unknown_conditions_false }.to raise_error InvalidSearchError }
end

context 'when ignore_unknown_conditions search parameter is true' do
Expand Down Expand Up @@ -614,6 +616,18 @@ def remove_quotes_and_backticks(str)
expect(@s.result.first.id).to eq 1
end

it 'raises ArgumentError when an invalid argument is sent' do
expect do
@s.sorts = 1234
end.to raise_error(ArgumentError, "Invalid argument (Integer) supplied to sorts=")
end

it 'raises InvalidSearchError when an invalid argument is sent' do
expect do
@s.sorts = 1234
end.to raise_error(Ransack::InvalidSearchError, "Invalid argument (Integer) supplied to sorts=")
end

it "PG's sort option", if: ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL" do
default = Ransack.options.clone

Expand Down
Loading