Merge pull request #46789 from ezekg/fix-string-keys-for-deterministic-active-record-encryption

Support string keys for queries on deterministic attributes
This commit is contained in:
Jean Boussier 2022-12-22 16:27:19 +01:00 committed by GitHub
commit 28386bdc0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -44,12 +44,12 @@ module ActiveRecord
return args if owner.deterministic_encrypted_attributes&.empty?
if args.is_a?(Array) && (options = args.first).is_a?(Hash)
options = options.dup
options = options.stringify_keys
args[0] = options
owner.deterministic_encrypted_attributes&.each do |attribute_name|
type = owner.type_for_attribute(attribute_name)
if !type.previous_types.empty? && value = options[attribute_name]
if !type.previous_types.empty? && value = options[attribute_name.to_s]
options[attribute_name] = process_encrypted_query_argument(value, check_for_additional_values, type)
end
end

View File

@ -25,6 +25,11 @@ class ActiveRecord::Encryption::ExtendedDeterministicQueriesTest < ActiveRecord:
assert EncryptedBookWithDowncaseName.find_by(name: "DUNE")
end
test "Works well with string attribute names" do
UnencryptedBook.create! "name" => "Dune"
assert EncryptedBook.find_by("name" => "Dune")
end
test "find_or_create works" do
EncryptedBook.find_or_create_by!(name: "Dune")
assert EncryptedBook.find_by(name: "Dune")