active_type
active_type copied to clipboard
Support for `read_attribute_before_type_cast`
Hi! Hope you're doing well.
Using active_type with custom serialization breaks because calling read_attribute_before_type_cast on a ActiveType::Object returns nil. I've managed to work around the problem by implementing the following in the custom serializer:
raw_data = defined?(ActiveType::Object) && is_a?(ActiveType::Object) ? virtual_attributes[attribute_name.to_s] : read_attribute_before_type_cast(attribute_name)
result = my_custom_type_value.deserialize(raw_data)
I thus suspect that overriding read_attribute_before_type_cast in active_type and having it return virtual_attributes[attribute_name] would likely fix this. However, I'm not sure about compatibility with different Rails versions.
What do you think, would this be a sensible way to fix this problem?
Best, Kalsan