Skip to content

Commit

Permalink
Extract decrypt_as_text/encrypt_as_text
Browse files Browse the repository at this point in the history
  • Loading branch information
djmb committed Feb 2, 2024
1 parent 7ed7408 commit 16330f1
Showing 1 changed file with 13 additions and 7 deletions.
Expand Up @@ -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?
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 16330f1

Please sign in to comment.