diff --git a/lib/ransack/search.rb b/lib/ransack/search.rb index a8bf95f8..eb7405a4 100644 --- a/lib/ransack/search.rb +++ b/lib/ransack/search.rb @@ -24,7 +24,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) && strip_whitespace ? v.strip : v } + params = params.deep_transform_values { |v| v.is_a?(String) && strip_whitespace ? 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 6426a20a..61b853ae 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -31,6 +31,14 @@ module Ransack .with({ 'name_eq' => 'foobar' }) Search.new(Person, name_eq: ' foobar ') end + + describe 'nested search params with group conditions' do + it 'strips leading & trailing whitespace in all nested values before building' do + expect_any_instance_of(Search).to receive(:build) + .with({ 'g' => [{ 'm' => 'or', 'name_eq' => 'foobar' }] }) + Search.new(Person, { g: [{ m: 'or', name_eq: ' foobar ' }] }) + end + end end context 'when whitespace_strip option is false' do