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

fixed and finetuned project custom field activation for create, update and copy contexts

Jonas Jabaria00a44a3c7b1
diff --git a/app/models/custom_value.rb b/app/models/custom_value.rb
index eeee77edd8b..ea8fde49706 100644
--- a/app/models/custom_value.rb
+++ b/app/models/custom_value.rb
@@ -35,7 +35,7 @@ class CustomValue < ApplicationRecord
validate :validate_type_of_value
validate :validate_length_of_value
- after_create :activate_custom_field_in_customized_project, if: -> { customized.is_a?(Project) && value.present? }
+ after_create :activate_custom_field_in_customized_project, if: -> { customized.is_a?(Project) }
delegate :typed_value,
:formatted_value,
@@ -69,6 +69,8 @@ class CustomValue < ApplicationRecord
end
def activate_custom_field_in_customized_project
+ return if default? || value.blank?
+
# if a custom value is created for a project via CustomValue.create(...),
# the custom field needs to be activated in the project
unless customized&.project_custom_fields&.include?(custom_field)
diff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rb
index 41fbda4b9c8..76d4f4bcdbe 100644
--- a/app/models/projects/acts_as_customizable_patches.rb
+++ b/app/models/projects/acts_as_customizable_patches.rb
@@ -39,7 +39,7 @@ module Projects::ActsAsCustomizablePatches
dependent: :destroy, inverse_of: :project
has_many :project_custom_fields, through: :project_custom_field_project_mappings, class_name: 'ProjectCustomField'
- before_save :build_missing_project_custom_field_project_mappings
+ before_create :build_missing_project_custom_field_project_mappings
after_save :reset_section_scoped_validation, :set_query_available_custom_fields_to_project_level
# we need to reset the query_available_custom_fields_on_global_level already after validation
@@ -57,8 +57,7 @@ module Projects::ActsAsCustomizablePatches
# current shortcommings:
# - boolean custom fields are always activated as a nil value is never provided (always true/false)
custom_field_ids = project.custom_values
- .reject { |cv| cv.value.blank? }
- .reject { |cv| cv.persisted? } # do not reactivate custom fields which have already been used and disabled
+ .select { |cv| cv.value.present? }
.pluck(:custom_field_id).uniq
activated_custom_field_ids = project_custom_field_project_mappings.pluck(:custom_field_id).uniq
diff --git a/app/services/projects/copy_service.rb b/app/services/projects/copy_service.rb
index 02199b83b0c..48c0cefc94c 100644
--- a/app/services/projects/copy_service.rb
+++ b/app/services/projects/copy_service.rb
@@ -81,24 +81,39 @@ module Projects
end
def after_perform(call)
- remove_custom_fields_not_activated_in_source(call)
+ disable_custom_fields_not_activated_in_source(call)
+ enable_custom_fields_not_activated_in_target(call)
super
end
- def remove_custom_fields_not_activated_in_source(call)
+ def disable_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:
+ # but the custom field would be activated in the target project with its default value,
+ # which is desired in pure project creation context
+ # but in the copy context 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
+ custom_fields_to_disable = custom_fields_activated_in_target - custom_fields_activated_in_source
+ call.result.project_custom_field_project_mappings.where(custom_field_id: custom_fields_to_disable).destroy_all
+ end
+
+ def enable_custom_fields_not_activated_in_target(call)
+ # TODO: seems a bit too hacky to me, find better solution
+ # if custom fields in source project are activated but set to blank
+ # they would not be activated in the target project,
+ # which is desired in pure project creation context (-> as form fields are blank)
+ # but in the copy context we activate them here in order to have the same mapping as seen in the source project:
+ 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_enable = custom_fields_activated_in_source - custom_fields_activated_in_target
+ call.result.project_custom_fields << ProjectCustomField.where(id: custom_fields_to_enable)
end
def contract_options
diff --git a/spec/features/projects/copy_spec.rb b/spec/features/projects/copy_spec.rb
index db69f6c59ed..7ba1583efbf 100644
--- a/spec/features/projects/copy_spec.rb
+++ b/spec/features/projects/copy_spec.rb
@@ -184,7 +184,7 @@ RSpec.describe 'Projects copy', :js, :with_cuprite do
)
end
- it 'disables optional project custom fields if explicitly set to blank' do
+ it 'does not disable optional project custom fields if explicitly set to blank' do
# Expand advanced settings
click_on 'Advanced settings'
@@ -199,8 +199,12 @@ RSpec.describe 'Projects copy', :js, :with_cuprite do
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
)
+
+ # the optional custom field is activated, but set to blank value
+ expect(copied_project.custom_value_for(optional_project_custom_field).typed_value).to eq('')
end
# TBD: Is this intended from a conceptial point of view?
@@ -208,7 +212,7 @@ RSpec.describe 'Projects copy', :js, :with_cuprite do
# If not, I don't know how to change this behavior while keeping the behavior specified in the creation spec where
# optional custom fields are not activated if the value is set to blank in the form (which seems to be desired from
# a concpetional point of view)
- it 'does not enable project custom fields (with default values) if set to blank in source project' do
+ it 'does enable project custom fields if set to blank in source project' do
project.update!(custom_field_values: {
optional_project_custom_field.id => '',
optional_project_custom_field_with_default.id => ''
@@ -237,8 +241,14 @@ RSpec.describe 'Projects copy', :js, :with_cuprite do
copied_project = Project.find_by(name: 'Copied project')
expect(copied_project.project_custom_field_ids).to contain_exactly(
- project_custom_field.id
+ project_custom_field.id,
+ optional_project_custom_field.id,
+ optional_project_custom_field_with_default.id
)
+
+ # the optional custom fields are activated, but set to blank values as seen in source project
+ expect(copied_project.custom_value_for(optional_project_custom_field).typed_value).to eq('')
+ expect(copied_project.custom_value_for(optional_project_custom_field_with_default).typed_value).to eq('')
end
context 'with project custom fields with default values, which are disabled in source project' do
@@ -326,7 +336,7 @@ RSpec.describe 'Projects copy', :js, :with_cuprite do
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
+ it 'does not show invisible fields in the form and but still activates them' do
expect(page).to have_no_content 'Text for Admins only'
click_button 'Save'
@@ -338,10 +348,9 @@ RSpec.describe 'Projects copy', :js, :with_cuprite do
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_project_custom_field_with_default.id,
+ invisible_field.id
)
-
- expect(copied_project.custom_value_for(invisible_field)).to be_nil
end
end
end
diff --git a/spec/models/projects/customizable_spec.rb b/spec/models/projects/customizable_spec.rb
index b33719040f1..a215180f719 100644
--- a/spec/models/projects/customizable_spec.rb
+++ b/spec/models/projects/customizable_spec.rb
@@ -232,10 +232,37 @@ 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
- let(:project) { create(:project) }
+ let!(:project) { create(:project) }
shared_examples 'implicitly enabled and saved custom values' do
it 'enables fields with provided values' do
@@ -311,9 +338,38 @@ RSpec.describe Project, 'customizable' do
.to contain_exactly(bool_custom_field)
end
- it 'does re-enable fields with new value which have been disabled in the past' do
- pending "this is currently not working, not sure if it should be supported at all"
+ 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 'does not activate custom fields with default values if not explicitly set to a value' do
+ project.update!(custom_field_values: {
+ text_custom_field.id => 'bar',
+ bool_custom_field.id => false
+ })
+
+ # text_custom_field_with_default is not provided, thus it should not be enabled (in contrast to creation)
+ expect(project.project_custom_field_project_mappings.pluck(:custom_field_id))
+ .to contain_exactly(text_custom_field.id, bool_custom_field.id)
+ end
+ it 'does activate custom fields with default values if explicitly set to a value' do
+ project.update!(custom_field_values: {
+ text_custom_field.id => 'bar',
+ bool_custom_field.id => false,
+ text_custom_field_with_default.id => 'overwritten default'
+ })
+
+ # text_custom_field_with_default is not provided, thus it should not be enabled (in contrast to creation)
+ 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
+
+ it 'does re-enable fields with new value which have been disabled in the past' do
project.update!(custom_field_values: {
text_custom_field.id => 'foo',
bool_custom_field.id => true