Support Rails normalizes method and fix empty string handling consistency#471
Closed
snaka wants to merge 2 commits intobrainspec:masterfrom
Closed
Support Rails normalizes method and fix empty string handling consistency#471snaka wants to merge 2 commits intobrainspec:masterfrom
snaka wants to merge 2 commits intobrainspec:masterfrom
Conversation
Add test cases to verify that enumerized attributes work correctly with Rails 7.1+ normalizes method for data normalization.
Fix issue where empty strings were not being stored as nil in Sequel and other ORMs. This ensures consistent behavior across all ORMs by converting empty strings to nil, matching ActiveRecord's behavior. - Convert empty strings to nil in attribute setter - Fix failing SequelTest for empty string assignment - Ensure consistent behavior between ActiveRecord and Sequel
Contributor
Author
|
I feel like this PR has become a bit hard to understand, so I'll fix it again on a separate branch. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation / Background
This Pull Request enhances Enumerize to properly support Rails 7.1+
normalizesmethod and addresses empty string handling consistency across ORMs.Detail
Rails Normalizes Integration
Rails 7.1 introduced the
normalizesmethod for attribute normalization:The existing
Type#castandType#serializemethods didn't properly delegate to subtypes for normalization. This prevented normalized values from being correctly matched against enum values, causing enum validation to fail even when the normalized value was valid.For example, with the above configuration, setting
locale = " DE "should normalize to"de"and match the:deenum value, but this wasn't working properly.Fixes #459
Empty String Handling Consistency
Additionally, Enumerize exhibited inconsistent behavior when handling empty strings across various ORMs:
nil""This inconsistency led to failing tests, specifically:
"stores nil when empty string assigned"in SequelTest.Solution
1. Enhanced Type Casting for Normalization Support
Updated
lib/enumerize/activerecord.rbto properly handle value transformation through subtypes:Similar logic was applied to the
serializemethod to ensure consistent behavior during database operations.2. Explicit Empty String Conversion
Modified
lib/enumerize/attribute.rbto explicitly convert empty strings tonilin the attribute setter:This ensures consistent behavior across all ORMs, eliminating the discrepancy between ActiveRecord and other ORMs like Sequel.