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 008c2245f..a824f1373 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: [''])