Fix date predicate formatter receiving String instead of Date object #1651
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
After upgrading from Ransack 4.3.0 to 4.4.0, custom predicate formatters with
type: :date
started receiving String values instead of properly casted Date/Time objects. This caused errors when trying to call date methods like.end_of_day
:Root Cause
The
Value#cast
method uses a Rubycase
statement to match type values against symbols:When
type
is a String ("date"
) instead of a Symbol (:date
), it doesn't match any of thewhen
clauses and falls through to theelse
branch, which returns the original string value instead of a Date object.Solution
Added defensive type conversion at the beginning of the
Value#cast
method to handle both string and symbol types:This ensures that string types like
"date"
,"datetime"
, etc. are automatically converted to symbols before case matching, allowing them to be cast correctly.Changes
'date'
works correctlyImpact
Example Usage
After this fix, the following code works correctly:
The formatter now correctly receives a Date object and can call
.end_of_day
without errors.Fixes #XXXX
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.