OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
fix touching the customizable on removing custom_values
ulferts8dfc5e880326
diff --git a/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rbindex 98ad1099273..d0f11347119 100644--- a/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb+++ b/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb@@ -90,42 +90,15 @@ module Redminereturn unless values.is_a?(Hash) && values.any?values.with_indifferent_access.each do |custom_field_id, val|- existing_custom_values = custom_values_for_custom_field id: custom_field_id- new_values = Array(val)-- unless existing_custom_values.empty?- assign_new_values! custom_field_id, existing_custom_values, new_values- delete_obsolete_custom_values! existing_custom_values, new_values- end- end- end-- def assign_new_values!(custom_field_id, existing_custom_values, new_values)- new_values.flatten.zip(existing_custom_values).each do |new_value, custom_value|- if custom_value.nil?- new_custom_value = custom_values.build(- customized: self, custom_field_id: custom_field_id, value: new_value- )-- custom_field_values.push(new_custom_value)- else- custom_value.value = new_value- end- end- end-- def delete_obsolete_custom_values!(existing_custom_values, new_values)- existing_custom_values.zip(new_values).each_with_index do |(custom_value, new_value), i|- if new_value.nil?- if i.zero?- # leave the first value but set it to nil as that's the behaviour expected- # by the original acts_as_customizable- custom_value.value = nil- else- custom_value.destroy- custom_field_values.delete custom_value- custom_values.delete custom_value- end+ existing_cv_by_value = custom_values_for_custom_field(id: custom_field_id)+ .group_by(&:value)+ .transform_values(&:first)+ new_values = Array(val).map { |v| v.respond_to?(:id) ? v.id.to_s : v.to_s }++ 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_valuesendendend@@ -198,12 +171,19 @@ module Redmineself.custom_values = custom_field_valuesend+ def reload(*args)+ reset_custom_values_change_tracker++ super+ end+def reset_custom_values_change_tracker@custom_field_values = nil+ self.custom_value_destroyed = falseenddef reset_custom_values!- @custom_field_values = nil+ reset_custom_values_change_trackercustom_values.each { |cv| cv.destroy unless custom_field_values.include?(cv) }end@@ -271,6 +251,10 @@ module Redmineendend+ protected++ attr_accessor :custom_value_destroyed+privatedef for_custom_field_accessor(method_symbol)@@ -314,8 +298,54 @@ module Redmineendend+ # Explicitly touch the customizable if+ # there where only changes to custom_values (added or removed).+ # Particularly important for caching.def touch_customizable- touch if !saved_changes? && custom_values.loaded? && custom_values.any?(&:saved_changes?)+ touch if !saved_changes? && custom_values.loaded? && (custom_values.any?(&:saved_changes?) || custom_value_destroyed)+ 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)+ end+ end++ def delete_obsolete_custom_values(existing_cv_by_value, new_values)+ (existing_cv_by_value.keys - new_values).each do |obsolete_value|+ next if obsolete_value.nil?++ custom_value = existing_cv_by_value[obsolete_value]++ remove_custom_value(custom_value)+ end+ end++ # The original acts_as_customizable ensured to always have a custom value+ # for every custom field. If no value was set, the custom value would have the value of nil+ def handle_minimum_custom_value(custom_field_id, existing_cv_by_value, new_values)+ nil_value = existing_cv_by_value[nil]++ if new_values.any?+ remove_custom_value(nil_value)+ elsif nil_value.nil?+ add_custom_value(custom_field_id, nil)+ end+ end++ def add_custom_value(custom_field_id, value)+ new_custom_value = custom_values.build(custom_field_id: custom_field_id,+ value: value)++ custom_field_values.push(new_custom_value)+ end++ def remove_custom_value(custom_value)+ return unless custom_value++ custom_value.mark_for_destruction+ custom_field_values.delete custom_value+ self.custom_value_destroyed = trueendmodule ClassMethodsdiff --git a/spec/features/custom_fields/multi_value_custom_field_spec.rb b/spec/features/custom_fields/multi_value_custom_field_spec.rbindex d2afe78f163..c9fd2e9b0ae 100644--- a/spec/features/custom_fields/multi_value_custom_field_spec.rb+++ b/spec/features/custom_fields/multi_value_custom_field_spec.rb@@ -141,6 +141,8 @@ describe "multi select custom values", clear_cache: true, js: true dowp1_field.submit_by_dashboard+ wp_page.expect_and_dismiss_notification message: 'Successful update'+# Expect changed groupsexpect(page).to have_selector('.group--value .count', count: 1)expect(page).to have_selector('.group--value', text: 'ham (2)')@@ -153,6 +155,8 @@ describe "multi select custom values", clear_cache: true, js: true dofield.unset_value "ham", truefield.submit_by_dashboard+ wp_page.expect_and_dismiss_notification message: 'Successful update'+# Expect none selected in split and tablefield.expect_state_text '-'wp1_field.expect_state_text '-'diff --git a/spec/models/work_package/work_package_custom_fields_spec.rb b/spec/models/work_package/work_package_custom_fields_spec.rbindex 211d3099cbe..2bd5122045b 100644--- a/spec/models/work_package/work_package_custom_fields_spec.rb+++ b/spec/models/work_package/work_package_custom_fields_spec.rb@@ -57,11 +57,11 @@ describe WorkPackage, type: :model doendbefore do- def self.change_custom_field_value(work_package, value)+ def self.change_custom_field_value(work_package, value, save: true)val = custom_field.custom_options.find { |co| co.value == value }.try(:id)work_package.custom_field_values = { custom_field.id => (val || value) }- work_package.save+ work_package.save if saveendend@@ -225,28 +225,22 @@ describe WorkPackage, type: :model doendcontext 'save' do- before do- work_package.save!- work_package.reload- end-subject { work_package.typed_custom_value_for(custom_field.id) }it { is_expected.to eq('PostgreSQL') }end+ end- describe 'value change' do- before do- change_custom_field_value(work_package, 'PostgreSQL')- @initial_custom_value = work_package.custom_value_for(custom_field).id- change_custom_field_value(work_package, 'MySQL')+ describe 'only updates when actually saving' do+ before do+ change_custom_field_value(work_package, 'PostgreSQL')+ change_custom_field_value(work_package, 'MySQL', save: false)- work_package.reload- end+ work_package.reload+ end- subject { work_package.custom_value_for(custom_field).id }+ subject { work_package.typed_custom_value_for(custom_field.id) }- it { is_expected.to eq(@initial_custom_value) }- end+ it { is_expected.to eql('PostgreSQL') }endenddiff --git a/spec/models/work_package/work_package_multi_value_custom_fields_spec.rb b/spec/models/work_package/work_package_multi_value_custom_fields_spec.rbindex 24a510b7455..fdf760b2d76 100644--- a/spec/models/work_package/work_package_multi_value_custom_fields_spec.rb+++ b/spec/models/work_package/work_package_multi_value_custom_fields_spec.rb@@ -44,9 +44,11 @@ describe WorkPackage, type: :model doendlet(:custom_values) do- ["ham", "onions", "pineapple"].map do |str|- custom_field.custom_options.find { |co| co.value == str }.try(:id)- end+ custom_field+ .custom_options+ .where(value: ["ham", "onions", "pineapple"])+ .pluck(:id)+ .map(&:to_s)endlet(:work_package) do@@ -63,7 +65,7 @@ describe WorkPackage, type: :model dolet(:typed_values) { work_package.typed_custom_value_for(custom_field.id) }it 'returns the properly typed values' do- expect(values.map { |cv| cv.value.to_i }).to eq(custom_values)+ expect(values.map { |cv| cv.value }).to eq(custom_values)expect(typed_values).to eq(%w(ham onions pineapple))end@@ -75,4 +77,63 @@ describe WorkPackage, type: :model doexpect(typed_values).to eq(nil)endend++ describe 'setting and reading values' do+ shared_examples_for 'custom field values updates' do+ before do+ # Reload to reset i.e. the saved_changes filter on custom_values+ work_package.reload+ end++ it 'touches the work_package' do+ expect do+ work_package.custom_field_values = { custom_field.id => ids }+ work_package.save+ end+ .to(change { work_package.lock_version })+ end++ it 'sets the values' do+ work_package.custom_field_values = { custom_field.id => ids }+ work_package.save++ expect(work_package.send("custom_field_#{custom_field.id}"))+ .to eql values+ end+ end++ context 'when removing some custom values' do+ it_behaves_like 'custom field values updates' do+ let(:ids) { [custom_values.first.to_s] }+ let(:values) { ['ham'] }+ end+ end++ context 'when removing all custom values' do+ it_behaves_like 'custom field values updates' do+ let(:ids) { [] }+ let(:values) { [nil] }+ end+ end++ context 'when adding values' do+ it_behaves_like 'custom field values updates' do+ let(:ids) do+ CustomOption.where(value: ["ham", "onions", "pineapple", "mushrooms"]).pluck(:id).map(&:to_s)+ end+ let(:values) { ["ham", "onions", "pineapple", "mushrooms"] }+ end+ end++ context 'when first having no values and then adding some' do+ let(:custom_values) { [] }++ it_behaves_like 'custom field values updates' do+ let(:ids) do+ CustomOption.where(value: ["ham", "mushrooms"]).pluck(:id).map(&:to_s)+ end+ let(:values) { ["ham", "mushrooms"] }+ end+ end+ endend