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
support easy usage of update method with implicit custom field activation
Jonas Jabarie9362507f87a
diff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rbindex b95a2f0bebf..d65e112170b 100644--- a/app/models/projects/acts_as_customizable_patches.rb+++ b/app/models/projects/acts_as_customizable_patches.rb@@ -29,7 +29,7 @@module Projects::ActsAsCustomizablePatchesextend ActiveSupport::Concern- attr_accessor :_limit_custom_fields_validation_to_section_id+ attr_accessor :_limit_custom_fields_validation_to_section_id, :_query_available_custom_fields_on_global_level# attr_accessor :_limit_custom_fields_validation_to_field_id# not needed for now, but might be relevant if we want to have edit dialogs just for one custom field@@ -104,9 +104,14 @@ module Projects::ActsAsCustomizablePatches# overrides acts_as_customizable# in contrast to acts_as_customizable, custom_fields are enabled per project# thus we need to check the project_custom_field_project_mappings- ProjectCustomField+ custom_fields = ProjectCustomField.includes(:project_custom_field_section)- .where(id: active_custom_field_ids_of_project)++ unless _query_available_custom_fields_on_global_level+ custom_fields = custom_fields.where(id: active_custom_field_ids_of_project)+ end++ custom_fieldsenddef available_project_custom_fields_grouped_by_section@@ -160,5 +165,38 @@ module Projects::ActsAsCustomizablePatchestrue # validate all custom values if no specific section was specifiedendend++ # patching the update methods directly as rails before/after/around update hooks did not work in this context+ #+ # reason for the patch:+ # 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+ # _query_available_custom_fields_on_global_level is used within the patched `available_custom_fields` method+ #+ # TODO: this patch seems far from ideal, find better solution for this+ #+ def update(attributes)+ if attributes[:custom_field_values].present?+ self._query_available_custom_fields_on_global_level = true+ result = super(attributes)+ self._query_available_custom_fields_on_global_level = false++ result+ else+ super+ end+ end++ def update!(attributes)+ if attributes[:custom_field_values].present?+ self._query_available_custom_fields_on_global_level = true+ result = super(attributes)+ self._query_available_custom_fields_on_global_level = false++ result+ else+ super+ end+ endendenddiff --git a/spec/models/projects/customizable_spec.rb b/spec/models/projects/customizable_spec.rbindex 24b469db43d..afc3b7da7f8 100644--- a/spec/models/projects/customizable_spec.rb+++ b/spec/models/projects/customizable_spec.rb@@ -240,4 +240,47 @@ RSpec.describe Project, 'customizable' doendendend++ context 'when updating with custom field values' do+ let(:project) { create(:project) }++ shared_examples 'implicitly enabled and saved custom values' do+ it 'enables fields with provided values' do+ # list_custom_field is not provided, thus it should not be enabled+ expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))+ .to contain_exactly(text_custom_field.id, bool_custom_field.id)+ expect(project.project_custom_fields)+ .to contain_exactly(text_custom_field, bool_custom_field)+ end++ it 'saves the custom field values properly' do+ expect(project.custom_value_for(text_custom_field).typed_value)+ .to eq('foo')+ expect(project.custom_value_for(bool_custom_field).typed_value)+ .to be_truthy+ end+ end++ context 'with #update method' do+ before do+ project.update(custom_field_values: {+ text_custom_field.id => 'foo',+ bool_custom_field.id => true+ })+ end++ it_behaves_like 'implicitly enabled and saved custom values'+ end++ context 'with #update! method' do+ before do+ project.update!(custom_field_values: {+ text_custom_field.id => 'foo',+ bool_custom_field.id => true+ })+ end++ it_behaves_like 'implicitly enabled and saved custom values'+ end+ endend