mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: JSON custom fields incorrectly being converted to an array.
https://meta.discourse.org/t/custom-fields-simultaneous-save-with-json-becomes-an-array/73647
This commit is contained in:
parent
cd744d58f8
commit
4bb454d889
@ -161,7 +161,7 @@ module HasCustomFields
|
|||||||
|
|
||||||
array_fields = {}
|
array_fields = {}
|
||||||
|
|
||||||
_custom_fields.each do |f|
|
_custom_fields.reload.each do |f|
|
||||||
if dup[f.name].is_a? Array
|
if dup[f.name].is_a? Array
|
||||||
# we need to collect Arrays fully before we can compare them
|
# we need to collect Arrays fully before we can compare them
|
||||||
if !array_fields.has_key?(f.name)
|
if !array_fields.has_key?(f.name)
|
||||||
@ -171,7 +171,7 @@ module HasCustomFields
|
|||||||
end
|
end
|
||||||
elsif dup[f.name].is_a? Hash
|
elsif dup[f.name].is_a? Hash
|
||||||
if dup[f.name].to_json != f.value
|
if dup[f.name].to_json != f.value
|
||||||
f.destroy
|
f.destroy!
|
||||||
else
|
else
|
||||||
dup.delete(f.name)
|
dup.delete(f.name)
|
||||||
end
|
end
|
||||||
@ -180,7 +180,7 @@ module HasCustomFields
|
|||||||
self.class.append_custom_field(t, f.name, f.value)
|
self.class.append_custom_field(t, f.name, f.value)
|
||||||
|
|
||||||
if dup[f.name] != t[f.name]
|
if dup[f.name] != t[f.name]
|
||||||
f.destroy
|
f.destroy!
|
||||||
else
|
else
|
||||||
dup.delete(f.name)
|
dup.delete(f.name)
|
||||||
end
|
end
|
||||||
@ -198,11 +198,12 @@ module HasCustomFields
|
|||||||
|
|
||||||
dup.each do |k, v|
|
dup.each do |k, v|
|
||||||
if v.is_a? Array
|
if v.is_a? Array
|
||||||
v.each { |subv| _custom_fields.create(name: k, value: subv) }
|
v.each { |subv| _custom_fields.create!(name: k, value: subv) }
|
||||||
elsif v.is_a? Hash
|
|
||||||
_custom_fields.create(name: k, value: v.to_json)
|
|
||||||
else
|
else
|
||||||
_custom_fields.create(name: k, value: v)
|
_custom_fields.create!(
|
||||||
|
name: k,
|
||||||
|
value: v.is_a?(Hash) ? v.to_json : v
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -193,5 +193,27 @@ describe HasCustomFields do
|
|||||||
expect(fields[item1.id]['not_whitelisted']).to be_blank
|
expect(fields[item1.id]['not_whitelisted']).to be_blank
|
||||||
expect(fields[item2.id]['e']).to eq('hallo')
|
expect(fields[item2.id]['e']).to eq('hallo')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "handles interleaving saving properly" do
|
||||||
|
field_type = 'deep-nest-test'
|
||||||
|
CustomFieldsTestItem.register_custom_field_type(field_type, :json)
|
||||||
|
test_item = CustomFieldsTestItem.create!
|
||||||
|
|
||||||
|
test_item.custom_fields[field_type] ||= {}
|
||||||
|
test_item.custom_fields[field_type]['b'] ||= {}
|
||||||
|
test_item.custom_fields[field_type]['b']['c'] = 'd'
|
||||||
|
test_item.save_custom_fields(true)
|
||||||
|
|
||||||
|
db_item = CustomFieldsTestItem.find(test_item.id)
|
||||||
|
db_item.custom_fields[field_type]['b']['e'] = 'f'
|
||||||
|
test_item.custom_fields[field_type]['b']['e'] = 'f'
|
||||||
|
expected = { field_type => { 'b' => { 'c' => 'd', 'e' => 'f' } } }
|
||||||
|
|
||||||
|
db_item.save_custom_fields(true)
|
||||||
|
expect(db_item.reload.custom_fields).to eq(expected)
|
||||||
|
|
||||||
|
test_item.save_custom_fields(true)
|
||||||
|
expect(test_item.reload.custom_fields).to eq(expected)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user