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
Activate custom fields for projects when activating new types
Dombi Attilabdf8f1bb47e5
diff --git a/app/services/projects/set_attributes_service.rb b/app/services/projects/set_attributes_service.rbindex 16818efb3c6..a42e3ec8bfe 100644--- a/app/services/projects/set_attributes_service.rb+++ b/app/services/projects/set_attributes_service.rb@@ -47,7 +47,7 @@ module Projectsset_default_public(attribute_keys.include?('public'))set_default_module_names(attribute_keys.include?('enabled_module_names'))set_default_types(attribute_keys.include?('types') || attribute_keys.include?('type_ids'))- set_default_active_work_pacakge_types+ set_default_active_work_package_custom_fieldsenddef set_default_public(provided)@@ -62,7 +62,7 @@ module Projectsmodel.types = ::Type.default if !provided && model.types.empty?end- def set_default_active_work_pacakge_types+ def set_default_active_work_package_custom_fieldsmodel.work_package_custom_fields = WorkPackageCustomField.joins(:types).where(types: { id: model.type_ids })enddiff --git a/app/services/update_projects_types_service.rb b/app/services/update_projects_types_service.rbindex 4bef4087743..35a7c53d4c4 100644--- a/app/services/update_projects_types_service.rb+++ b/app/services/update_projects_types_service.rb@@ -28,7 +28,7 @@class UpdateProjectsTypesService < BaseProjectServicedef call(type_ids)- type_ids = standard_types if type_ids.nil? || type_ids.empty?+ type_ids = standard_types if type_ids.blank?if types_missing?(type_ids)project.errors.add(:types,@@ -36,7 +36,7 @@ class UpdateProjectsTypesService < BaseProjectServicetypes: missing_types(type_ids).map(&:name).join(', '))falseelse- project.type_ids = type_ids+ update_project_types(type_ids)trueend@@ -58,10 +58,16 @@ class UpdateProjectsTypesService < BaseProjectServiceenddef missing_types(type_ids)- types_used_by_work_packages.select { |t| !type_ids.include?(t.id) }+ types_used_by_work_packages.select { |t| type_ids.exclude?(t.id) }enddef types_used_by_work_packages@types_used_by_work_packages ||= project.types_used_by_work_packagesend++ def update_project_types(type_ids)+ new_types_to_add = type_ids - project.type_ids+ project.type_ids = type_ids+ project.work_package_custom_field_ids |= WorkPackageCustomField.joins(:types).where(types: { id: new_types_to_add }).ids+ endenddiff --git a/spec/services/projects/set_attributes_service_spec.rb b/spec/services/projects/set_attributes_service_spec.rbindex 8f2f18e4281..0ae0d4d69db 100644--- a/spec/services/projects/set_attributes_service_spec.rb+++ b/spec/services/projects/set_attributes_service_spec.rb@@ -207,6 +207,7 @@ describe Projects::SetAttributesService, type: :model doshared_examples "setting custom field defaults" docontext 'with custom fields' dolet!(:custom_field) { create :text_wp_custom_field, types: }+ let!(:custom_field_with_no_type) { create :text_wp_custom_field }it 'activates the type\'s custom fields' doexpect(subject.result.work_package_custom_fields)diff --git a/spec/services/update_projects_types_service_spec.rb b/spec/services/update_projects_types_service_spec.rbindex 0685c9594cc..ecfdc2310d8 100644--- a/spec/services/update_projects_types_service_spec.rb+++ b/spec/services/update_projects_types_service_spec.rb@@ -38,54 +38,97 @@ describe UpdateProjectsTypesService doenddescribe '.call' do+ subject { instance.call(ids) }+before doallow(project).to receive(:type_ids=)end+ shared_examples 'activating custom fields' do+ let(:project) { create :project, no_types: true }+ let!(:custom_field) { create :text_wp_custom_field, types: }++ it 'updates the active custom fields' do+ expect { subject }+ .to change { project.reload.work_package_custom_field_ids }+ .from([])+ .to([custom_field.id])+ end++ it 'does not activates the same custom field twice' do+ expect { subject }.to change { project.reload.work_package_custom_field_ids }+ expect { subject }.not_to change { project.reload.work_package_custom_field_ids }+ end++ context 'for a project with already existing types' do+ let(:project) { create :project, types:, work_package_custom_fields: [create(:text_wp_custom_field)] }++ it 'does not change custom fields' do+ expect { subject }.not_to change { project.reload.work_package_custom_field_ids }+ end+ end+ end+context 'with ids provided' dolet(:ids) { [1, 2, 3] }it 'returns true and updates the ids' do- expect(instance.call(ids)).to be_truthy+ expect(subject).to be_truthyexpect(project).to have_received(:type_ids=).with(ids)end++ include_examples 'activating custom fields' do+ let(:types) { create_list(:type, 2) }+ let(:ids) { types.collect(&:id) }+ endendcontext 'with no id passed' dolet(:ids) { [] }it 'adds the id of the default type and returns true' do- expect(instance.call(ids)).to be_truthy+ expect(subject).to be_truthyexpect(project).to have_received(:type_ids=).with([standard_type.id])end++ include_examples 'activating custom fields' do+ let(:standard_type) { create(:type_standard) }+ let(:types) { [standard_type] }+ endendcontext 'with nil passed' dolet(:ids) { nil }it 'adds the id of the default type and returns true' do- expect(instance.call(ids)).to be_truthy+ expect(subject).to be_truthyexpect(project).to have_received(:type_ids=).with([standard_type.id])end++ include_examples 'activating custom fields' do+ let(:standard_type) { create(:type_standard) }+ let(:types) { [standard_type] }+ endendcontext 'when the id of a type in use is not provided' dolet(:type) { build_stubbed(:type) }+ let(:ids) { [1] }before doallow(project).to receive(:types_used_by_work_packages).and_return([type])+ allow(project).to receive(:work_package_custom_field_ids=).and_return([type])endit 'returns false and sets an error message' do- ids = [1]-errors = instance_double(ActiveModel::Errors)allow(errors).to receive(:add)allow(project).to receive(:errors).and_return(errors)- expect(instance.call(ids)).to be_falsey+ expect(subject).to be_falseyexpect(errors).to have_received(:add).with(:types, :in_use_by_work_packages, types: type.name)expect(project).not_to have_received(:type_ids=)+ expect(project).not_to have_received(:work_package_custom_field_ids=)endendend