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
Update the acts_as_customizable plugin custom field value caching mechanism to use cache keys
Dombi Attila93b7923f2856
diff --git a/app/models/work_package.rb b/app/models/work_package.rbindex dce3416b4e3..57fe0c07a7f 100644--- a/app/models/work_package.rb+++ b/app/models/work_package.rb@@ -315,7 +315,6 @@ class WorkPackage < ApplicationRecorddef type_id=(tid)self.type = nilresult = write_attribute(:type_id, tid)- @custom_field_values = nilresultend@@ -452,6 +451,10 @@ class WorkPackage < ApplicationRecordWorkPackage::AvailableCustomFields.for(work_package.project, work_package.type)end+ def custom_field_cache_key+ [project_id, type_id]+ end+protecteddef <=>(other)diff --git a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rbindex ca4fee51796..722a9f2889d 100644--- a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb+++ b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb@@ -108,18 +108,31 @@ module Redmineenddef custom_field_values- @custom_field_values ||= available_custom_fields.flat_map do |custom_field|- existing_cvs = custom_values.select { |v| v.custom_field_id == custom_field.id }-- if existing_cvs.empty?- new_value = custom_values.build(customized: self,- custom_field:,- value: custom_field.default_value)- existing_cvs.push new_value+ custom_field_values_cache[custom_field_cache_key] ||=+ available_custom_fields.flat_map do |custom_field|+ existing_cvs = custom_values.select { |v| v.custom_field_id == custom_field.id }++ if existing_cvs.empty?+ new_value = custom_values.build(customized: self,+ custom_field:,+ value: custom_field.default_value)+ existing_cvs.push new_value+ end++ existing_cvsend+ end- existing_cvs- end+ # Returns the cache key for caching @custom_field_values_cache.+ #+ # In certain cases, the implementing models have a changing list of custom field values+ # depending on certain attributes. By overriding this method, we can include the+ # dependent attributes in the cache key, providing a more flexible key caching mechanism.+ #+ # i.e.: The work package custom field values are changing based on the project_id and type_id.+ # The only way to keep the cache updated is to include those ids in the cache key.+ def custom_field_cache_key+ 1end##@@ -193,7 +206,7 @@ module Redmineenddef reset_custom_values_change_tracker- @custom_field_values = nil+ @custom_field_values_cache = nilself.custom_value_destroyed = falseend@@ -384,6 +397,10 @@ module Redmineself.custom_value_destroyed = trueend+ def custom_field_values_cache+ @custom_field_values_cache ||= {}+ end+module ClassMethodsdef available_custom_fields(_model)RequestStore.fetch(:"#{name.underscore}_custom_fields") do