From eaa74eedba945e1bdcedd7fa6ac38c6493edd735 Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Tue, 13 Aug 2024 21:04:24 +0000 Subject: [PATCH] EncryptedAttributeType#type should return cast_type's type. (#52247) --- activerecord/CHANGELOG.md | 5 +++++ .../lib/active_record/encryption/encrypted_attribute_type.rb | 4 ++-- .../test/cases/encryption/encryptable_record_test.rb | 5 +++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index eb20701e9a177..320a0f16402b5 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,8 @@ +* Fix `ActiveRecord::Encryption::EncryptedAttributeType#type` to return + actual cast type. + + *Vasiliy Ermolovich* + * SQLite3Adapter: Bulk insert fixtures. Previously one insert command was executed for each fixture, now they are diff --git a/activerecord/lib/active_record/encryption/encrypted_attribute_type.rb b/activerecord/lib/active_record/encryption/encrypted_attribute_type.rb index f02b89755b561..10948ddea91b6 100644 --- a/activerecord/lib/active_record/encryption/encrypted_attribute_type.rb +++ b/activerecord/lib/active_record/encryption/encrypted_attribute_type.rb @@ -7,13 +7,13 @@ module Encryption # This is the central piece that connects the encryption system with +encrypts+ declarations in the # model classes. Whenever you declare an attribute as encrypted, it configures an +EncryptedAttributeType+ # for that attribute. - class EncryptedAttributeType < ::ActiveRecord::Type::Text + class EncryptedAttributeType < ::ActiveModel::Type::Value include ActiveModel::Type::Helpers::Mutable attr_reader :scheme, :cast_type delegate :key_provider, :downcase?, :deterministic?, :previous_schemes, :with_context, :fixed?, to: :scheme - delegate :accessor, to: :cast_type + delegate :accessor, :type, to: :cast_type # === Options # diff --git a/activerecord/test/cases/encryption/encryptable_record_test.rb b/activerecord/test/cases/encryption/encryptable_record_test.rb index 010f6a9b11873..74f7fb490631f 100644 --- a/activerecord/test/cases/encryption/encryptable_record_test.rb +++ b/activerecord/test/cases/encryption/encryptable_record_test.rb @@ -418,6 +418,11 @@ def name assert EncryptedBookWithCustomCompressor.create!(name: name).name.start_with?("[compressed]") end + test "type method returns cast type" do + assert_equal :string, EncryptedBook.type_for_attribute(:name).type + assert_equal :text, EncryptedPost.type_for_attribute(:body).type + end + private def build_derived_key_provider_with(hash_digest_class) ActiveRecord::Encryption.with_encryption_context(key_generator: ActiveRecord::Encryption::KeyGenerator.new(hash_digest_class: hash_digest_class)) do