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

Activate custom fields for projects when adding new projects to the type

Dombi Attilae447a03698be
diff --git a/app/services/base_type_service.rb b/app/services/base_type_service.rb
index 1348c8af391..bf9bf187640 100644
--- a/app/services/base_type_service.rb
+++ b/app/services/base_type_service.rb
@@ -47,29 +47,11 @@ class BaseTypeService
private
def 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 = false
errors = type.errors
Type.transaction do
- set_scalar_params(params)
-
- # Only set attribute groups when it exists
- # (Regression #28400)
- unless params[:attribute_groups].nil?
- set_attribute_groups(params)
- end
-
- set_active_custom_fields
-
- success, errors = validate_and_save(type, user)
+ success, errors = set_params_and_validate(params)
if success
after_type_save(params, options)
else
@@ -80,6 +62,30 @@ class BaseTypeService
ServiceResult.new(success:,
errors:,
result: type)
+ rescue StandardError => e
+ ServiceResult.failure.tap do |result|
+ result.errors.add(:base, e.message)
+ end
+ end
+
+ def set_params_and_validate(params)
+ # Only set attribute groups when it exists
+ # (Regression #28400)
+ unless params[:attribute_groups].nil?
+ set_attribute_groups(params)
+ end
+
+ # This should go before `set_scalar_params` call to get the
+ # project_ids, custom_field_ids diffs from the type and the params
+ set_active_custom_fields
+
+ if params[:project_ids].present?
+ set_active_custom_fields_for_project_ids(params[:project_ids])
+ end
+
+ set_scalar_params(params)
+
+ validate_and_save(type, user)
end
def set_scalar_params(params)
@@ -153,7 +159,7 @@ class BaseTypeService
def set_active_custom_fields
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 }
+ set_active_custom_fields_for_projects(type.projects, new_cf_ids_to_add)
end
def active_custom_field_ids
@@ -170,4 +176,16 @@ class BaseTypeService
active_cf_ids.uniq
end
end
+
+ def set_active_custom_fields_for_projects(projects, custom_field_ids)
+ projects.each { |p| p.work_package_custom_field_ids |= custom_field_ids }
+ end
+
+ def set_active_custom_fields_for_project_ids(project_ids)
+ new_project_ids_to_activate_cfs = project_ids.reject(&:empty?).map(&:to_i) - type.project_ids
+ set_active_custom_fields_for_projects(
+ Project.where(id: new_project_ids_to_activate_cfs),
+ type.custom_field_ids
+ )
+ end
end
diff --git a/spec/features/types/form_configuration_spec.rb b/spec/features/types/form_configuration_spec.rb
index 38bb15077f8..af15eb78e15 100644
--- a/spec/features/types/form_configuration_spec.rb
+++ b/spec/features/types/form_configuration_spec.rb
@@ -305,6 +305,20 @@ describe 'form configuration', type: :feature, js: true do
context 'if inactive in project' do
it 'can be added to the type, but is not shown' do
+ # Disable in project, should be invisible
+ # This step is necessary, since we auto-activate custom fields
+ # when adding them to the form configuration
+ project_settings_page.visit_tab!('custom_fields')
+
+ expect(page).to have_selector(".custom-field-#{custom_field.id} td", text: 'MyNumber')
+ expect(page).to have_selector(".custom-field-#{custom_field.id} td", text: type.name)
+
+ id_checkbox = find("#project_work_package_custom_field_ids_#{custom_field.id}")
+ expect(id_checkbox).to be_checked
+ id_checkbox.set(false)
+
+ click_button 'Save'
+
# Visit work package with that type
wp_page.visit!
wp_page.ensure_page_loaded
diff --git a/spec/services/shared_type_service.rb b/spec/services/shared_type_service.rb
index a8feaf1c904..df91edd254b 100644
--- a/spec/services/shared_type_service.rb
+++ b/spec/services/shared_type_service.rb
@@ -29,7 +29,9 @@
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
+ let!(:cf3) { create :work_package_custom_field, field_format: 'text' }
+
+ let(:attribute_groups) do
{
attribute_groups: [
{ 'type' => 'attribute',
@@ -41,6 +43,8 @@ shared_context 'with custom field params' do
]
}
end
+
+ let(:params) { attribute_groups }
end
shared_examples_for 'type service' do
@@ -130,7 +134,7 @@ shared_examples_for 'type service' do
expect(type).to have_received(:custom_field_ids=)
end
- context 'when the type is associated with projects' do
+ context 'when all the projects are associated with the type' do
before do
type.projects = create_list :project, 2
end
@@ -141,6 +145,79 @@ shared_examples_for 'type service' do
.from([[], []])
.to([[cf1, cf2], [cf1, cf2]])
end
+
+ context 'when a custom field is already associated with the type' do
+ before do
+ type.custom_field_ids = [cf1.id]
+ end
+
+ it 'enables the new custom field only' do
+ expect { service_call }
+ .to change { Project.where(id: type.project_ids).map(&:work_package_custom_fields) }
+ .from([[], []])
+ .to([[cf2], [cf2]])
+ end
+ end
+
+ context 'when all custom fields are already associated with the type' do
+ before do
+ type.custom_field_ids = [cf1.id, cf2.id]
+ end
+
+ it 'enables no custom field' do
+ expect { service_call }
+ .not_to change { Project.where(id: type.project_ids).map(&:work_package_custom_field_ids) }
+ .from([[], []])
+ end
+ end
+ end
+
+ context 'when a project is being set on the type' do
+ let(:projects) { create_list :project, 2 }
+ let(:active_project) { projects.first }
+ let(:project_ids) { { project_ids: [*projects.map { |p| p.id.to_s }, ""] } }
+ let(:params) do
+ attribute_groups.merge(project_ids)
+ end
+
+ before do
+ type.projects << active_project
+ end
+
+ it 'enables the custom fields for all 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]])
+ end
+
+ context 'when a custom field is already associated with the type' do
+ before do
+ type.custom_field_ids = [cf1.id]
+ end
+
+ it 'enables the new cf for the existing project and enables both cfs for the new project' do
+ expect { service_call }
+ .to change { Project.where(id: type.project_ids).map(&:work_package_custom_fields) }
+ .from([[]])
+ .to([[cf2], [cf1, cf2]])
+ end
+ end
+
+ context 'when all custom fields are already associated with the type' do
+ let(:params) { project_ids }
+
+ before do
+ type.custom_field_ids = [cf1.id, cf2.id]
+ end
+
+ it 'enables the custom fields in the new project only' do
+ expect { service_call }
+ .to change { Project.where(id: type.project_ids).map(&:work_package_custom_fields) }
+ .from([[]])
+ .to([[], [cf1, cf2]])
+ end
+ end
end
end
@@ -228,6 +305,7 @@ shared_examples_for 'type service' do
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) }
+ .from([[], []])
end
end
end