From 16330f172a9b0ec0396832a58674b1f4d2a2cb50 Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Fri, 2 Feb 2024 13:02:22 +0000 Subject: [PATCH] Extract decrypt_as_text/encrypt_as_text --- .../encryption/encrypted_attribute_type.rb | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/activerecord/lib/active_record/encryption/encrypted_attribute_type.rb b/activerecord/lib/active_record/encryption/encrypted_attribute_type.rb index 9d25db9e63a0..fe3ce36199e9 100644 --- a/activerecord/lib/active_record/encryption/encrypted_attribute_type.rb +++ b/activerecord/lib/active_record/encryption/encrypted_attribute_type.rb @@ -81,17 +81,15 @@ def previous_type? @previous_type end - def decrypt(value) + def decrypt_as_text(value) with_context do unless value.nil? if @default && @default == value - decrypted_value = value + value else - decrypted_value = encryptor.decrypt(value, **decryption_options) + encryptor.decrypt(value, **decryption_options) end end - - reserialize(decrypted_value) end rescue ActiveRecord::Encryption::Errors::Base => error if previous_types_without_clean_text.blank? @@ -101,6 +99,10 @@ def decrypt(value) end end + def decrypt(value) + reserialize decrypt_as_text(value) + end + def try_to_deserialize_with_previous_encrypted_types(value) previous_types.each.with_index do |type, index| break type.deserialize(value) @@ -131,12 +133,16 @@ def serialize_with_current(value) encrypt(casted_value.to_s) unless casted_value.nil? end - def encrypt(value) + def encrypt_as_text(value) with_context do - reserialize(encryptor.encrypt(value, **encryption_options)) + encryptor.encrypt(value, **encryption_options) end end + def encrypt(value) + reserialize encrypt_as_text(value) + end + def encryptor ActiveRecord::Encryption.encryptor end