From eabd135b4404d309cf9e7e5f0b6a422307b9184c Mon Sep 17 00:00:00 2001 From: lichao Date: Fri, 22 May 2020 09:49:16 +0000 Subject: [PATCH] Strip Leading & Trailing Whitespace Before Searching, close #1115 --- lib/ransack/search.rb | 1 + spec/ransack/search_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/lib/ransack/search.rb b/lib/ransack/search.rb index 085cb1337..9b4384443 100644 --- a/lib/ransack/search.rb +++ b/lib/ransack/search.rb @@ -18,6 +18,7 @@ def initialize(object, params = {}, options = {}) params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h) if params.is_a? Hash params = params.dup + params = params.transform_values { |v| v.is_a?(String) ? v.strip : v } params.delete_if { |k, v| [*v].all?{ |i| i.blank? && i != false } } else params = {} diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index be4141643..590df0515 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -20,6 +20,12 @@ module Ransack Search.new(Person, name_eq: 'foobar') end + it 'strip leading & trailing whitespace before building' do + expect_any_instance_of(Search).to receive(:build) + .with({ 'name_eq' => 'foobar' }) + Search.new(Person, name_eq: ' foobar ') + end + it 'removes empty suffixed conditions before building' do expect_any_instance_of(Search).to receive(:build).with({}) Search.new(Person, name_eq_any: [''])