From 0276b0243359378a30f1e65496f2d041ddeb41c3 Mon Sep 17 00:00:00 2001 From: nakamura Date: Fri, 26 Jul 2024 07:02:25 +0000 Subject: [PATCH] feat: Add invalid search term handling --- lib/administrate_ransack/searchable.rb | 35 ++++++++++++++++- .../admin/posts_controller_spec.rb | 39 +++++++++++++++++-- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/lib/administrate_ransack/searchable.rb b/lib/administrate_ransack/searchable.rb index 17d89ef..7a5e43b 100644 --- a/lib/administrate_ransack/searchable.rb +++ b/lib/administrate_ransack/searchable.rb @@ -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 @@ -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], + 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(' '), + 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 diff --git a/spec/controllers/admin/posts_controller_spec.rb b/spec/controllers/admin/posts_controller_spec.rb index 5d5a493..acc71b5 100644 --- a/spec/controllers/admin/posts_controller_spec.rb +++ b/spec/controllers/admin/posts_controller_spec.rb @@ -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 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 + 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 + end end end end