From 629810bd0742775bed54ee0050d33f190716da98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 16 Aug 2017 23:04:40 +0200 Subject: [PATCH] FIX: when registering custom_field types, ensure we're casting them before checking for equality --- app/models/concerns/has_custom_fields.rb | 6 ++++-- spec/components/concern/has_custom_fields_spec.rb | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/has_custom_fields.rb b/app/models/concerns/has_custom_fields.rb index 139b598e28b..25a6422b0e5 100644 --- a/app/models/concerns/has_custom_fields.rb +++ b/app/models/concerns/has_custom_fields.rb @@ -139,7 +139,6 @@ module HasCustomFields end def custom_fields - if @preloaded_custom_fields return @preloaded_proxy ||= PreloadedProxy.new(@preloaded_custom_fields) end @@ -177,7 +176,10 @@ module HasCustomFields dup.delete(f.name) end 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 else dup.delete(f.name) diff --git a/spec/components/concern/has_custom_fields_spec.rb b/spec/components/concern/has_custom_fields_spec.rb index 75ec51dd058..fc1f1a43e82 100644 --- a/spec/components/concern/has_custom_fields_spec.rb +++ b/spec/components/concern/has_custom_fields_spec.rb @@ -142,6 +142,16 @@ describe HasCustomFields do test_item.reload 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 it "simple modifications don't interfere" do