DEV: Ensure custom_fields_clean? returns false when values change ()

We were calling `dup` on the hash and using that to check for changes. However, we were not duplicating the values, so changes to arrays or nested hashes would not be detected.
This commit is contained in:
David Taylor 2022-04-25 17:19:39 +01:00 committed by GitHub
parent 32346f4ba5
commit 922fbe82da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions
app/models/concerns
spec/lib/concern

@ -301,7 +301,7 @@ protected
self.class.append_custom_field(target, key, value) self.class.append_custom_field(target, key, value)
end end
@custom_fields_orig = target @custom_fields_orig = target
@custom_fields = @custom_fields_orig.dup @custom_fields = @custom_fields_orig.deep_dup
end end
end end

@ -307,6 +307,35 @@ describe HasCustomFields do
expect(test_item.reload.custom_fields).to eq(expected) expect(test_item.reload.custom_fields).to eq(expected)
end end
it 'determines clean state correctly for mutable fields' do
json_field = "json_field"
array_field = "array_field"
CustomFieldsTestItem.register_custom_field_type(json_field, :json)
CustomFieldsTestItem.register_custom_field_type(array_field, :array)
item_with_array = CustomFieldsTestItem.new
expect(item_with_array.custom_fields_clean?).to eq(true)
item_with_array.custom_fields[array_field] = [1]
expect(item_with_array.custom_fields_clean?).to eq(false)
item_with_array.save!
expect(item_with_array.custom_fields_clean?).to eq(true)
item_with_array.custom_fields[array_field] << 2
expect(item_with_array.custom_fields_clean?).to eq(false)
item_with_array.save!
expect(item_with_array.custom_fields_clean?).to eq(true)
item_with_json = CustomFieldsTestItem.new
expect(item_with_json.custom_fields_clean?).to eq(true)
item_with_json.custom_fields[json_field] = { "hello" => "world" }
expect(item_with_json.custom_fields_clean?).to eq(false)
item_with_json.save!
expect(item_with_json.custom_fields_clean?).to eq(true)
item_with_json.custom_fields[json_field]["hello"] = "world2"
expect(item_with_json.custom_fields_clean?).to eq(false)
item_with_json.save!
expect(item_with_json.custom_fields_clean?).to eq(true)
end
describe "create_singular" do describe "create_singular" do
it "creates new records" do it "creates new records" do
item = CustomFieldsTestItem.create! item = CustomFieldsTestItem.create!