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
Cache resources fetched when rendering journal details
Christophe Bliard53286184ba3d
diff --git a/lib/open_project/journal_formatter/custom_field.rb b/lib/open_project/journal_formatter/custom_field.rbindex 15d5a57cedd..360196dac30 100644--- a/lib/open_project/journal_formatter/custom_field.rb+++ b/lib/open_project/journal_formatter/custom_field.rb@@ -63,11 +63,13 @@ class OpenProject::JournalFormatter::CustomField < ::JournalFormatter::Baseenddef formatted_values(custom_field, values, modifier_fn)- old_value, new_value = values- old_option = modifier_fn.call(old_value, custom_field) if old_value- new_option = modifier_fn.call(new_value, custom_field) if new_value+ values.map { |value| formatted_value(custom_field, value, modifier_fn) }+ end++ def formatted_value(custom_field, value, modifier_fn)+ return if value.nil?- [old_option || old_value, new_option || new_value]+ modifier_fn.call(value, custom_field) || valueenddef find_user_value(value, _custom_field)@@ -89,13 +91,12 @@ class OpenProject::JournalFormatter::CustomField < ::JournalFormatter::Baseend.join(', ')end- def find_list_value(id, custom_field)- ids = id.split(",").map(&:to_i)+ def find_list_value(value, custom_field)+ ids = value.split(",").map(&:to_i)id_value = custom_field.custom_options.where(id: ids)- .order(:position).pluck(:id, :value).to_hdiff --git a/lib_static/plugins/acts_as_journalized/lib/journal_deprecated.rb b/lib_static/plugins/acts_as_journalized/lib/journal_deprecated.rbdeleted file mode 100644index 97398d84b11..00000000000--- a/lib_static/plugins/acts_as_journalized/lib/journal_deprecated.rb+++ /dev/null@@ -1,60 +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.-#++--# This file is part of the acts_as_journalized plugin for the redMine-# project management software-#-# Copyright (C) 2010 Finn GmbH, http://finn.de-#-# 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 journal 2-# of the License, or (at your option) any later journal.-#-# 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.--# This module holds the formatting methods that each journal has.-# It provides the hooks to apply different formatting to the details-# of a specific journal.-module JournalDeprecated- # Shortcut from more issue-specific journals- def attachments- journaled.respond_to?(:attachments) ? journaled.attachments : nil- end-- # deprecate created_on: "use #created_at"- # deprecate journalized: "use journaled"- # deprecate attachments: "implement it yourself"-enddiff --git a/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb b/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rbindex 2806b34ac6c..ab38c589045 100644--- a/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb+++ b/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb@@ -49,6 +49,7 @@# It provides the hooks to apply different formatting to the details# of a specific journal.+require_relative './journal_formatter_cache'require_relative './journal_formatter/base'require_relative './journal_formatter/attribute'require_relative './journal_formatter/datetime'@@ -95,7 +96,7 @@ module JournalFormatterenddef render_detail(detail, options = {})- merge_options = { html: true, only_path: true }.merge(options)+ options = options.reverse_merge(html: true, only_path: true, cache: JournalFormatterCache.request_instance)if detail.respond_to? :to_arykey = detail.first@@ -107,9 +108,9 @@ module JournalFormatterformatter = formatter_instance(key.to_s)- return nil if formatter.nil?+ return if formatter.nil?- formatter.render(key, values, merge_options).html_safe+ formatter.render(key, values, options).html_safeenddef formatter_instance(formatter_key)diff --git a/lib_static/plugins/acts_as_journalized/lib/journal_formatter/named_association.rb b/lib_static/plugins/acts_as_journalized/lib/journal_formatter/named_association.rbindex 804c94e9625..30fd3dcc5c7 100644--- a/lib_static/plugins/acts_as_journalized/lib/journal_formatter/named_association.rb+++ b/lib_static/plugins/acts_as_journalized/lib/journal_formatter/named_association.rb@@ -28,8 +28,9 @@module JournalFormatterclass NamedAssociation < Attribute- def render(key, values, options = { html: true })- label, old_value, value = format_details(key, values, options)+ def render(key_with_id, values, options = { html: true })+ key = key_with_id.to_s.delete_suffix('_id')+ label, old_value, value = format_details(key, values, cache: options[:cache])if options[:html]label, old_value, value = *format_html_details(label, old_value, value)@@ -40,21 +41,20 @@ module JournalFormatterprivate- def format_details(key, values, options = {})+ def format_details(key, values, cache:)label = label(key)- old_value, value = *format_values(values, key, options)+ old_value, value = *format_values(values, key, cache:)[label, old_value, value]end- def format_values(values, key, options)- field = key.to_s.gsub(/_id\z/, '').to_sym- klass = class_from_field(field)+ def format_values(values, key, cache:)+ klass = class_from_field(key)values.map do |value|- if klass- record = associated_object(klass, value.to_i, options)+ if klass && value+ record = associated_object(klass, value.to_i, cache:)if recordif record.respond_to? 'name'record.name@@ -66,26 +66,20 @@ module JournalFormatterendend- def associated_object(klass, id, options = {})- cache = options[:cache]-- if cache.is_a?(Acts::Journalized::JournalObjectCache)- cache.fetch(klass, id) do |k, i|- k.find_by(id: i)+ def associated_object(klass, id, cache:)+ if cache+ cache.fetch(klass, id) do+ klass.find_by(id:)endelseklass.find_by(id:)endend- def label(key)- @journal.journable.class.human_attribute_name(key.to_s.gsub(/_id$/, ''))- end-def class_from_field(field)association = @journal.journable.class.reflect_on_association(field)- association&.class_name&.constantize+ association&.klassendendenddiff --git a/lib_static/plugins/acts_as_journalized/lib/journal_detail.rb b/lib_static/plugins/acts_as_journalized/lib/journal_formatter_cache.rbsimilarity index 79%rename from lib_static/plugins/acts_as_journalized/lib/journal_detail.rbrename to lib_static/plugins/acts_as_journalized/lib/journal_formatter_cache.rbindex 20917be11fa..d32889a153c 100644--- a/lib_static/plugins/acts_as_journalized/lib/journal_detail.rb+++ b/lib_static/plugins/acts_as_journalized/lib/journal_formatter_cache.rb@@ -26,12 +26,21 @@# See COPYRIGHT and LICENSE files for more details.#++-class JournalDetail- attr_reader :prop_key, :value, :old_value+class JournalFormatterCache+ def self.request_instance+ RequestStore.store[:journal_formatter_cache] ||= new+ end++ def initialize+ @cache = Hash.new+ end- def initialize(prop_key, old_value, value)- @prop_key = prop_key- @old_value = old_value- @value = value+ def fetch(klass, id, &)+ key = [klass, id]+ if @cache.key?(key)+ @cache[key]+ elsif block_given?+ @cache[key] = yield+ endendenddiff --git a/lib_static/plugins/acts_as_journalized/lib/acts/journalized/journal_object_cache.rb b/spec/lib/acts_as_journalized/journal_formatter_cache_spec.rbsimilarity index 52%rename from lib_static/plugins/acts_as_journalized/lib/acts/journalized/journal_object_cache.rbrename to spec/lib/acts_as_journalized/journal_formatter_cache_spec.rbindex c0444daeecb..3f476ccabd4 100644--- a/lib_static/plugins/acts_as_journalized/lib/acts/journalized/journal_object_cache.rb+++ b/spec/lib/acts_as_journalized/journal_formatter_cache_spec.rb@@ -1,6 +1,6 @@#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2023 the OpenProject GmbH+# Copyright (C) 2012-2022 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.@@ -26,18 +26,33 @@# See COPYRIGHT and LICENSE files for more details.#++-module Acts- module Journalized- class JournalObjectCache- def fetch(klass, id, &)- @cache ||= Hash.new do |klass_hash, klass_key|- klass_hash[klass_key] = Hash.new do |id_hash, id_key|- id_hash[id_key] = yield klass_key, id_key- end- end+require 'spec_helper'- @cache[klass][id]- end+RSpec.describe JournalFormatterCache do+ subject(:cache) { described_class.new }++ describe '#fetch' do+ it 'caches and returns the value returned by the block on cache miss' do+ expect(cache.fetch(User, 3) { 'user_3' }).to eq('user_3')+ expect(cache.fetch('Answer', 42) { 'Life Universe Everything' }).to eq('Life Universe Everything')+ end++ it 'returns nil on cache miss if no block is given' do+ expect(cache.fetch(User, 3)).to be_nil+ expect(cache.fetch(User, 17)).to be_nil+ end++ it 'returns the cached value on cache hit' do+ cache.fetch(User, 3) { 'user_3' }++ expect(cache.fetch(User, 3)).to eq('user_3')+ expect(cache.fetch(User, 3) { 'another value' }).to eq('user_3')++ expect(cache.fetch(Project, 62)).to be_nil+ cache.fetch(Project, 62) { 'project_62' }+ expect(cache.fetch(Project, 62)).to eq('project_62')+ cache.fetch(Project, 62) { 'another value' }+ expect(cache.fetch(Project, 62)).to eq('project_62')endendend