FIX: Update only passed custom fields (#14357)

It used to replace custom fields instead of updating only the custom
fields that were passed. The changes to custom fields will also be
logged.
This commit is contained in:
Bianca Nenciu
2021-09-17 13:37:56 +03:00
committed by GitHub
parent 8002b95682
commit c9ad9bff8a
4 changed files with 42 additions and 8 deletions

View File

@ -147,10 +147,23 @@ class CategoriesController < ApplicationController
guardian.ensure_can_edit!(@category)
json_result(@category, serializer: CategorySerializer) do |cat|
old_category_params = category_params.dup
cat.move_to(category_params[:position].to_i) if category_params[:position]
category_params.delete(:position)
old_custom_fields = cat.custom_fields.dup
if category_params[:custom_fields]
category_params[:custom_fields].each do |key, value|
if value.present?
cat.custom_fields[key] = value
else
cat.custom_fields.delete(key)
end
end
end
category_params.delete(:custom_fields)
# properly null the value so the database constraint doesn't catch us
category_params[:email_in] = nil if category_params[:email_in]&.blank?
category_params[:minimum_required_tags] = 0 if category_params[:minimum_required_tags]&.blank?
@ -159,7 +172,12 @@ class CategoriesController < ApplicationController
if result = cat.update(category_params)
Scheduler::Defer.later "Log staff action change category settings" do
@staff_action_logger.log_category_settings_change(@category, category_params, old_permissions)
@staff_action_logger.log_category_settings_change(
@category,
old_category_params,
old_permissions: old_permissions,
old_custom_fields: old_custom_fields
)
end
end