This PR fixes a bug introduced by ActiveRecord::Encrypted attributes.
Instead of returning a ActiveModel::Type::String like they used to, encrypted attributes return an instance of ActiveRecord::Encryption::EncryptedAttributeType, on which you need to call #cast_type in order to obtain the original type.
To fix this, I check whether or not the attribute #responds_to?(:cast_type) which would indicate that we need to take into account the underlying type instead of just #type_for_attribute.
Checking this instead of checking if the attribute_name#responds_to?(:encrypted_attributes) allows to take into account other forms of type overriding that ActiveRecord may introduce in the future or potentially already exist.
See the type difference below between a regular string attribute and an encrypted attribute
@Michaelvilleneuve hey! Thanks for the PR! It looks like Rails bug to me so I've sent a PR fixing it https://github.com/rails/rails/pull/52247 but I think it's ok to merge this for now.
This PR fixes a bug introduced by ActiveRecord::Encrypted attributes. Instead of returning a
ActiveModel::Type::String
like they used to, encrypted attributes return an instance ofActiveRecord::Encryption::EncryptedAttributeType
, on which you need to call#cast_type
in order to obtain the original type.To fix this, I check whether or not the attribute
#responds_to?(:cast_type)
which would indicate that we need to take into account the underlying type instead of just#type_for_attribute
.Checking this instead of checking if the
attribute_name
#responds_to?(:encrypted_attributes)
allows to take into account other forms of type overriding that ActiveRecord may introduce in the future or potentially already exist.See the type difference below between a regular string attribute and an encrypted attribute![image](https://github.com/heartcombo/simple_form/assets/4990201/41ada727-8776-4660-a2dd-0bbf1ba1ee83)
Fixes https://github.com/heartcombo/simple_form/issues/1829