FIX: when registering custom_field types, ensure we're casting them before checking for equality

This commit is contained in:
Régis Hanol 2017-08-16 23:04:40 +02:00
parent 55f449edc5
commit 629810bd07
2 changed files with 14 additions and 2 deletions

View File

@ -139,7 +139,6 @@ module HasCustomFields
end end
def custom_fields def custom_fields
if @preloaded_custom_fields if @preloaded_custom_fields
return @preloaded_proxy ||= PreloadedProxy.new(@preloaded_custom_fields) return @preloaded_proxy ||= PreloadedProxy.new(@preloaded_custom_fields)
end end
@ -177,7 +176,10 @@ module HasCustomFields
dup.delete(f.name) dup.delete(f.name)
end end
else else
if dup[f.name] != f.value t = {}
self.class.append_custom_field(t, f.name, f.value)
if dup[f.name] != t[f.name]
f.destroy f.destroy
else else
dup.delete(f.name) dup.delete(f.name)

View File

@ -142,6 +142,16 @@ describe HasCustomFields do
test_item.reload test_item.reload
expect(test_item.custom_fields).to eq("bool" => true, "int" => 1, "json" => { "foo" => "bar" }) expect(test_item.custom_fields).to eq("bool" => true, "int" => 1, "json" => { "foo" => "bar" })
before_ids = CustomFieldsTestItemCustomField.where(custom_fields_test_item_id: test_item.id).pluck(:id)
test_item.custom_fields["bool"] = false
test_item.save
after_ids = CustomFieldsTestItemCustomField.where(custom_fields_test_item_id: test_item.id).pluck(:id)
# we updated only 1 custom field, so there should be only 1 different id
expect((before_ids - after_ids).size).to eq(1)
end end
it "simple modifications don't interfere" do it "simple modifications don't interfere" do