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
added more project model customizable specs
Jonas Jabariefb30cd1466d
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rbindex 49e55622adc..7b0ec44ca03 100644--- a/app/contracts/projects/base_contract.rb+++ b/app/contracts/projects/base_contract.rb@@ -50,8 +50,8 @@ module Projectsvalidate_templated_set_by_adminend- attribute :limit_custom_fields_validation_to_section_id- # `limit_custom_fields_validation_to_section_id` used in Projects::ActsAsCustomizablePatches in order to+ attribute :_limit_custom_fields_validation_to_section_id+ # `_limit_custom_fields_validation_to_section_id` used in Projects::ActsAsCustomizablePatches in order to# only validate custom fields of the touched sectionvalidate :validate_user_allowed_to_managediff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rbindex 5d5cdbc9e03..ae58a3aa8a3 100644--- a/app/models/projects/acts_as_customizable_patches.rb+++ b/app/models/projects/acts_as_customizable_patches.rb@@ -29,9 +29,9 @@module Projects::ActsAsCustomizablePatchesextend ActiveSupport::Concern- attr_accessor :limit_custom_fields_validation_to_section_id+ attr_accessor :_limit_custom_fields_validation_to_section_id- # attr_accessor :limit_custom_fields_validation_to_field_id+ # 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 fieldincluded do@@ -40,6 +40,8 @@ module Projects::ActsAsCustomizablePatcheshas_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_scope+ before_create :reject_section_scoped_validation_for_creationafter_create :disable_custom_fields_with_empty_valuesdef build_missing_project_custom_field_project_mappings@@ -55,6 +57,19 @@ module Projects::ActsAsCustomizablePatchesproject_custom_field_project_mappings.build(mappings)end+ def reset_section_scope+ # reset the section scope after saving+ # in order not to silently carry this setting in this instance+ self._limit_custom_fields_validation_to_section_id = nil+ end++ def reject_section_scoped_validation_for_creation+ if _limit_custom_fields_validation_to_section_id.present?+ raise ArgumentError,+ 'Section scoped validation is not supported for project creation, only for project updates'+ end+ end+def disable_custom_fields_with_empty_values# run only on initial creation! (otherwise we would deactivate custom fields with empty values on every update!)#@@ -77,6 +92,11 @@ module Projects::ActsAsCustomizablePatchesProjectCustomField.pluck(:id)elseproject_custom_field_project_mappings.pluck(:custom_field_id)+ .concat(ProjectCustomField.required.pluck(:id))+ .uniq+ # if for whatever reason a required custom field is not activated for this instance,+ # we need to make sure it's treated as activated especially in context of the validation+ # relevant when a project is created with a section scopedendend@@ -134,8 +154,8 @@ module Projects::ActsAsCustomizablePatchesenddef of_specified_custom_field_section?(custom_value)- if limit_custom_fields_validation_to_section_id.present?- custom_field_section_ids[custom_value.custom_field_id] == limit_custom_fields_validation_to_section_id+ if _limit_custom_fields_validation_to_section_id.present?+ custom_field_section_ids[custom_value.custom_field_id] == _limit_custom_fields_validation_to_section_idelsetrue # validate all custom values if no specific section was specifiedenddiff --git a/modules/overviews/app/controllers/overviews/overviews_controller.rb b/modules/overviews/app/controllers/overviews/overviews_controller.rbindex a4626350a2f..088790c5993 100644--- a/modules/overviews/app/controllers/overviews/overviews_controller.rb+++ b/modules/overviews/app/controllers/overviews/overviews_controller.rb@@ -31,7 +31,7 @@ module ::Overviews).call(permitted_params.project.merge(- limit_custom_fields_validation_to_section_id: section.id+ _limit_custom_fields_validation_to_section_id: section.id))diff --git a/spec/models/projects/customizable_spec.rb b/spec/models/projects/customizable_spec.rbindex 0d9faf3fb06..24b469db43d 100644--- a/spec/models/projects/customizable_spec.rb+++ b/spec/models/projects/customizable_spec.rb@@ -28,9 +28,17 @@require 'spec_helper'RSpec.describe Project, 'customizable' do- let!(:bool_custom_field) { create(:boolean_project_custom_field) }- let!(:text_custom_field) { create(:text_project_custom_field) }- let!(:list_custom_field) { create(:list_project_custom_field) }+ let!(:section) { create(:project_custom_field_section) }++ let!(:bool_custom_field) do+ create(:boolean_project_custom_field, project_custom_field_section: section)+ end+ let!(:text_custom_field) do+ create(:text_project_custom_field, project_custom_field_section: section)+ end+ let!(:list_custom_field) do+ create(:list_project_custom_field, project_custom_field_section: section)+ endcontext 'when not persisted' dolet(:project) { build(:project) }@@ -146,4 +154,90 @@ RSpec.describe Project, 'customizable' doendendend++ context 'when creating with custom field values' do+ let(:project) do+ create(:project, custom_field_values: {+ text_custom_field.id => 'foo',+ bool_custom_field.id => true+ })+ 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++ it 'enables fields with provided values and disables fields with none' 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++ context 'with correct validation' do+ let(:another_section) { create(:project_custom_field_section) }++ let!(:required_text_custom_field) do+ create(:text_project_custom_field,+ is_required: true,+ project_custom_field_section: another_section)+ end++ it 'validates all custom values if not scoped to a section' do+ project = build(:project, custom_field_values: {+ text_custom_field.id => 'foo',+ bool_custom_field.id => true+ })++ expect(project).not_to be_valid++ expect { project.save! }.to raise_error(ActiveRecord::RecordInvalid)+ end++ it 'rejects section validation scoping for project creation' do+ project = build(:project, custom_field_values: {+ text_custom_field.id => 'foo',+ bool_custom_field.id => true+ },+ _limit_custom_fields_validation_to_section_id: section.id)++ expect { project.save! }.to raise_error(ArgumentError)+ end++ it 'temporarly validates only custom values of a section if section scope is provided while updating' do+ project = create(:project, custom_field_values: {+ text_custom_field.id => 'foo',+ bool_custom_field.id => true,+ required_text_custom_field.id => 'bar'+ })++ expect(project).to be_valid++ # after a project is created, a new require custom field is added+ # which gets automatically activated for all projects+ create(:text_project_custom_field,+ is_required: true,+ project_custom_field_section: another_section)++ # thus, the project is invalid in total+ expect(project.reload).not_to be_valid+ expect { project.save! }.to raise_error(ActiveRecord::RecordInvalid)++ # but we still want to allow updating other sections without invalid required custom field values+ # by limiting the validation scope to a section temporarily+ project._limit_custom_fields_validation_to_section_id = section.id++ expect(project).to be_valid++ expect { project.save! }.not_to raise_error++ # section scope is resetted after each update+ expect { project.save! }.to raise_error(ActiveRecord::RecordInvalid)+ end+ end+ endend