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

support implicit activation of custom fields when using custom_field_values method

Jonas Jabari96f112e8cf06
diff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rb
index d65e112170b..1ca6560c4d7 100644
--- a/app/models/projects/acts_as_customizable_patches.rb
+++ b/app/models/projects/acts_as_customizable_patches.rb
@@ -40,8 +40,11 @@ module Projects::ActsAsCustomizablePatches
has_many :project_custom_fields, through: :project_custom_field_project_mappings, class_name: 'ProjectCustomField'
before_save :build_missing_project_custom_field_project_mappings
- after_save :reset_section_scoped_validation
+
+ after_save :reset_section_scoped_validation, :reset_query_available_custom_fields_on_global_level
+
before_create :reject_section_scoped_validation_for_creation
+
after_create :disable_custom_fields_with_empty_values
def build_missing_project_custom_field_project_mappings
@@ -63,6 +66,12 @@ module Projects::ActsAsCustomizablePatches
self._limit_custom_fields_validation_to_section_id = nil
end
+ def reset_query_available_custom_fields_on_global_level
+ # reset the query_available_custom_fields_on_global_level after saving
+ # in order not to silently carry this setting in this instance
+ self._query_available_custom_fields_on_global_level = nil
+ end
+
def reject_section_scoped_validation_for_creation
if _limit_custom_fields_validation_to_section_id.present?
raise ArgumentError,
@@ -198,5 +207,32 @@ module Projects::ActsAsCustomizablePatches
super
end
end
+
+ def custom_field_values=(values)
+ # overrides acts_as_customizable
+ # we need to query the available custom fields on a global level when updating custom field values
+ # in order to support implicit activation of custom fields when values are provided during an update
+ self._query_available_custom_fields_on_global_level = true
+ set_custom_field_values_method_from_acts_as_customizable_module(values)
+ end
+
+ # we cannot call super as the code in acts_as_customizable is shipped in a module
+ # thus copy and pasted the code from acts_as_customizable here
+ def set_custom_field_values_method_from_acts_as_customizable_module(values)
+ return unless values.is_a?(Hash) && values.any?
+
+ values.with_indifferent_access.each do |custom_field_id, val|
+ 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_values
+ end
+ end
+ end
end
end
diff --git a/spec/models/projects/customizable_spec.rb b/spec/models/projects/customizable_spec.rb
index afc3b7da7f8..605ac6006fe 100644
--- a/spec/models/projects/customizable_spec.rb
+++ b/spec/models/projects/customizable_spec.rb
@@ -135,7 +135,7 @@ RSpec.describe Project, 'customizable' do
.to eq('foo')
expect(project.custom_value_for(bool_custom_field).typed_value)
.to be_truthy
- expect(project.custom_value_for(list_custom_field))
+ expect(project.custom_value_for(list_custom_field).typed_value)
.to be_nil
end
@@ -282,5 +282,18 @@ RSpec.describe Project, 'customizable' do
it_behaves_like 'implicitly enabled and saved custom values'
end
+
+ context 'with #custom_field_values= method' do
+ before do
+ project.custom_field_values = {
+ text_custom_field.id => 'foo',
+ bool_custom_field.id => true
+ }
+
+ project.save!
+ end
+
+ it_behaves_like 'implicitly enabled and saved custom values'
+ end
end
end