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_whitespaceoption 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" [email protected] "would retain their leading/trailing whitespace even whenstrip_whitespacewas enabled globally or passed as an option.Root Cause
The whitespace stripping logic was only implemented in
Search#initializeat the top level. When group conditions were processed, they went through theGrouping#buildmethod which didn't have access to thestrip_whitespaceoption, so nested conditions never had their whitespace stripped.Solution
This fix implements a minimal change to pass the
strip_whitespaceoption down to nested groupings:Added
strip_whitespaceattribute to Context: The baseContextclass now stores the strip_whitespace setting so it's available to all nested operations.Set context strip_whitespace in Search: The
Search#initializemethod now sets thestrip_whitespaceoption on the context after determining it from options or global config.Enhanced Grouping#build with whitespace stripping: The
Grouping#buildmethod now applies whitespace stripping recursively whenstrip_whitespaceis enabled.Added recursive helper method: A new
strip_whitespace_from_paramsmethod handles stripping whitespace from nested Hash, Array, and String structures.Testing
strip_whitespace: trueandstrip_whitespace: falsescenarios 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: falseoptions.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.