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

Implement polymorphic association for journaling

Klaus Zanders3ab822fa1e12
diff --git a/app/components/activities/item_component.rb b/app/components/activities/item_component.rb
index fcbb391d45c..c62dd6bc571 100644
--- a/app/components/activities/item_component.rb
+++ b/app/components/activities/item_component.rb
@@ -132,13 +132,38 @@ class Activities::ItemComponent < ViewComponent::Base
remove_detail_when_changing_from_empty(details, "activity_id")
remove_detail_when_changing_from_empty(details, "spent_on")
+ build_polymorphic_entity_gid_changeset(details)
+
details
end
+ def build_polymorphic_entity_gid_changeset(details)
+ return if !details.key?("entity_id") && !details.key?("entity_type")
+
+ details["entity_gid"] = [
+ build_gid(*type_and_id_for("entity", index: 0)),
+ build_gid(*type_and_id_for("entity", index: 1))
+ ]
+
+ details.delete("entity_id")
+ details.delete("entity_type")
+ end
+
+ def type_and_id_for(field, index:)
+ [
+ details["#{field}_type"]&.at(index) || @event.journal.journable.public_send("#{field}_type"),
+ details["#{field}_id"]&.at(index) || @event.journal.journable.public_send("#{field}_id")
+ ]
+ end
+
def remove_detail_when_changing_from_empty(details, field)
details.delete(field) if details[field] && details[field].first.nil?
end
+ def build_gid(entity_type, entity_id)
+ "gid://#{GlobalID.app}/#{entity_type}/#{entity_id}"
+ end
+
def render_event_details
data[:details].filter_map do |detail|
@event.journal.render_detail(detail, activity_page: @activity_page)
diff --git a/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb b/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb
index 099e18a5aa4..1ad38a3527d 100644
--- a/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb
+++ b/lib_static/plugins/acts_as_journalized/lib/journal_formatter.rb
@@ -59,6 +59,7 @@ require_relative "journal_formatter/decimal"
require_relative "journal_formatter/fraction"
require_relative "journal_formatter/id"
require_relative "journal_formatter/named_association"
+require_relative "journal_formatter/polymorphic_association"
require_relative "journal_formatter/percentage"
require_relative "journal_formatter/plaintext"
@@ -84,6 +85,7 @@ module JournalFormatter
fraction: JournalFormatter::Fraction,
id: JournalFormatter::Id,
named_association: JournalFormatter::NamedAssociation,
+ polymorphic_association: JournalFormatter::PolymorphicAssociation,
percentage: JournalFormatter::Percentage,
plaintext: JournalFormatter::Plaintext
}
diff --git a/lib_static/plugins/acts_as_journalized/lib/journal_formatter/polymorphic_association.rb b/lib_static/plugins/acts_as_journalized/lib/journal_formatter/polymorphic_association.rb
new file mode 100644
index 00000000000..7d8d65f4ef0
--- /dev/null
+++ b/lib_static/plugins/acts_as_journalized/lib/journal_formatter/polymorphic_association.rb
@@ -0,0 +1,78 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 JournalFormatter
+ class PolymorphicAssociation < Attribute
+ def render(key_with_gid, values, options = { html: true })
+ key = key_with_gid.to_s.delete_suffix("_gid")
+ 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)
+ end
+
+ render_ternary_detail_text(label, value, old_value, options)
+ end
+
+ private
+
+ def format_details(key, values, cache:)
+ label = label(key)
+
+ old_value, value = *format_values(values, cache:)
+
+ [label, old_value, value]
+ end
+
+ def format_values(values, cache:)
+ values.map do |value|
+ next if value.nil?
+
+ record = associated_object(value, cache:)
+ associated_object_name(record)
+ end
+ end
+
+ def associated_object_name(object)
+ object&.name
+ end
+
+ def associated_object(gid, cache:)
+ if cache
+ go = GlobalID.new(gid)
+ cache.fetch(go.model_name, go.model_id) do # rubocop:disable Lint/UselessDefaultValueArgument
+ GlobalID::Locator.locate(gid)
+ end
+ else
+ GlobalID::Locator.locate(gid)
+ end
+ end
+ end
+end
diff --git a/modules/costs/app/models/time_entry.rb b/modules/costs/app/models/time_entry.rb
index 02fb85e1d66..d4f5febc8ca 100644
--- a/modules/costs/app/models/time_entry.rb
+++ b/modules/costs/app/models/time_entry.rb
@@ -99,7 +99,8 @@ class TimeEntry < ApplicationRecord
register_journal_formatted_fields "hours", formatter_key: :time_entry_hours
register_journal_formatted_fields "user_id", formatter_key: :time_entry_named_association
- register_journal_formatted_fields "work_package_id", "activity_id", formatter_key: :named_association
+ register_journal_formatted_fields "activity_id", formatter_key: :named_association
+ register_journal_formatted_fields "entity_gid", formatter_key: :polymorphic_association
register_journal_formatted_fields "comments", "spent_on", "start_time", formatter_key: :plaintext
def self.update_all(updates, conditions = nil, options = {})