Skip to content

Commit

Permalink
raise InvalidSearchError in condition.rb and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sherifnada committed Jan 27, 2025
1 parent 8a5b2d3 commit 8c9b8e6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
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
4 changes: 2 additions & 2 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ module ActiveRecord
expect { Person.ransack('') }.to_not raise_error
end

it 'raises exception if ransack! called with unknown condition' do
expect { Person.ransack!(unknown_attr_eq: 'Ernie') }.to raise_error(ArgumentError)
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
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

0 comments on commit 8c9b8e6

Please sign in to comment.