Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix encrypted attributes improperly casted #1836

Conversation

Michaelvilleneuve
Copy link
Contributor

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 #1829

@nashby
Copy link
Collaborator

nashby commented Jul 12, 2024

@Michaelvilleneuve hey! Thanks for the PR! It looks like Rails bug to me so I've sent a PR fixing it rails/rails#52247 but I think it's ok to merge this for now.

@nashby nashby merged commit c2c7faf into heartcombo:main Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Encrypted attributes always default to textarea
2 participants