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

[#49569] Selected values are multiplied on CF multi-select

Dombi Attila8c77564f1290
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index cc48970983b..d35bf9ab8e8 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -460,14 +460,16 @@ class WorkPackage < ApplicationRecord
def self.preload_available_custom_fields(work_packages)
custom_fields = available_custom_fields_from_db(work_packages)
- .select('projects.id project_id',
- 'types.id type_id',
+ .select('array_agg(projects.id) available_project_ids',
+ 'array_agg(types.id) available_type_ids',
'custom_fields.*')
+ .group('custom_fields.id')
work_packages.each do |work_package|
RequestStore.store[available_custom_field_key(work_package)] = custom_fields
.select do |cf|
- ((cf.project_id == work_package.project_id) || cf.is_for_all?) && cf.type_id == work_package.type_id
+ (cf.available_project_ids.include?(work_package.project_id) || cf.is_for_all?) &&
+ cf.available_type_ids.include?(work_package.type_id)
end
end
end
@@ -482,6 +484,7 @@ class WorkPackage < ApplicationRecord
.references(:projects, :types)
.where(is_for_all: true)
.where(types: { id: work_packages.map(&:type_id).uniq }))
+ .distinct
end
private_class_method :available_custom_fields_from_db
diff --git a/spec/models/work_package/work_package_acts_as_customizable_spec.rb b/spec/models/work_package/work_package_acts_as_customizable_spec.rb
index 45a4df5bd95..c982a69637d 100644
--- a/spec/models/work_package/work_package_acts_as_customizable_spec.rb
+++ b/spec/models/work_package/work_package_acts_as_customizable_spec.rb
@@ -170,6 +170,14 @@ RSpec.describe WorkPackage, 'acts_as_customizable' do
type:)
end
+ let(:project2) { create(:project) }
+ let(:type2) { create(:type) }
+ let(:work_package2) do
+ build(:work_package,
+ project: project2,
+ type: type2)
+ end
+
let!(:custom_field_of_project_and_type) do
create(:work_package_custom_field,
name: 'Custom field of type and project').tap do |cf|
@@ -202,23 +210,41 @@ RSpec.describe WorkPackage, 'acts_as_customizable' do
is_for_all: true)
end
+ let!(:custom_field_of_projects_and_types_for_all) do
+ create(:work_package_custom_field,
+ name: 'Custom field for all and many types and projects',
+ is_for_all: true).tap do |cf|
+ project.work_package_custom_fields << cf
+ type.custom_fields << cf
+ project2.work_package_custom_fields << cf
+ type2.custom_fields << cf
+ end
+ end
+
context 'when preloading the custom fields' do
before do
- described_class.preload_available_custom_fields([work_package])
+ described_class.preload_available_custom_fields([work_package, work_package2])
# Bad replacement to check that no database query is run.
allow(WorkPackageCustomField)
.to receive(:left_joins)
.and_call_original
end
- it 'returns all custom fields of the project and type' do
+ it 'returns all custom fields of the project and type for work_package' do
expect(work_package.available_custom_fields)
.to contain_exactly(custom_field_of_project_and_type,
- custom_field_for_all_and_type)
+ custom_field_for_all_and_type,
+ custom_field_of_projects_and_types_for_all)
+ end
+
+ it 'returns all custom fields of the project and type for work_package2' do
+ expect(work_package2.available_custom_fields)
+ .to contain_exactly(custom_field_of_projects_and_types_for_all)
end
it 'does not call the database' do
work_package.available_custom_fields
+ work_package2.available_custom_fields
expect(WorkPackageCustomField)
.not_to have_received(:left_joins)
@@ -229,7 +255,8 @@ RSpec.describe WorkPackage, 'acts_as_customizable' do
it 'returns all custom fields of the project and type' do
expect(work_package.available_custom_fields)
.to contain_exactly(custom_field_of_project_and_type,
- custom_field_for_all_and_type)
+ custom_field_for_all_and_type,
+ custom_field_of_projects_and_types_for_all)
end
end
end