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 specs for activating custom fields with default values to Projects::CreateContract
Dombi Attila2fd2fb897b8b
diff --git a/app/services/projects/concerns/new_project_service.rb b/app/services/projects/concerns/new_project_service.rbindex cf8ba4b497f..53b0a392c75 100644--- a/app/services/projects/concerns/new_project_service.rb+++ b/app/services/projects/concerns/new_project_service.rb@@ -124,7 +124,7 @@ module Projects::Concernsactivated_custom_field_ids = project.project_custom_field_project_mappings.pluck(:custom_field_id).uniqmappings = (custom_field_ids - activated_custom_field_ids).uniq- .map { |pcf_id| { project_id: project.id, custom_field_id: pcf_id } }+ .map { |custom_field_id| { custom_field_id: } }project.project_custom_field_project_mappings.build(mappings)enddiff --git a/spec/models/projects/customizable_spec.rb b/spec/models/projects/customizable_spec.rbindex 9ac961dab43..2dffb1136d2 100644--- a/spec/models/projects/customizable_spec.rb+++ b/spec/models/projects/customizable_spec.rb@@ -221,33 +221,6 @@ RSpec.describe Project, "customizable" doexpect { project.save! }.to raise_error(ActiveRecord::RecordInvalid)endend-- context "with correct handling of custom fields with default values" do- let!(:text_custom_field_with_default) do- create(:text_project_custom_field,- default_value: "default",- project_custom_field_section: section)- end-- it "activates custom fields with default values if not explicitly set to blank" do- project = create(:project, custom_field_values: {- text_custom_field.id => "foo",- bool_custom_field.id => true- })- expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))- .to contain_exactly(text_custom_field.id, bool_custom_field.id, text_custom_field_with_default.id)- end-- it "does not activate custom fields with default values if explicitly set to blank" do- project = create(:project, custom_field_values: {- text_custom_field.id => "foo",- bool_custom_field.id => true,- text_custom_field_with_default.id => ""- })- expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))- .to contain_exactly(text_custom_field.id, bool_custom_field.id)- end- endendcontext "when updating with custom field values" dodiff --git a/spec/services/projects/create_service_spec.rb b/spec/services/projects/create_service_spec.rbindex 4c277922d81..42eea35d9ea 100644--- a/spec/services/projects/create_service_spec.rb+++ b/spec/services/projects/create_service_spec.rb@@ -87,24 +87,66 @@ RSpec.describe Projects::CreateService, type: :model docreate(:text_project_custom_field, project_custom_field_section: section, visible: false)endlet(:project) { subject.result }+ let(:project_attributes) { {} }+ let(:call_attributes) do+ attributes_for(:project, project_attributes).except(:created_at, :updated_at)+ end++ let(:user) { build_stubbed(:admin) }before doUser.current = userend+ context "with correct handling of custom fields with default values" do+ let!(:text_custom_field_with_default) do+ create(:text_project_custom_field,+ default_value: "default",+ project_custom_field_section: section)+ end++ context "if the default value is not explicitly set to blank" do+ let(:project_attributes) do+ { custom_field_values: {+ text_custom_field.id => "foo",+ bool_custom_field.id => true+ } }+ end++ it "activates custom fields with default values" do+ subject+ expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))+ .to contain_exactly(text_custom_field.id, bool_custom_field.id, text_custom_field_with_default.id)+ end+ end++ context "if the default value is explicitly set to blank" do+ let(:project_attributes) do+ { custom_field_values: {+ text_custom_field.id => "foo",+ bool_custom_field.id => true,+ text_custom_field_with_default.id => ""+ } }+ end++ it "does not activate custom fields with default values" do+ subject+ expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))+ .to contain_exactly(text_custom_field.id, bool_custom_field.id)+ end+ end+ end+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)+ let(:project_attributes) do+ { custom_field_values: {+ text_custom_field.id => "foo",+ bool_custom_field.id => true,+ hidden_custom_field.id => "hidden"+ } }endcontext "with admin permission" do- let(:user) { build_stubbed(:admin) }-it "does activate hidden custom fields" dosubjectexpect(project.project_custom_field_project_mappings.pluck(:custom_field_id))@@ -133,11 +175,9 @@ RSpec.describe Projects::CreateService, type: :model doendcontext "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)+ let(:project_attributes) do+ { custom_field_values: { text_custom_field.id => "foo" },+ _limit_custom_fields_validation_to_section_id: section.id }endit "rejects section validation scoping for project creation" do