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
adjusted copy service to not activate project custom fields with default values which are disabled in source
Jonas Jabari0c3db8a08c81
diff --git a/app/services/projects/copy_service.rb b/app/services/projects/copy_service.rbindex 97863455514..02199b83b0c 100644--- a/app/services/projects/copy_service.rb+++ b/app/services/projects/copy_service.rb@@ -80,6 +80,27 @@ module Projectsendend+ def after_perform(call)+ remove_custom_fields_not_activated_in_source(call)++ super+ end++ def remove_custom_fields_not_activated_in_source(call)+ # TODO: seems a bit too hacky to me, find better solution+ # remove custom fields from target project which are not activated in source project+ # this is required in cases, when+ # a custom field with a default value exists but is not activated in the source project+ # this custom field is then not shown in the copy form (which is desired)+ # but the custom field would be activated in the target project with its default value (which is hard to prevent)+ # thus we clean them up here:+ custom_fields_activated_in_source = source.project_custom_fields.pluck(:id)+ custom_fields_activated_in_target = call.result.project_custom_fields.pluck(:id)++ custom_fields_to_remove = custom_fields_activated_in_target - custom_fields_activated_in_source+ call.result.project_custom_fields.where(id: custom_fields_to_remove).destroy_all+ end+def contract_options{ copy_source: source, validate_model: true }enddiff --git a/spec/features/projects/copy_spec.rb b/spec/features/projects/copy_spec.rbindex d15e877638c..db69f6c59ed 100644--- a/spec/features/projects/copy_spec.rb+++ b/spec/features/projects/copy_spec.rb@@ -245,16 +245,14 @@ RSpec.describe 'Projects copy', :js, :with_cuprite dolet!(:optional_boolean_project_custom_field_with_default) docreate(:boolean_project_custom_field, is_required: false, default_value: true, project_custom_field_section:)end+ let!(:optional_boolean_project_custom_field_with_no_default) do+ create(:boolean_project_custom_field, is_required: false, project_custom_field_section:)+ endlet!(:optional_string_project_custom_field_with_default) docreate(:string_project_custom_field, is_required: false, default_value: 'bar', project_custom_field_section:)end- # TBD: Is this intended from a conceptial point of view?- #- # If not, I don't know how to change this behavior while keeping the behavior specified in the creation spec where- # optional custom fields with default values are activated if the value is untouched in the form (which seems to be desired from- # a concpetional point of view)- it 'does enable optional project custom fields with default values although not enabled in source project' do+ it 'does not enable optional project custom fields with default values when not enabled in source project' do# the optional boolean and string fields are not activated in the source projectexpect(project.project_custom_field_ids).to contain_exactly(project_custom_field.id,@@ -268,17 +266,82 @@ RSpec.describe 'Projects copy', :js, :with_cuprite docopied_project = Project.find_by(name: 'Copied project')- # the optional boolean and string fields are activated in the copied project with their default values+ # the optional boolean and string fields are not activated in the target project, although they have a default value+ expect(copied_project.project_custom_field_ids).to contain_exactly(+ project_custom_field.id,+ optional_project_custom_field.id,+ optional_project_custom_field_with_default.id+ )+ end+ end+ end++ context 'with correct handling of invisible values' do+ let!(:invisible_field) do+ create(:string_project_custom_field, name: 'Text for Admins only',+ visible: false,+ project_custom_field_section:,+ projects: [project])+ end+ let!(:source_custom_value_for_invisible_field) do+ create(:custom_value, customized: project, custom_field: invisible_field, value: 'foo')+ end++ before do+ original_settings_page = Pages::Projects::Settings.new(project)+ original_settings_page.visit!++ find('.toolbar a', text: 'Copy').click++ expect(page).to have_text "Copy project \"#{project.name}\""++ fill_in 'Name', with: 'Copied project'+ click_on 'Advanced settings'+ end++ context 'with an admin user' do+ let(:user) { create(:admin) }++ it 'shows invisible fields in the form and allows their activation' do+ expect(page).to have_content 'Text for Admins only'++ # don't touch the source value++ click_button 'Save'++ wait_for_copy_to_finish++ copied_project = Project.find_by(name: 'Copied project')+expect(copied_project.project_custom_field_ids).to contain_exactly(project_custom_field.id,optional_project_custom_field.id,optional_project_custom_field_with_default.id,- optional_boolean_project_custom_field_with_default.id,- optional_string_project_custom_field_with_default.id+ invisible_field.id+ )++ expect(copied_project.custom_value_for(invisible_field).typed_value).to eq('foo')+ end+ end++ context 'with non-admin user' do+ # TBD: Not sure if this is the desired behavior, but would be a bit tricky to change+ it 'does not show invisible fields in the form and thus do not activate them' do+ expect(page).to have_no_content 'Text for Admins only'++ click_button 'Save'++ wait_for_copy_to_finish++ copied_project = Project.find_by(name: 'Copied project')++ expect(copied_project.project_custom_field_ids).to contain_exactly(+ project_custom_field.id,+ optional_project_custom_field.id,+ optional_project_custom_field_with_default.id)- expect(copied_project.custom_value_for(optional_boolean_project_custom_field_with_default).typed_value).to be_truthy- expect(copied_project.custom_value_for(optional_string_project_custom_field_with_default).typed_value).to eq('bar')+ expect(copied_project.custom_value_for(invisible_field)).to be_nilendendenddiff --git a/spec/services/projects/copy_service_integration_spec.rb b/spec/services/projects/copy_service_integration_spec.rbindex d48b613d147..c95a328eb85 100644--- a/spec/services/projects/copy_service_integration_spec.rb+++ b/spec/services/projects/copy_service_integration_spec.rb@@ -157,6 +157,8 @@ RSpec.describe(it 'copies the custom_field' doexpect(subject).to be_success+ expect(project_copy.project_custom_fields).to contain_exactly(user_custom_field)+cv = project_copy.custom_values.reload.find_by(custom_field: user_custom_field)expect(cv).to be_presentexpect(cv.value).to eq user_value.id.to_s@@ -177,6 +179,8 @@ RSpec.describe(it 'copies the custom_field' doexpect(subject).to be_success+ expect(project_copy.project_custom_fields).to contain_exactly(list_custom_field)+cv = project_copy.custom_values.reload.where(custom_field: list_custom_field).to_aexpect(cv).to be_a Arrayexpect(cv.count).to eq 2@@ -184,6 +188,17 @@ RSpec.describe(endend+ context 'with disabled project custom fields with default value' do+ it 'is still disabled in the copy' do+ create(:text_project_custom_field, default_value: 'default value')++ expect(subject).to be_success++ expect(source.project_custom_fields).to eq([])+ expect(project_copy.project_custom_fields).to match_array(source.project_custom_fields)+ end+ end+context 'with disabled work package custom field' doit 'is still disabled in the copy' docustom_field = create(:text_wp_custom_field)