From bf1182ba7c19a9de4440679a4c431742ead8e8b3 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Wed, 4 Feb 2026 19:48:46 +0100 Subject: [PATCH] refactor custom_field_values= to calm down rubocop --- .../lib/acts_as_customizable.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb index a6840af4533..02200694da7 100644 --- a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb +++ b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb @@ -126,17 +126,13 @@ module Redmine def custom_field_values=(values) return unless values.is_a?(Hash) && values.any? - values.with_indifferent_access.each do |custom_field_id, val| + values.with_indifferent_access.each do |custom_field_id, new_values| existing_cv_by_value = custom_values_for_custom_field(custom_field_id, all: true) .group_by(&:value) .transform_values(&:first) - new_values = Array(val).map { |v| v.respond_to?(:id) ? v.id.to_s : v.to_s } + next if existing_cv_by_value.empty? - if existing_cv_by_value.any? - assign_new_values custom_field_id, existing_cv_by_value, new_values - delete_obsolete_custom_values existing_cv_by_value, new_values - handle_minimum_custom_value custom_field_id, existing_cv_by_value, new_values - end + update_custom_value(custom_field_id, existing_cv_by_value, new_values) end end @@ -435,6 +431,14 @@ module Redmine touch if !saved_changes? && custom_values.loaded? && (custom_values.any?(&:saved_changes?) || custom_value_destroyed) end + def update_custom_value(custom_field_id, existing_cv_by_value, new_values) + new_values = Array(new_values).map { |v| v.respond_to?(:id) ? v.id.to_s : v.to_s } + + assign_new_values(custom_field_id, existing_cv_by_value, new_values) + delete_obsolete_custom_values(existing_cv_by_value, new_values) + handle_minimum_custom_value(custom_field_id, existing_cv_by_value, new_values) + end + def assign_new_values(custom_field_id, existing_cv_by_value, new_values) (new_values - existing_cv_by_value.keys).each do |new_value| add_custom_value(custom_field_id, new_value)