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

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.rb
index 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::Concerns
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 } }
+ .map { |custom_field_id| { custom_field_id: } }
project.project_custom_field_project_mappings.build(mappings)
end
diff --git a/spec/models/projects/customizable_spec.rb b/spec/models/projects/customizable_spec.rb
index 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" do
expect { project.save! }.to raise_error(ActiveRecord::RecordInvalid)
end
end
-
- 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
- end
end
context "when updating with custom field values" do
diff --git a/spec/services/projects/create_service_spec.rb b/spec/services/projects/create_service_spec.rb
index 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 do
create(:text_project_custom_field, project_custom_field_section: section, visible: false)
end
let(: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 do
User.current = user
end
+ 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"
+ } }
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))
@@ -133,11 +175,9 @@ RSpec.describe Projects::CreateService, type: :model do
end
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)
+ let(:project_attributes) do
+ { custom_field_values: { text_custom_field.id => "foo" },
+ _limit_custom_fields_validation_to_section_id: section.id }
end
it "rejects section validation scoping for project creation" do