Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to disable whitespace stripping for string searches #1214

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,35 @@ def index
end
```

##### Default search parameter
##### Default search options

**Search parameter**

Ransack uses a default `:q` param key for search params. This may be changed by
setting the `search_key` option in a Ransack initializer file (typically
`config/initializers/ransack.rb`):

```
```ruby
Ransack.configure do |c|
# Change default search parameter key name.
# Default key name is :q
c.search_key = :query
end
```

**String search**

After version 2.4.0 when searching a string query Ransack by default strips all whitespace around the query string.
This may be disabled by setting the `strip_whitespace` option in a Ransack initializer file:

```ruby
Ransack.configure do |c|
# Change whitespace stripping behaviour.
# Default is true
c.strip_whitespace = false
end
```

#### In your view

The two primary Ransack view helpers are `search_form_for` and `sort_link`,
Expand Down
16 changes: 15 additions & 1 deletion lib/ransack/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def []=(key, value)
:down_arrow => '▲'.freeze,
:default_arrow => nil,
:sanitize_scope_args => true,
:postgres_fields_sort_option => nil
:postgres_fields_sort_option => nil,
:strip_whitespace => true
}

def configure
Expand Down Expand Up @@ -170,6 +171,19 @@ def hide_sort_order_indicators=(boolean)
self.options[:hide_sort_order_indicators] = boolean
end

# By default, Ransack displays strips all whitespace when searching for a string.
# The default may be globally changed in an initializer file like
# `config/initializers/ransack.rb` as follows:
#
# Ransack.configure do |config|
# # Enable whitespace stripping for string searches
# config.strip_whitespace = true
# end
#
def strip_whitespace=(boolean)
self.options[:strip_whitespace] = boolean
end

def arel_predicate_with_suffix(arel_predicate, suffix)
if arel_predicate === Proc
proc { |v| "#{arel_predicate.call(v)}#{suffix}" }
Expand Down
3 changes: 2 additions & 1 deletion lib/ransack/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ class Search
:translate, :to => :base

def initialize(object, params = {}, options = {})
strip_whitespace = options.fetch(:strip_whitespace, Ransack.options[:strip_whitespace])
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 = params.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 = {}
Expand Down
14 changes: 14 additions & 0 deletions spec/ransack/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ module Ransack
Ransack.options = default
end

it 'should have default value for strip_whitespace' do
expect(Ransack.options[:strip_whitespace]).to eq true
end

it 'changes default search key parameter' do
default = Ransack.options.clone

Ransack.configure { |c| c.strip_whitespace = false }

expect(Ransack.options[:strip_whitespace]).to eq false

Ransack.options = default
end

it 'should have default values for arrows' do
expect(Ransack.options[:up_arrow]).to eq '▼'
expect(Ransack.options[:down_arrow]).to eq '▲'
Expand Down
40 changes: 36 additions & 4 deletions spec/ransack/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,42 @@ 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 ')
context 'whitespace stripping' do
context 'when whitespace_strip option is true' do
before do
Ransack.configure { |c| c.strip_whitespace = true }
end

it 'strips 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
end

context 'when whitespace_strip option is false' do
before do
Ransack.configure { |c| c.strip_whitespace = false }
end

it 'doesn\'t 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
end

it 'strips leading & trailing whitespace when strip_whitespace search parameter is true' do
expect_any_instance_of(Search).to receive(:build)
.with({ 'name_eq' => 'foobar' })
Search.new(Person, { name_eq: ' foobar ' }, { strip_whitespace: true })
end

it 'doesn\'t strip leading & trailing whitespace when strip_whitespace search parameter is false' do
expect_any_instance_of(Search).to receive(:build)
.with({ 'name_eq' => ' foobar ' })
Search.new(Person, { name_eq: ' foobar ' }, { strip_whitespace: false })
end
end

it 'removes empty suffixed conditions before building' do
Expand Down