Skip to content

Commit

Permalink
Merge pull request #1126 from itsalongstory/fix_1115
Browse files Browse the repository at this point in the history
Strip Leading & Trailing Whitespace Before Searching
  • Loading branch information
scarroll32 authored Nov 27, 2020
2 parents 216d909 + 05750ce commit dc3eb66
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/ransack/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
6 changes: 6 additions & 0 deletions spec/ransack/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: [''])
Expand Down

0 comments on commit dc3eb66

Please sign in to comment.