FIX: Nil-filled CF arrays were not being deleted (#13518)

This commit is contained in:
Jarek Radosz 2021-06-25 11:34:51 +02:00 committed by GitHub
parent 8ab6fd88ef
commit b4f0a0fb94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 5 deletions

View File

@ -233,10 +233,10 @@ module HasCustomFields
t = {}
self.class.append_custom_field(t, f.name, f.value)
if dup[f.name] != t[f.name]
f.destroy!
else
if dup.has_key?(f.name) && dup[f.name] == t[f.name]
dup.delete(f.name)
else
f.destroy!
end
end
end

View File

@ -3,7 +3,6 @@
require "rails_helper"
describe HasCustomFields do
context "custom_fields" do
before do
DB.exec("create temporary table custom_fields_test_items(id SERIAL primary key)")
@ -104,7 +103,6 @@ describe HasCustomFields do
end
it "handles arrays properly" do
CustomFieldsTestItem.register_custom_field_type "array", [:integer]
test_item = CustomFieldsTestItem.new
test_item.custom_fields = { "array" => ["1"] }
@ -136,6 +134,19 @@ describe HasCustomFields do
expect(db_item.custom_fields).to eq({})
end
it "deletes nil-filled arrays" do
test_item = CustomFieldsTestItem.create!
db_item = CustomFieldsTestItem.find(test_item.id)
db_item.custom_fields.update("a" => [nil, nil])
db_item.save_custom_fields
db_item.custom_fields.delete("a")
expect(db_item.custom_fields).to eq({})
db_item.save_custom_fields
expect(db_item.custom_fields).to eq({})
end
it "casts integers in arrays properly without error" do
test_item = CustomFieldsTestItem.new
test_item.custom_fields = { "a" => ["b", 10, "d"] }