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 adding new custom fields to existing types
Dombi Attila3807c2e600ff
diff --git a/app/services/base_type_service.rb b/app/services/base_type_service.rbindex aaeefaa731f..1348c8af391 100644--- a/app/services/base_type_service.rb+++ b/app/services/base_type_service.rb@@ -47,6 +47,14 @@ class BaseTypeServiceprivatedef update(params, options)+ set_params_and_validate(params, options)+ rescue StandardError => e+ ServiceResult.failure.tap do |result|+ result.errors.add(:base, e.message)+ end+ end++ def set_params_and_validate(params, options)success = falseerrors = type.errors@@ -72,10 +80,6 @@ class BaseTypeServiceServiceResult.new(success:,errors:,result: type)- rescue StandardError => e- ServiceResult.failure.tap do |result|- result.errors.add(:base, e.message)- endenddef set_scalar_params(params)@@ -147,16 +151,23 @@ class BaseTypeService# for this type. If a custom field is not in a group, it is removed from the# custom_field_ids list.def set_active_custom_fields- active_cf_ids = []+ new_cf_ids_to_add = active_custom_field_ids - type.custom_field_ids+ type.custom_field_ids = active_custom_field_ids+ type.projects.each { |p| p.work_package_custom_field_ids |= new_cf_ids_to_add }+ end++ def active_custom_field_ids+ @active_custom_field_ids ||= begin+ active_cf_ids = []- type.attribute_groups.each do |group|- group.members.each do |attribute|- if CustomField.custom_field_attribute? attribute- active_cf_ids << attribute.gsub(/^custom_field_/, '').to_i+ type.attribute_groups.each do |group|+ group.members.each do |attribute|+ if CustomField.custom_field_attribute? attribute+ active_cf_ids << attribute.gsub(/^custom_field_/, '').to_i+ endendend+ active_cf_ids.uniqend-- type.custom_field_ids = active_cf_ids.uniqendenddiff --git a/spec/services/shared_type_service.rb b/spec/services/shared_type_service.rbindex e03e9a93ffd..a8feaf1c904 100644--- a/spec/services/shared_type_service.rb+++ b/spec/services/shared_type_service.rb@@ -26,6 +26,23 @@# See COPYRIGHT and LICENSE files for more details.#+++shared_context 'with custom field params' do+ let(:cf1) { create :work_package_custom_field, field_format: 'text' }+ let(:cf2) { create :work_package_custom_field, field_format: 'text' }+ let(:params) do+ {+ attribute_groups: [+ { 'type' => 'attribute',+ 'name' => 'group1',+ 'attributes' => [{ 'key' => "custom_field_#{cf1.id}" }, { 'key' => "custom_field_#{cf2.id}" }] },+ { 'type' => 'attribute',+ 'name' => 'groups',+ 'attributes' => [{ 'key' => "custom_field_#{cf2.id}" }] }+ ]+ }+ end+end+shared_examples_for 'type service' dolet(:success) { true }let(:params) { {} }@@ -42,7 +59,7 @@ shared_examples_for 'type service' doendit 'yields the block with success' do- expect(service_call { |call| call.success? }).to be_truthy+ expect(service_call(&:success?)).to be_truthyenddescribe 'with attributes' do@@ -56,15 +73,19 @@ shared_examples_for 'type service' doenddescribe 'attribute groups' do+ before do+ allow(type).to receive(:reset_attribute_groups)+ allow(type).to receive(:attribute_groups=)+ end+context 'when not given' dolet(:params) { { name: 'blubs blubs' } }it 'set the values provided on the call' do- expect(type).not_to receive(:reset_attribute_groups)- expect(type).not_to receive(:attribute_groups=)-service_call+ expect(type).not_to have_received(:reset_attribute_groups)+ expect(type).not_to have_received(:attribute_groups=)expect(type.name).to eql params[:name]endend@@ -73,10 +94,10 @@ shared_examples_for 'type service' dolet(:params) { { attribute_groups: [] } }it 'set the values provided on the call' do- expect(type).to receive(:reset_attribute_groups)- expect(type).not_to receive(:attribute_groups=)-service_call++ expect(type).to have_received(:reset_attribute_groups)+ expect(type).not_to have_received(:attribute_groups=)endend@@ -84,40 +105,42 @@ shared_examples_for 'type service' dolet(:params) { { attribute_groups: [{ 'type' => 'attribute', 'name' => 'foo', 'attributes' => [] }] } }it 'set the values provided on the call' do- expect(type).not_to receive(:reset_attribute_groups)- expect(type).to receive(:attribute_groups=)-service_call++ expect(type).not_to have_received(:reset_attribute_groups)+ expect(type).to have_received(:attribute_groups=)endendenddescribe 'custom fields' do- let(:cf1) { create :work_package_custom_field, field_format: 'text' }- let(:cf2) { create :work_package_custom_field, field_format: 'text' }- let(:params) do- {- attribute_groups: [- { 'type' => 'attribute',- 'name' => 'group1',- 'attributes' => [{ 'key' => "custom_field_#{cf1.id}" }, { 'key' => 'custom_field_54' }] },- { 'type' => 'attribute',- 'name' => 'groups',- 'attributes' => [{ 'key' => "custom_field_#{cf2.id}" }] }- ]- }- end+ include_context 'with custom field params'it 'enables the custom fields that are passed via attribute_groups' doallow(type).to receive(:work_package_attributes).and_return("custom_field_#{cf1.id}" => {}, "custom_field_#{cf2.id}" => {})- expect(type)+ allow(type).to receive(:custom_field_ids=).with([cf1.id, cf2.id])service_call++ expect(type).to have_received(:custom_field_ids=)+ end++ context 'when the type is associated with projects' do+ before do+ type.projects = create_list :project, 2+ end++ it 'enables the custom fields in the projects' do+ expect { service_call }+ .to change { Project.where(id: type.project_ids).map(&:work_package_custom_fields) }+ .from([[], []])+ .to([[cf1, cf2], [cf1, cf2]])+ endendend@@ -141,7 +164,7 @@ shared_examples_for 'type service' do.with(name: "Embedded table: group1").and_return(query)- parse_service = double('ParseQueryParamsService')+ parse_service = instance_double(::API::V3::ParseQueryParamsService)allow(::API::V3::UpdateQueryFromV3ParamsService).to receive(:new).with(query, user)@@ -179,7 +202,7 @@ shared_examples_for 'type service' doendend- context 'on failure' do+ describe 'on failure' dolet(:success) { false }let(:params) { { name: nil } }@@ -193,6 +216,21 @@ shared_examples_for 'type service' dotype.name = nilexpect(subject.errors.symbols_for(:name)).to include :blankend++ describe 'custom fields' do+ include_context 'with custom field params'++ context 'when the type is associated with projects' do+ before do+ type.projects = create_list :project, 2+ end++ it 'does not changes project custom fields' do+ expect { service_call }+ .not_to change { Project.where(id: type.project_ids).map(&:work_package_custom_field_ids) }+ end+ end+ endendendend