Make active record encryption work with store attributes

Fix: https://github.com/rails/rails/issues/43012
This commit is contained in:
Jorge Manrubia 2021-08-14 11:06:51 +02:00
parent 6ec669b65d
commit 34ac8fc1f1
3 changed files with 12 additions and 0 deletions

View File

@ -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
#

View File

@ -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!")

View File

@ -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