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

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

diff --git a/app/models/concerns/has_custom_fields.rb b/app/models/concerns/has_custom_fields.rb
index 925bb8a..f073544 100644
--- a/app/models/concerns/has_custom_fields.rb
+++ b/app/models/concerns/has_custom_fields.rb
@@ -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
diff --git a/spec/components/concern/has_custom_fields_spec.rb b/spec/components/concern/has_custom_fields_spec.rb
index 6fab074..c2f4aff 100644
--- a/spec/components/concern/has_custom_fields_spec.rb
+++ b/spec/components/concern/has_custom_fields_spec.rb
@@ -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"] }

GitHub sha: b4f0a0fb9489223f6d4d56ddbfe0f5532a6f598a

This commit appears in #13518 which was approved by eviltrout. It was merged by CvX.