heartcombo / simple_form

Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.
http://blog.plataformatec.com.br/tag/simple_form
MIT License
8.21k stars 1.31k forks source link

Fix encrypted attributes improperly casted #1836

Closed Michaelvilleneuve closed 1 month ago

Michaelvilleneuve commented 6 months ago

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 image

Fixes https://github.com/heartcombo/simple_form/issues/1829

nashby commented 1 month ago

@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.