Skip to content

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

fixed not desired reactivation of custom fields which have been disabled in the past

Jonas Jabari33e40b39eeb1
diff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rb
index 0807d96779e..c544762f485 100644
--- a/app/models/projects/acts_as_customizable_patches.rb
+++ b/app/models/projects/acts_as_customizable_patches.rb
@@ -56,7 +56,10 @@ module Projects::ActsAsCustomizablePatches
# activate custom fields for this project (via mapping table) if values have been provided for custom_fields but no mapping exists
# current shortcommings:
# - boolean custom fields are always activated as a nil value is never provided (always true/false)
- custom_field_ids = project.custom_values.reject { |cv| cv.value.blank? }.pluck(:custom_field_id).uniq
+ custom_field_ids = project.custom_values
+ .reject { |cv| cv.value.blank? }
+ .reject { |cv| cv.persisted? } # do not reactivate custom fields which have already been used and disabled
+ .pluck(:custom_field_id).uniq
activated_custom_field_ids = project_custom_field_project_mappings.pluck(:custom_field_id).uniq
mappings = (custom_field_ids - activated_custom_field_ids).uniq
diff --git a/spec/models/projects/customizable_spec.rb b/spec/models/projects/customizable_spec.rb
index 153a5f3df59..6debf0516c0 100644
--- a/spec/models/projects/customizable_spec.rb
+++ b/spec/models/projects/customizable_spec.rb
@@ -295,6 +295,55 @@ RSpec.describe Project, 'customizable' do
it_behaves_like 'implicitly enabled and saved custom values'
end
+
+ it 'does not re-enable fields without new value which have been disabled in the past (regression)' do
+ project.update!(custom_field_values: {
+ text_custom_field.id => 'foo',
+ bool_custom_field.id => true
+ })
+
+ expect(project.reload.project_custom_fields)
+ .to contain_exactly(text_custom_field, bool_custom_field)
+
+ project.project_custom_field_project_mappings.find_by(custom_field_id: text_custom_field.id).destroy
+
+ expect(project.reload.project_custom_fields)
+ .to contain_exactly(bool_custom_field)
+
+ project.update!(custom_field_values: {
+ bool_custom_field.id => true
+ })
+
+ expect(project.reload.project_custom_fields)
+ .to contain_exactly(bool_custom_field)
+ end
+
+ it 'does re-enable fields with new value which have been disabled in the past' do
+ pending "this is currently not working, not sure if it should be supported at all"
+
+ project.update!(custom_field_values: {
+ text_custom_field.id => 'foo',
+ bool_custom_field.id => true
+ })
+
+ expect(project.reload.project_custom_fields)
+ .to contain_exactly(text_custom_field, bool_custom_field)
+
+ project.project_custom_field_project_mappings.find_by(custom_field_id: text_custom_field.id).destroy
+
+ expect(project.reload.project_custom_fields)
+ .to contain_exactly(bool_custom_field)
+
+ project.update!(custom_field_values: {
+ text_custom_field.id => 'bar'
+ })
+
+ expect(project.reload.project_custom_fields)
+ .to contain_exactly(text_custom_field, bool_custom_field)
+
+ expect(project.custom_value_for(text_custom_field).typed_value)
+ .to eq('bar')
+ end
end
context 'when updating with custom field setter methods (API approach)' do