Fix strip_whitespace not working with group conditions #1635
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The
strip_whitespace
option was not working for group conditions in Ransack searches. While whitespace stripping worked correctly for top-level conditions, it was ignored when using grouped conditions with theg:
parameter.For example, this search would not strip whitespace from the values:
The values
" John "
and" john@example.com "
would retain their leading/trailing whitespace even whenstrip_whitespace
was enabled globally or passed as an option.Root Cause
The whitespace stripping logic was only implemented in
Search#initialize
at the top level. When group conditions were processed, they went through theGrouping#build
method which didn't have access to thestrip_whitespace
option, so nested conditions never had their whitespace stripped.Solution
This fix implements a minimal change to pass the
strip_whitespace
option down to nested groupings:Added
strip_whitespace
attribute to Context: The baseContext
class now stores the strip_whitespace setting so it's available to all nested operations.Set context strip_whitespace in Search: The
Search#initialize
method now sets thestrip_whitespace
option on the context after determining it from options or global config.Enhanced Grouping#build with whitespace stripping: The
Grouping#build
method now applies whitespace stripping recursively whenstrip_whitespace
is enabled.Added recursive helper method: A new
strip_whitespace_from_params
method handles stripping whitespace from nested Hash, Array, and String structures.Testing
strip_whitespace: true
andstrip_whitespace: false
scenarios for group conditionsExample
After this fix, the following now works as expected:
The fix maintains full backward compatibility and respects both global
Ransack.options[:strip_whitespace]
configuration and per-searchstrip_whitespace: false
options.Fixes the issue where group conditions didn't respect the strip_whitespace setting.
Original prompt
Fixes #1414
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.