2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-05 08:59:27 +08:00

New interface to upsert custom fields

This commit is contained in:
Robin Ward 2018-03-02 12:45:34 -05:00
parent 0ec1dc9237
commit 730201d423
2 changed files with 40 additions and 0 deletions

View file

@ -155,6 +155,20 @@ module HasCustomFields
!@custom_fields || @custom_fields_orig == @custom_fields
end
# `upsert_custom_fields` will only insert/update existing fields, and will not
# delete anything. It is safer under concurrency and is recommended when
# you just want to attach fields to things without maintaining a specific
# set of fields.
def upsert_custom_fields(fields)
fields.each do |k, v|
row_count = _custom_fields.where(name: k).update_all(value: v)
if row_count == 0
_custom_fields.create!(name: k, value: v)
end
custom_fields[k] = v
end
end
def save_custom_fields(force = false)
if force || !custom_fields_clean?
dup = @custom_fields.dup