Rename previous_types_including_clean_text => previous_types

It's make more sense to revert the naming approach:

- `#previous_types`, the exposed public method, always include the clean
 text type when suport for unencrypted data is enabled
- `#previous_types_without_clean_text` is a private method used
internally by the type
This commit is contained in:
Jorge Manrubia 2021-04-22 11:10:18 +02:00 committed by George Claghorn
parent 2c79d2b260
commit 9ed49b76fb
5 changed files with 19 additions and 19 deletions

@ -43,9 +43,9 @@ def changed_in_place?(raw_old_value, new_value)
old_value != new_value
end
def previous_types_including_clean_text # :nodoc:
@previous_types_including_clean_text ||= {} # Memoizing on support_unencrypted_data so that we can tweak it during tests
@previous_types_including_clean_text[support_unencrypted_data?] ||= build_previous_types_for(previous_schemes_including_clean_text)
def previous_types # :nodoc:
@previous_types ||= {} # Memoizing on support_unencrypted_data so that we can tweak it during tests
@previous_types[support_unencrypted_data?] ||= build_previous_types_for(previous_schemes_including_clean_text)
end
private
@ -53,8 +53,8 @@ def previous_schemes_including_clean_text
previous_schemes.including((clean_text_scheme if support_unencrypted_data?)).compact
end
def previous_types
@previous_types ||= build_previous_types_for(previous_schemes)
def previous_types_without_clean_text
@previous_types_without_clean_text ||= build_previous_types_for(previous_schemes)
end
def build_previous_types_for(schemes)
@ -72,7 +72,7 @@ def decrypt(value)
encryptor.decrypt(value, **decryption_options) unless value.nil?
end
rescue ActiveRecord::Encryption::Errors::Base => error
if previous_types.blank?
if previous_types_without_clean_text.blank?
handle_deserialize_error(error, value)
else
try_to_deserialize_with_previous_encrypted_types(value)
@ -80,10 +80,10 @@ def decrypt(value)
end
def try_to_deserialize_with_previous_encrypted_types(value)
previous_types_including_clean_text.each.with_index do |type, index|
previous_types.each.with_index do |type, index|
break type.deserialize(value)
rescue ActiveRecord::Encryption::Errors::Base => error
handle_deserialize_error(error, value) if index == previous_types.length - 1
handle_deserialize_error(error, value) if index == previous_types_without_clean_text.length - 1
end
end
@ -96,11 +96,11 @@ def handle_deserialize_error(error, value)
end
def serialize_with_oldest?
@serialize_with_oldest ||= fixed? && previous_types.present?
@serialize_with_oldest ||= fixed? && previous_types_without_clean_text.present?
end
def serialize_with_oldest(value)
previous_types_including_clean_text.first.serialize(value)
previous_types.first.serialize(value)
end
def serialize_with_current(value)

@ -46,7 +46,7 @@ def process_encrypted_query_arguments(args, check_for_additional_values)
if args.is_a?(Array) && (options = args.first).is_a?(Hash)
self.deterministic_encrypted_attributes&.each do |attribute_name|
type = type_for_attribute(attribute_name)
if !type.previous_types_including_clean_text.empty? && value = options[attribute_name]
if !type.previous_types.empty? && value = options[attribute_name]
options[attribute_name] = process_encrypted_query_argument(value, check_for_additional_values, type)
end
end
@ -72,7 +72,7 @@ def process_encrypted_query_argument(value, check_for_additional_values, type)
end
def additional_values_for(value, type)
type.previous_types_including_clean_text.collect do |additional_type|
type.previous_types.collect do |additional_type|
AdditionalValue.new(value, additional_type)
end
end

@ -14,7 +14,7 @@ def validate_each(record, attribute, value)
klass = record.class
if klass.deterministic_encrypted_attributes&.each do |attribute_name|
encrypted_type = klass.type_for_attribute(attribute_name)
[ encrypted_type, *encrypted_type.previous_types_including_clean_text ].each do |type|
[ encrypted_type, *encrypted_type.previous_types ].each do |type|
encrypted_value = type.serialize(value)
ActiveRecord::Encryption.without_encryption do
super(record, attribute, encrypted_value)

@ -106,7 +106,7 @@ class ActiveRecord::Encryption::EncryptableRecordApiTest < ActiveRecord::Encrypt
test "encrypt attributes encrypted with a previous encryption scheme" do
author = EncryptedAuthor.create!(name: "david")
old_type = EncryptedAuthor.type_for_attribute(:name).previous_types_including_clean_text.first
old_type = EncryptedAuthor.type_for_attribute(:name).previous_types.first
value_encrypted_with_old_type = old_type.serialize("dhh")
ActiveRecord::Encryption.without_encryption do
author.update!(name: value_encrypted_with_old_type)

@ -50,8 +50,8 @@ class ActiveRecord::Encryption::EncryptionSchemesTest < ActiveRecord::Encryption
encrypts :name
end
assert_equal 2, encrypted_author_class.type_for_attribute(:name).previous_types_including_clean_text.count
previous_type_1, previous_type_2 = encrypted_author_class.type_for_attribute(:name).previous_types_including_clean_text
assert_equal 2, encrypted_author_class.type_for_attribute(:name).previous_types.count
previous_type_1, previous_type_2 = encrypted_author_class.type_for_attribute(:name).previous_types
author = ActiveRecord::Encryption.without_encryption do
encrypted_author_class.create name: previous_type_1.serialize("1")
@ -75,8 +75,8 @@ class ActiveRecord::Encryption::EncryptionSchemesTest < ActiveRecord::Encryption
encrypts :name
end
assert_equal 3, encrypted_author_class.type_for_attribute(:name).previous_types_including_clean_text.count
previous_type_1, previous_type_2 = encrypted_author_class.type_for_attribute(:name).previous_types_including_clean_text
assert_equal 3, encrypted_author_class.type_for_attribute(:name).previous_types.count
previous_type_1, previous_type_2 = encrypted_author_class.type_for_attribute(:name).previous_types
author = ActiveRecord::Encryption.without_encryption do
encrypted_author_class.create name: previous_type_1.serialize("1")
@ -153,7 +153,7 @@ class EncryptedAuthor2 < Author
def create_author_with_name_encrypted_with_previous_scheme
author = EncryptedAuthor.create!(name: "david")
old_type = EncryptedAuthor.type_for_attribute(:name).previous_types_including_clean_text.first
old_type = EncryptedAuthor.type_for_attribute(:name).previous_types.first
value_encrypted_with_old_type = old_type.serialize("dhh")
ActiveRecord::Encryption.without_encryption do
author.update!(name: value_encrypted_with_old_type)