Skip to content
Draft
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
2 changes: 1 addition & 1 deletion lib/ransack/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Ransack
class Context
attr_reader :search, :object, :klass, :base, :engine, :arel_visitor
attr_accessor :auth_object, :search_key
attr_accessor :auth_object, :search_key, :strip_whitespace
attr_reader :arel_visitor

class << self
Expand Down
14 changes: 14 additions & 0 deletions lib/ransack/nodes/grouping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def new_grouping(params = {})
end

def build(params)
params = strip_whitespace_from_params(params) if @context.strip_whitespace
params.with_indifferent_access.each do |key, value|
case key
when /^(g|c|m)$/
Expand Down Expand Up @@ -201,6 +202,19 @@ def remove_duplicate_conditions!
c.values.map { |v| v.value }
end
end

def strip_whitespace_from_params(params)
case params
when Hash
params.transform_values { |v| strip_whitespace_from_params(v) }
when Array
params.map { |v| strip_whitespace_from_params(v) }
when String
params.strip
else
params
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/ransack/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def initialize(object, params = {}, options = {})
end
@context = options[:context] || Context.for(object, options)
@context.auth_object = options[:auth_object]
@context.strip_whitespace = strip_whitespace
@base = Nodes::Grouping.new(
@context, options[:grouping] || Constants::AND
)
Expand Down
30 changes: 30 additions & 0 deletions spec/ransack/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,36 @@ module Ransack
.with({ 'name_eq' => ' foobar ' })
Search.new(Person, { name_eq: ' foobar ' }, { strip_whitespace: false })
end

context 'with group conditions' do
it 'strips leading & trailing whitespace from group condition values when strip_whitespace is true' do
search = Person.ransack({g: [{name_cont: " John ", email_cont: " john@example.com ", m: "or"}]}, { strip_whitespace: true })

# Access the base grouping to inspect conditions
grouping = search.base.groupings.first

# Check that whitespace was stripped from the values
name_condition = grouping.conditions.find { |c| c.predicate.name == "cont" && c.attributes.first.name == "name" }
email_condition = grouping.conditions.find { |c| c.predicate.name == "cont" && c.attributes.first.name == "email" }

expect(name_condition.values.first.value).to eq("John") # Should be stripped
expect(email_condition.values.first.value).to eq("john@example.com") # Should be stripped
end

it 'does not strip whitespace from group condition values when strip_whitespace is false' do
search = Person.ransack({g: [{name_cont: " John ", email_cont: " john@example.com ", m: "or"}]}, { strip_whitespace: false })

# Access the base grouping to inspect conditions
grouping = search.base.groupings.first

# Check that whitespace was NOT stripped from the values
name_condition = grouping.conditions.find { |c| c.predicate.name == "cont" && c.attributes.first.name == "name" }
email_condition = grouping.conditions.find { |c| c.predicate.name == "cont" && c.attributes.first.name == "email" }

expect(name_condition.values.first.value).to eq(" John ") # Should NOT be stripped
expect(email_condition.values.first.value).to eq(" john@example.com ") # Should NOT be stripped
end
end
end

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