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
Move section based validation check to the Projects::CreateService
Dombi Attila3640a74485df
diff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rbindex 6b1dc672129..dec980b246f 100644--- a/app/models/projects/acts_as_customizable_patches.rb+++ b/app/models/projects/acts_as_customizable_patches.rb@@ -41,23 +41,8 @@ module Projects::ActsAsCustomizablePatchesbefore_update :set_query_available_custom_fields_to_global_level- before_create :build_missing_project_custom_field_project_mappings-after_save :reset_section_scoped_validation- def build_missing_project_custom_field_project_mappings- # activate custom fields for this project (via mapping table) if values have been provided for custom_fields but no mapping exists- custom_field_ids = project.custom_values- .select { |cv| cv.value.present? }- .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- .map { |pcf_id| { project_id: id, custom_field_id: pcf_id } }-- project_custom_field_project_mappings.build(mappings)- end-def reset_section_scoped_validation# reset the section scope after saving# in order not to silently carry this setting in this instancediff --git a/app/services/projects/concerns/new_project_service.rb b/app/services/projects/concerns/new_project_service.rbindex eb7a6cc2c2d..cf8ba4b497f 100644--- a/app/services/projects/concerns/new_project_service.rb+++ b/app/services/projects/concerns/new_project_service.rb@@ -32,21 +32,30 @@ module Projects::Concernsprivate- def after_perform(attributes_call)- new_project = attributes_call.result-- set_default_role(new_project) unless user.admin?- disable_custom_fields_with_empty_values(new_project)- notify_project_created(new_project)-- super+ def before_perform(params, service_call)+ # we need to reset the query_available_custom_fields_on_global_level already after validation+ # as the update service just calls .valid? and returns if invalid+ # after_save is not touched in this case which causes the flag to stay active+ super.tap do |super_call|+ reject_section_scoped_validation(super_call.result)+ endenddef after_validate(params, service_call)# we need to reset the query_available_custom_fields_on_global_level already after validation# as the update service just calls .valid? and returns if invalid# after_save is not touched in this case which causes the flag to stay active- reject_section_scoped_validation(service_call.result)+ super.tap do |super_call|+ build_missing_project_custom_field_project_mappings(super_call.result)+ end+ end++ def after_perform(attributes_call)+ new_project = attributes_call.result++ set_default_role(new_project) unless user.admin?+ disable_custom_fields_with_empty_values(new_project)+ notify_project_created(new_project)superend@@ -81,9 +90,14 @@ module Projects::Concerns)end+ def reject_section_scoped_validation(new_project)+ if new_project._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(new_project)- # run only on initial creation! (otherwise we would deactivate custom fields with empty values on every update!)- ## ideally, `build_missing_project_custom_field_project_mappings` would not activate custom fields with empty values# but:# this hook is required as acts_as_customizable build custom values with their default value even if a blank value@@ -101,11 +115,18 @@ module Projects::Concerns.destroy_allend- def reject_section_scoped_validation(new_project)- if new_project._limit_custom_fields_validation_to_section_id.present?- raise ArgumentError,- "Section scoped validation is not supported for project creation, only for project updates"- end+ def build_missing_project_custom_field_project_mappings(project)+ # activate custom fields for this project (via mapping table) if values have been provided+ # for custom_fields but no mapping exists+ custom_field_ids = project.custom_values+ .select { |cv| cv.value.present? }+ .pluck(:custom_field_id).uniq+ activated_custom_field_ids = project.project_custom_field_project_mappings.pluck(:custom_field_id).uniq++ mappings = (custom_field_ids - activated_custom_field_ids).uniq+ .map { |pcf_id| { project_id: project.id, custom_field_id: pcf_id } }++ project.project_custom_field_project_mappings.build(mappings)endendenddiff --git a/spec/models/projects/customizable_spec.rb b/spec/models/projects/customizable_spec.rbindex 2f47c31bec8..9ac961dab43 100644--- a/spec/models/projects/customizable_spec.rb+++ b/spec/models/projects/customizable_spec.rb@@ -190,16 +190,6 @@ RSpec.describe Project, "customizable" doexpect { 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" doproject = create(:project, custom_field_values: {text_custom_field.id => "foo",diff --git a/spec/services/projects/create_service_spec.rb b/spec/services/projects/create_service_spec.rbindex b4dd2aa38e0..4c277922d81 100644--- a/spec/services/projects/create_service_spec.rb+++ b/spec/services/projects/create_service_spec.rb@@ -71,17 +71,8 @@ RSpec.describe Projects::CreateService, type: :model doendend- context "with hidden custom fields" do- # Skip stubbing in order to execute the real service call+ context "with a real service call" dolet(:stub_model_instance) { false }- let(:call_attributes) do- attributes_for(:project,- custom_field_values: {- text_custom_field.id => "foo",- bool_custom_field.id => true,- hidden_custom_field.id => "hidden"- }).except(:created_at, :updated_at)- endlet!(:section) { create(:project_custom_field_section) }let!(:bool_custom_field) docreate(:boolean_project_custom_field, project_custom_field_section: section)@@ -101,32 +92,56 @@ RSpec.describe Projects::CreateService, type: :model doUser.current = userend- context "with admin permission" do- let(:user) { build_stubbed(:admin) }+ context "with hidden custom fields" do+ let(:call_attributes) do+ attributes_for(:project,+ custom_field_values: {+ text_custom_field.id => "foo",+ bool_custom_field.id => true,+ hidden_custom_field.id => "hidden"+ }).except(:created_at, :updated_at)+ end++ context "with admin permission" do+ let(:user) { build_stubbed(:admin) }- it "does activate hidden custom fields" do- subject- expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))- .to contain_exactly(text_custom_field.id, bool_custom_field.id, hidden_custom_field.id)- expect(project.custom_value_for(hidden_custom_field).typed_value).to eq("hidden")+ it "does activate hidden custom fields" do+ subject+ expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))+ .to contain_exactly(text_custom_field.id, bool_custom_field.id, hidden_custom_field.id)+ expect(project.custom_value_for(hidden_custom_field).typed_value).to eq("hidden")+ endend- end- context "without admin permission" do- let(:user) { create(:user) }+ context "without admin permission" do+ let(:user) { create(:user) }- before do- mock_permissions_for(user) do |mock|- mock.allow_globally :add_project+ before do+ mock_permissions_for(user) do |mock|+ mock.allow_globally :add_project+ end+ end++ it "does not activate hidden custom fields" do+ subject++ expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))+ .to contain_exactly(text_custom_field.id, bool_custom_field.id)+ expect(project.custom_value_for(hidden_custom_field)).to be_nilendend+ end- it "does not activate hidden custom fields" do- subject+ context "with a section scoped validation" do+ let(:call_attributes) do+ attributes_for(:project,+ custom_field_values: { text_custom_field.id => "foo" },+ _limit_custom_fields_validation_to_section_id: section.id+ ).except(:created_at, :updated_at)+ end- expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))- .to contain_exactly(text_custom_field.id, bool_custom_field.id)- expect(project.custom_value_for(hidden_custom_field)).to be_nil+ it "rejects section validation scoping for project creation" do+ expect { subject }.to raise_error(ArgumentError)endendend