Skip to content

Commit 34ac8fc

Browse files
committed
Make active record encryption work with store attributes
Fix: rails#43012
1 parent 6ec669b commit 34ac8fc

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

activerecord/lib/active_record/encryption/encrypted_attribute_type.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class EncryptedAttributeType < ::ActiveRecord::Type::Text
1313
attr_reader :scheme, :cast_type
1414

1515
delegate :key_provider, :downcase?, :deterministic?, :previous_schemes, :with_context, :fixed?, to: :scheme
16+
delegate :accessor, to: :cast_type
1617

1718
# === Options
1819
#

activerecord/test/cases/encryption/encryptable_record_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ class ActiveRecord::Encryption::EncryptableRecordTest < ActiveRecord::Encryption
4747
assert_encrypted_attribute(traffic_light, :state, states)
4848
end
4949

50+
test "encrypts store attributes with accessors" do
51+
traffic_light = EncryptedTrafficLightWithStoreState.create!(color: "red", long_state: %i[ green red ])
52+
assert_equal "red", traffic_light.color
53+
assert_encrypted_attribute(traffic_light, :state, { "color" => "red" })
54+
end
55+
5056
test "can configure a custom key provider on a per-record-class basis through the :key_provider option" do
5157
post = EncryptedPost.create!(title: "The Starfleet is here!", body: "take cover!")
5258
assert_encrypted_attribute(post, :body, "take cover!")

activerecord/test/models/traffic_light_encrypted.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@
66
class EncryptedTrafficLight < TrafficLight
77
encrypts :state
88
end
9+
10+
class EncryptedTrafficLightWithStoreState < TrafficLight
11+
store :state, accessors: %i[ color ]
12+
encrypts :state
13+
end

0 commit comments

Comments
 (0)