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
Auto activate custom fields of default types when creating projects
Dombi Attila1c504209059c
diff --git a/app/services/projects/set_attributes_service.rb b/app/services/projects/set_attributes_service.rbindex c48d3333624..16818efb3c6 100644--- a/app/services/projects/set_attributes_service.rb+++ b/app/services/projects/set_attributes_service.rb@@ -47,6 +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_typesenddef set_default_public(provided)@@ -61,6 +62,10 @@ module Projectsmodel.types = ::Type.default if !provided && model.types.empty?end+ def set_default_active_work_pacakge_types+ model.work_package_custom_fields = WorkPackageCustomField.joins(:types).where(types: { id: model.type_ids })+ end+def update_status(attributes)with_hack_around_faulty_enum(attributes) do |safe_attributes|if model.status@@ -91,7 +96,7 @@ module Projectsenddef faulty_code?(attributes)- attributes && attributes[:code] && !Projects::Status.codes.keys.include?(attributes[:code].to_s)+ attributes && attributes[:code] && Projects::Status.codes.keys.exclude?(attributes[:code].to_s)enddef first_not_set_codediff --git a/spec/services/projects/set_attributes_service_spec.rb b/spec/services/projects/set_attributes_service_spec.rbindex f088def741f..8f2f18e4281 100644--- a/spec/services/projects/set_attributes_service_spec.rb+++ b/spec/services/projects/set_attributes_service_spec.rb@@ -28,11 +28,10 @@require 'spec_helper'-# rubocop:disable RSpec/NestedGroupsdescribe Projects::SetAttributesService, type: :model dolet(:user) { build_stubbed(:user) }let(:contract_class) do- contract = double('contract_class')+ contract = class_double(Projects::CreateContract)allow(contract).to receive(:new)@@ -42,11 +41,11 @@ describe Projects::SetAttributesService, type: :model docontractendlet(:contract_instance) do- double('contract_instance', validate: contract_valid, errors: contract_errors)+ instance_double(Projects::CreateContract, validate: contract_valid, errors: contract_errors)endlet(:contract_valid) { true }let(:contract_errors) do- double('contract_errors')+ instance_double(ActiveModel::Errors)endlet(:project_valid) { true }let(:instance) do@@ -70,7 +69,7 @@ describe Projects::SetAttributesService, type: :model do.to receive(:valid?).and_return(project_valid)- expect(contract_instance)+ allow(contract_instance).to receive(:validate).and_return(contract_valid)end@@ -78,7 +77,14 @@ describe Projects::SetAttributesService, type: :model dosubject { instance.call(call_attributes) }it 'is successful' do- expect(subject.success?).to be_truthy+ expect(subject).to be_success+ end++ it 'calls validation' do+ subject++ expect(contract_instance)+ .to have_received(:validate)endit 'sets the attributes' do@@ -89,10 +95,13 @@ describe Projects::SetAttributesService, type: :model doendit 'does not persist the project' do- expect(project)- .not_to receive(:save)+ allow(project)+ .to receive(:save)subject++ expect(project)+ .not_to have_received(:save)endcontext 'for a new record' do@@ -195,6 +204,17 @@ describe Projects::SetAttributesService, type: :model do.and_return default_typesend+ shared_examples "setting custom field defaults" do+ context 'with custom fields' do+ let!(:custom_field) { create :text_wp_custom_field, types: }++ it 'activates the type\'s custom fields' do+ expect(subject.result.work_package_custom_fields)+ .to eq([custom_field])+ end+ end+ end+context 'with a value for types provided' dolet(:call_attributes) do{@@ -206,6 +226,11 @@ describe Projects::SetAttributesService, type: :model doexpect(subject.result.types).to match_array other_typesend++ include_examples "setting custom field defaults" do+ let(:other_types) { [create(:type)] }+ let(:types) { other_types }+ endendcontext 'with no value for types provided' do@@ -213,17 +238,28 @@ describe Projects::SetAttributesService, type: :model doexpect(subject.result.types).to match_array default_typesend++ include_examples "setting custom field defaults" do+ let(:default_types) { [create(:type)] }+ let(:types) { default_types }+ endendcontext 'with the types being set before' do+ let(:types) { [build(:type, name: 'lorem')] }+before do- project.types.build(name: 'lorem')+ project.types = typesendit 'does not alter the types modules' doexpect(subject.result.types.map(&:name)).to match_array %w(lorem)end++ include_examples "setting custom field defaults" do+ let(:types) { [create(:type, name: 'lorem')] }+ endendend@@ -350,4 +386,3 @@ describe Projects::SetAttributesService, type: :model doendendend-# rubocop:enable RSpec/NestedGroups