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
move wp cf load caching into wp model
ulfertsff812fb48fc2
diff --git a/app/models/work_package.rb b/app/models/work_package.rbindex bb119391d0a..cc48970983b 100644--- a/app/models/work_package.rb+++ b/app/models/work_package.rb@@ -449,8 +449,46 @@ class WorkPackage < ApplicationRecord# Overrides Redmine::Acts::Customizable::ClassMethods#available_custom_fieldsdef self.available_custom_fields(work_package)- WorkPackage::AvailableCustomFields.for(work_package.project, work_package.type)+ if work_package.project_id && work_package.type_id+ RequestStore.fetch(available_custom_field_key(work_package)) do+ available_custom_fields_from_db([work_package])+ end+ else+ []+ end+ end++ 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',+ 'custom_fields.*')++ 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+ end+ end+ end++ def self.available_custom_fields_from_db(work_packages)+ WorkPackageCustomField+ .left_joins(:projects, :types)+ .where(projects: { id: work_packages.map(&:project_id).uniq },+ types: { id: work_packages.map(&:type_id).uniq })+ .or(WorkPackageCustomField+ .left_joins(:projects, :types)+ .references(:projects, :types)+ .where(is_for_all: true)+ .where(types: { id: work_packages.map(&:type_id).uniq }))+ end+ private_class_method :available_custom_fields_from_db++ def self.available_custom_field_key(work_package)+ :"#work_package_custom_fields_#{work_package.project_id}_#{work_package.type_id}"end+ private_class_method :available_custom_field_keydef custom_field_cache_key[project_id, type_id]diff --git a/app/models/work_package/available_custom_fields.rb b/app/models/work_package/available_custom_fields.rbdeleted file mode 100644index c909428f814..00000000000--- a/app/models/work_package/available_custom_fields.rb+++ /dev/null@@ -1,33 +0,0 @@-#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 2012-2023 the OpenProject GmbH-#-# This program is free software; you can redistribute it and/or-# modify it under the terms of the GNU General Public License version 3.-#-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:-# Copyright (C) 2006-2013 Jean-Philippe Lang-# Copyright (C) 2010-2013 the ChiliProject Team-#-# This program is free software; you can redistribute it and/or-# modify it under the terms of the GNU General Public License-# as published by the Free Software Foundation; either version 2-# of the License, or (at your option) any later version.-#-# This program is distributed in the hope that it will be useful,-# but WITHOUT ANY WARRANTY; without even the implied warranty of-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-# GNU General Public License for more details.-#-# You should have received a copy of the GNU General Public License-# along with this program; if not, write to the Free Software-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.-#-# See COPYRIGHT and LICENSE files for more details.-#++--module WorkPackage::AvailableCustomFields- def self.for(project, type)- project && type ? (project.all_work_package_custom_fields & type.custom_fields) : []- end-enddiff --git a/lib/api/v3/work_packages/eager_loading/custom_value.rb b/lib/api/v3/work_packages/eager_loading/custom_value.rbindex 4aa041e85b9..3584324839c 100644--- a/lib/api/v3/work_packages/eager_loading/custom_value.rb+++ b/lib/api/v3/work_packages/eager_loading/custom_value.rb@@ -31,14 +31,17 @@ module APImodule WorkPackagesmodule EagerLoadingclass CustomValue < Base+ attr_accessor :query++ def initialize(work_packages, **options)+ super++ WorkPackage.preload_available_custom_fields(work_packages)+ end+def apply(work_package)load_custom_values(work_package)load_custom_values_values(work_package)- load_available_custom_fields(work_package)- end-- def self.module- ::API::V3::Utilities::EagerLoading::CustomFieldAccessorendprivate@@ -65,10 +68,6 @@ module APIendend- def load_available_custom_fields(work_package)- work_package.available_custom_fields = custom_fields_of(work_package).to_a- end-def grouped_custom_values@grouped_custom_values ||= begincustom_values = ::CustomValue@@ -126,66 +125,14 @@ module API.select { |cv| cv.custom_field && cv.custom_field.field_format == field_format && cv.value.present? }end- def usages- @usages ||= ActiveRecord::Base- .connection- .select_all(configured_fields_sql)- .to_a- .uniq- end-def custom_field(id)- @loaded_custom_fields_by_id ||= WorkPackageCustomField- .where(id: usages.pluck('custom_field_id').uniq)- .index_by(&:id)+ @loaded_custom_fields_by_id ||= work_packages.map(&:available_custom_fields).flatten.uniq.index_by(&:id)@loaded_custom_fields_by_id[id]end- def usage_map- @usage_map ||= usages.inject(usage_hash) do |hash, by|- cf = custom_field(by['custom_field_id'])- target_project_id = by['project_id']-- # If the project_id is NOT nil, and the custom_field is `is_for_all`- # Ensure that it gets added to hash[nil] (Regression #28435)- if by['project_id'].present? && cf.is_for_all- target_project_id = nil- end-- hash[target_project_id][by['type_id']] << cf-- hash- end- end-- def custom_fields_of(work_package)- usage_map[work_package.project_id][work_package.type_id] +- usage_map[nil][work_package.type_id]- end-- def configured_fields_sql- WorkPackageCustomField- .left_joins(:projects, :types)- .where(projects: { id: work_packages.map(&:project_id).uniq },- types: { id: work_packages.map(&:type_id).uniq })- .or(WorkPackageCustomField- .left_joins(:projects, :types)- .references(:projects, :types)- .where(is_for_all: true))- .select('projects.id project_id',- 'types.id type_id',- 'custom_fields.id custom_field_id')- .to_sql- end-- def usage_hash- Hash.new do |by_project_hash, project_id|- by_project_hash[project_id] = Hash.new do |by_type_hash, type_id|- # Use a set to ensure CFs are only available once- by_type_hash[type_id] = Set.new- end- end+ def timestamped?+ @timestamped ||= query.try(:timestamps).any?(&:historic?)endendenddiff --git a/lib/api/v3/work_packages/work_package_collection_representer.rb b/lib/api/v3/work_packages/work_package_collection_representer.rbindex 8133ba9fc4c..47307208cab 100644--- a/lib/api/v3/work_packages/work_package_collection_representer.rb+++ b/lib/api/v3/work_packages/work_package_collection_representer.rb@@ -185,10 +185,6 @@ module APIdef schemasschemas = schema_pairs.map do |project, type, available_custom_fields|- # This hack preloads the custom fields for a project so that they do not have to be- # loaded again later on- project.instance_variable_set(:@all_work_package_custom_fields, all_cfs_of_project[project.id])-Schema::TypedWorkPackageSchema.new(project:, type:, custom_fields: available_custom_fields)end@@ -221,12 +217,6 @@ module APIendend- def all_cfs_of_project- @all_cfs_of_project ||= represented- .group_by(&:project_id)- .transform_values { |wps| wps.map(&:available_custom_fields).flatten.uniq }- end-def paged_models(models)super.pluck(:id)enddiff --git a/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rb b/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rbindex e29ca66c356..7207bc0ed02 100644--- a/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rb+++ b/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rb@@ -77,8 +77,11 @@ module API::API::V3::WorkPackages::EagerLoading::Project,::API::V3::WorkPackages::EagerLoading::Checksum,::API::V3::WorkPackages::EagerLoading::CustomValue,- ::API::V3::WorkPackages::EagerLoading::HistoricAttributes,- ::API::V3::WorkPackages::EagerLoading::CustomAction+ ::API::V3::WorkPackages::EagerLoading::CustomAction,+ # Have the historic attributes last as they require the custom values+ # to be loaded first in order to create the diffs between the current+ # and the historic values without loading the custom fields (JournableDiffer).+ ::API::V3::WorkPackages::EagerLoading::HistoricAttributes]enddiff --git a/spec/models/work_package/work_package_custom_fields_spec.rb b/spec/models/work_package/work_package_custom_fields_spec.rbindex e5b42e3c223..313d3cff188 100644--- a/spec/models/work_package/work_package_custom_fields_spec.rb+++ b/spec/models/work_package/work_package_custom_fields_spec.rb@@ -400,4 +400,77 @@ RSpec.describe WorkPackage doendendend++ describe '.preload_available_custom_fields/#available_custom_fields' do+ let(:project) { create(:project) }+ let(:type) { create(:type) }+ let(:work_package) do+ build(:work_package,+ project:,+ type:)+ end++ let!(:custom_field_of_project_and_type) do+ create(:work_package_custom_field,+ name: 'Custom field of type and project').tap do |cf|+ project.work_package_custom_fields << cf+ type.custom_fields << cf+ end+ end+ let!(:custom_field_of_project_not_type) do+ create(:work_package_custom_field,+ name: 'Custom field of project not type').tap do |cf|+ project.work_package_custom_fields << cf+ end+ end+ let!(:custom_field_of_type_not_project) do+ create(:work_package_custom_field,+ name: 'Custom field of type not project').tap do |cf|+ type.custom_fields << cf+ end+ end+ let!(:custom_field_for_all_and_type) do+ create(:work_package_custom_field,+ name: 'Custom field for all and type',+ is_for_all: true).tap do |cf|+ type.custom_fields << cf+ end+ end+ let!(:custom_field_for_all_not_type) do+ create(:work_package_custom_field,+ name: 'Custom field for all not type',+ is_for_all: true)+ end++ context 'when preloading the custom fields' do+ before do+ described_class.preload_available_custom_fields([work_package])+ # 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+ expect(work_package.available_custom_fields)+ .to contain_exactly(custom_field_of_project_and_type,+ custom_field_for_all_and_type)+ end++ it 'does not call the database' do+ work_package.available_custom_fields++ expect(WorkPackageCustomField)+ .not_to have_received(:left_joins)+ end+ end++ context 'when not preloading the custom fields' 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)+ end+ end+ endend