Skip to content

Commit

Permalink
feat: Add invalid search term handling
Browse files Browse the repository at this point in the history
  • Loading branch information
goosys committed Jul 26, 2024
1 parent 722291d commit 0276b02
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
35 changes: 34 additions & 1 deletion lib/administrate_ransack/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ module AdministrateRansack
module Searchable
def scoped_resource
options = respond_to?(:ransack_options) ? ransack_options : {}
@ransack_results = super.ransack(params[:q], **options)
begin
@ransack_results = super.ransack(params[:q], **options)
rescue ArgumentError => e
handle_ransack_argument_error(e)
set_flash_message_as_ransack_argument_error(e)
@ransack_results = reset_ransack_result_on_error(super).ransack({}, **options)
end
@ransack_results.result(distinct: true)
end

Expand All @@ -24,5 +30,32 @@ def prepended(base)
base.helper_method :sanitized_order_params
end
end

private

def set_flash_message_as_ransack_argument_error(error)
if error.message.eql?("Invalid sorting parameter provided")
flash.now[:alert] = I18n.t(
:invalid_sorting_parameter_provided,
scope: [:administrate_ransack, :errors],

Check failure on line 40 in lib/administrate_ransack/searchable.rb

View workflow job for this annotation

GitHub Actions / reviewdog

[rubocop] reported by reviewdog 🐶 [Correctable] Rails/DotSeparatedKeys: Use the dot-separated keys instead of specifying the :scope option. Raw Output: lib/administrate_ransack/searchable.rb:40:11: C: [Correctable] Rails/DotSeparatedKeys: Use the dot-separated keys instead of specifying the :scope option. scope: [:administrate_ransack, :errors], ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
default: error.message
)
elsif error.message.start_with?("Invalid search term ")
flash.now[:alert] = I18n.t(
:invalid_search_term,
search_term: error.message.split(' ')[3..].join(' '),

Check failure on line 46 in lib/administrate_ransack/searchable.rb

View workflow job for this annotation

GitHub Actions / reviewdog

[rubocop] reported by reviewdog 🐶 [Correctable] Style/RedundantArgument: Argument ' ' is redundant because it is implied by default. Raw Output: lib/administrate_ransack/searchable.rb:46:43: C: [Correctable] Style/RedundantArgument: Argument ' ' is redundant because it is implied by default. search_term: error.message.split(' ')[3..].join(' '), ^^^^^
scope: [:administrate_ransack, :errors],

Check failure on line 47 in lib/administrate_ransack/searchable.rb

View workflow job for this annotation

GitHub Actions / reviewdog

[rubocop] reported by reviewdog 🐶 [Correctable] Rails/DotSeparatedKeys: Use the dot-separated keys instead of specifying the :scope option. Raw Output: lib/administrate_ransack/searchable.rb:47:11: C: [Correctable] Rails/DotSeparatedKeys: Use the dot-separated keys instead of specifying the :scope option. scope: [:administrate_ransack, :errors], ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
default: error.message
)
end
end

def handle_ransack_argument_error(error)
super if defined?(super)
end

def reset_ransack_result_on_error(super_scoped_resource)
super_scoped_resource.none
end
end
end
39 changes: 36 additions & 3 deletions spec/controllers/admin/posts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,43 @@
end

context "when ransack_options is defined" do
it "raises an exception with an invalid parameter" do
expect {
context "when you want to handle the error manualy" do
controller do
def handle_ransack_argument_error(error)
raise error
end
end

it "raises an exception with an invalid parameter" do
expect {
get :index, params: { "q[title2_cont]" => "test" }
}.to raise_exception(ArgumentError, 'Invalid search term title2_cont')
end
end

context "when you want to reset the ransack result and display a flash message on error" do
it "resets the ransack result and displays a flash message" do

Check failure on line 27 in spec/controllers/admin/posts_controller_spec.rb

View workflow job for this annotation

GitHub Actions / reviewdog

[rubocop] reported by reviewdog 🐶 RSpec/MultipleExpectations: Example has too many expectations [3/1]. Raw Output: spec/controllers/admin/posts_controller_spec.rb:27:9: C: RSpec/MultipleExpectations: Example has too many expectations [3/1]. it "resets the ransack result and displays a flash message" do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
get :index, params: { "q[title2_cont]" => "test" }
}.to raise_exception(ArgumentError, 'Invalid search term title2_cont')

expect(assigns(:ransack_results)).to be_instance_of Ransack::Search
expect(assigns(:ransack_results).result).to be_empty
expect(flash[:alert]).to eq 'Invalid search term title2_cont'
end
end

context "when you want to not reset the ransack result on error" do
controller do
def reset_ransack_result_on_error(super_scoped_resource)
super_scoped_resource
end
end

it "does not reset the ransack result" do

Check failure on line 43 in spec/controllers/admin/posts_controller_spec.rb

View workflow job for this annotation

GitHub Actions / reviewdog

[rubocop] reported by reviewdog 🐶 RSpec/MultipleExpectations: Example has too many expectations [2/1]. Raw Output: spec/controllers/admin/posts_controller_spec.rb:43:9: C: RSpec/MultipleExpectations: Example has too many expectations [2/1]. it "does not reset the ransack result" do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
get :index, params: { "q[title2_cont]" => "test" }

expect(assigns(:ransack_results)).to be_instance_of Ransack::Search
expect(assigns(:ransack_results).result).to_not be_empty

Check failure on line 47 in spec/controllers/admin/posts_controller_spec.rb

View workflow job for this annotation

GitHub Actions / reviewdog

[rubocop] reported by reviewdog 🐶 [Correctable] RSpec/NotToNot: Prefer not_to over to_not. Raw Output: spec/controllers/admin/posts_controller_spec.rb:47:52: C: [Correctable] RSpec/NotToNot: Prefer not_to over to_not. expect(assigns(:ransack_results).result).to_not be_empty ^^^^^^
end
end
end
end
Expand Down

0 comments on commit 0276b02

Please sign in to comment.