From 34ac8fc1f11ed56fe114bee403a06cacbb836c46 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Sat, 14 Aug 2021 11:06:51 +0200 Subject: [PATCH] Make active record encryption work with store attributes Fix: https://github.com/rails/rails/issues/43012 --- .../active_record/encryption/encrypted_attribute_type.rb | 1 + .../test/cases/encryption/encryptable_record_test.rb | 6 ++++++ activerecord/test/models/traffic_light_encrypted.rb | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/activerecord/lib/active_record/encryption/encrypted_attribute_type.rb b/activerecord/lib/active_record/encryption/encrypted_attribute_type.rb index 00b007b02a..51bd2da630 100644 --- a/activerecord/lib/active_record/encryption/encrypted_attribute_type.rb +++ b/activerecord/lib/active_record/encryption/encrypted_attribute_type.rb @@ -13,6 +13,7 @@ module ActiveRecord attr_reader :scheme, :cast_type delegate :key_provider, :downcase?, :deterministic?, :previous_schemes, :with_context, :fixed?, to: :scheme + delegate :accessor, 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 7edf238432..1e25904ae9 100644 --- a/activerecord/test/cases/encryption/encryptable_record_test.rb +++ b/activerecord/test/cases/encryption/encryptable_record_test.rb @@ -47,6 +47,12 @@ class ActiveRecord::Encryption::EncryptableRecordTest < ActiveRecord::Encryption assert_encrypted_attribute(traffic_light, :state, states) end + test "encrypts store attributes with accessors" do + traffic_light = EncryptedTrafficLightWithStoreState.create!(color: "red", long_state: %i[ green red ]) + assert_equal "red", traffic_light.color + assert_encrypted_attribute(traffic_light, :state, { "color" => "red" }) + end + test "can configure a custom key provider on a per-record-class basis through the :key_provider option" do post = EncryptedPost.create!(title: "The Starfleet is here!", body: "take cover!") assert_encrypted_attribute(post, :body, "take cover!") diff --git a/activerecord/test/models/traffic_light_encrypted.rb b/activerecord/test/models/traffic_light_encrypted.rb index fd23e95e99..46b887bbbd 100644 --- a/activerecord/test/models/traffic_light_encrypted.rb +++ b/activerecord/test/models/traffic_light_encrypted.rb @@ -6,3 +6,8 @@ require "models/traffic_light" class EncryptedTrafficLight < TrafficLight encrypts :state end + +class EncryptedTrafficLightWithStoreState < TrafficLight + store :state, accessors: %i[ color ] + encrypts :state +end