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
[#49569] Selected values are multiplied on CF multi-select
Dombi Attila8c77564f1290
diff --git a/app/models/work_package.rb b/app/models/work_package.rbindex cc48970983b..d35bf9ab8e8 100644--- a/app/models/work_package.rb+++ b/app/models/work_package.rb@@ -460,14 +460,16 @@ class WorkPackage < ApplicationRecorddef 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)endendend@@ -482,6 +484,7 @@ class WorkPackage < ApplicationRecord.references(:projects, :types).where(is_for_all: true).where(types: { id: work_packages.map(&:type_id).uniq }))+ .distinctendprivate_class_method :available_custom_fields_from_dbdiff --git a/spec/models/work_package/work_package_acts_as_customizable_spec.rb b/spec/models/work_package/work_package_acts_as_customizable_spec.rbindex 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' dotype:)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) docreate(:work_package_custom_field,name: 'Custom field of type and project').tap do |cf|@@ -202,23 +210,41 @@ RSpec.describe WorkPackage, 'acts_as_customizable' dois_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' dobefore 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_originalend- it 'returns all custom fields of the project and type' do+ it 'returns all custom fields of the project and type for work_package' doexpect(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)endit 'does not call the database' dowork_package.available_custom_fields+ work_package2.available_custom_fieldsexpect(WorkPackageCustomField).not_to have_received(:left_joins)@@ -229,7 +255,8 @@ RSpec.describe WorkPackage, 'acts_as_customizable' doit 'returns all custom fields of the project and type' doexpect(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)endendend