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

replace JournableDiffer leftovers with Differ::Association

Ivan Kuchinb3dcce1649a1
diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rb
index 727aeec48ee..c60ebf1ab3b 100644
--- a/app/models/journable/with_historic_attributes.rb
+++ b/app/models/journable/with_historic_attributes.rb
@@ -238,7 +238,7 @@ class Journable::WithHistoricAttributes < SimpleDelegator
changes = ::Acts::Journalized::Differ::Model.changes(__getobj__, historic_journable)
- # In the other occurrences of JournableDiffer.association_changes calls, we are using the plural
+ # In the other occurrences of Differ::Association calls, we are using the plural
# of the association name (`custom_fields` in this instance), to map the association fields. That
# will result in a changes hash containing { "custom_fields_1" => ... }. This makes sense in the case
# of journal changes, because the formatted fields have the convention for plural lookup for journals
@@ -248,13 +248,14 @@ class Journable::WithHistoricAttributes < SimpleDelegator
# As a food for thought, I think it would be more handy to use the singular naming everywhere.
changes.merge!(
- ::Acts::Journalized::JournableDiffer.association_changes(
+ ::Acts::Journalized::Differ::Association.new(
historic_journable,
__getobj__,
- "custom_values",
- "custom_field",
- :custom_field_id,
- :value
+ association: :custom_values,
+ id_attribute: :custom_field_id
+ ).attribute_changes(
+ :value,
+ key_prefix: "custom_field"
)
)
diff --git a/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rb b/lib/api/v3/work_packages/work_package_eager_loading_wrapper.rb
index 70cdb247d47..79c41206e8b 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
@@ -81,7 +81,7 @@ module API
::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).
+ # and the historic values without loading the custom fields (Acts::Journalized::Differ).
::API::V3::WorkPackages::EagerLoading::HistoricAttributes
]
end
diff --git a/lib_static/plugins/acts_as_journalized/lib/acts/journalized/differ/association.rb b/lib_static/plugins/acts_as_journalized/lib/acts/journalized/differ/association.rb
new file mode 100644
index 00000000000..a7d76ddf12d
--- /dev/null
+++ b/lib_static/plugins/acts_as_journalized/lib/acts/journalized/differ/association.rb
@@ -0,0 +1,79 @@
+# 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 Acts::Journalized::Differ
+ class Association
+ def initialize(original, changed, association:, id_attribute:)
+ @original_by_id = attributes_by_id(original, association, id_attribute)
+ @changed_by_id = attributes_by_id(changed, association, id_attribute)
+ @ids = (@changed_by_id.keys | @original_by_id.keys).compact
+ end
+
+ def attribute_changes(attribute, key_prefix:)
+ single_attribute_changes(attribute)
+ .transform_keys { |id| "#{key_prefix}_#{id}" }
+ end
+
+ def attributes_changes(attributes, key_prefix:)
+ attributes.each_with_object({}) do |attribute, result|
+ single_attribute_changes(attribute).each do |id, change|
+ result["#{key_prefix}_#{id}_#{attribute}"] = change
+ end
+ end
+ end
+
+ private
+
+ def attributes_by_id(model, association, id_attribute)
+ return {} unless model
+
+ id_attribute = id_attribute.to_s
+
+ model.send(association).map(&:attributes).group_by { _1[id_attribute] }
+ end
+
+ def single_attribute_changes(attribute)
+ attribute = attribute.to_s
+
+ pairs = @ids.index_with do |id|
+ [
+ combine_journals(@original_by_id[id], attribute),
+ combine_journals(@changed_by_id[id], attribute)
+ ]
+ end
+
+ pairs.reject { |_, (old_value, new_value)| old_value.to_s.strip == new_value.to_s.strip }
+ end
+
+ def combine_journals(journals, attribute)
+ journals.pluck(attribute).sort.join(",") if journals
+ end
+ end
+end
diff --git a/lib_static/plugins/acts_as_journalized/lib/acts/journalized/journable_differ.rb b/lib_static/plugins/acts_as_journalized/lib/acts/journalized/journable_differ.rb
deleted file mode 100644
index e2ede7948b1..00000000000
--- a/lib_static/plugins/acts_as_journalized/lib/acts/journalized/journable_differ.rb
+++ /dev/null
@@ -1,89 +0,0 @@
-# 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 Acts::Journalized
- class JournableDiffer
- class << self
- def association_changes(original, changed, *)
- get_association_changes(original, changed, *)
- end
-
- def association_changes_multiple_attributes(original, changed, association, key_prefix, id_attribute, attributes)
- list = {}
- attributes.each do |attribute|
- list.store(attribute, get_association_changes(original, changed, association, key_prefix, id_attribute, attribute))
- end
-
- transformed = {}
- list.each do |attribute, changes|
- changes.each do |key, change|
- transformed["#{key}_#{attribute}"] = change
- end
- end
-
- transformed
- end
-
- private
-
- def get_association_changes(original, changed, association, key, id_attribute, attribute)
- original_journals = original&.send(association)&.map(&:attributes) || []
- changed_journals = changed.send(association).map(&:attributes)
-
- merged_journals = merge_reference_journals_by_id(original_journals, changed_journals, id_attribute.to_s, attribute.to_s)
-
- changes = merged_journals.reject do |_, (old_value, new_value)|
- old_value.to_s.strip == new_value.to_s.strip
- end
-
- changes.transform_keys { |id| "#{key}_#{id}" }
- end
-
- def merge_reference_journals_by_id(old_journals, new_journals, id_attribute, attribute)
- all_associated_journal_ids = (new_journals.pluck(id_attribute) | old_journals.pluck(id_attribute)).compact
-
- all_associated_journal_ids.index_with do |id|
- [select_and_combine_journals(old_journals, id, id_attribute, attribute),
- select_and_combine_journals(new_journals, id, id_attribute, attribute)]
- end
- end
-
- def select_and_combine_journals(journals, id, id_attribute, attribute)
- selected_journals = journals.select { |j| j[id_attribute] == id }.pluck(attribute)
-
- if selected_journals.empty?
- nil
- else
- selected_journals.sort.join(",")
- end
- end
- end
- end
-end
diff --git a/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb b/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb
index 85069220d48..6edd4d69078 100644
--- a/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb
+++ b/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb
@@ -56,26 +56,28 @@ module JournalChanges
def get_attachments_changes
return unless journable&.attachable?
- ::Acts::Journalized::JournableDiffer.association_changes(
+ ::Acts::Journalized::Differ::Association.new(
predecessor,
self,
- "attachable_journals",
- "attachments",
- :attachment_id,
- :filename
+ association: :attachable_journals,
+ id_attribute: :attachment_id
+ ).attribute_changes(
+ :filename,
+ key_prefix: "attachments"
)
end
def get_custom_fields_changes
return unless journable&.customizable?
- customizable_changes = ::Acts::Journalized::JournableDiffer.association_changes(
+ customizable_changes = ::Acts::Journalized::Differ::Association.new(
predecessor,
self,
- "customizable_journals",
- "custom_fields",
- :custom_field_id,
- :value
+ association: :customizable_journals,
+ id_attribute: :custom_field_id
+ ).attribute_changes(
+ :value,
+ key_prefix: "custom_fields"
)
if journable.class.name == "Project"
@@ -97,13 +99,14 @@ module JournalChanges
def get_agenda_items_changes
return unless journable.respond_to?(:agenda_items)
- ::Acts::Journalized::JournableDiffer.association_changes_multiple_attributes(
+ ::Acts::Journalized::Differ::Association.new(
predecessor,
self,
- "agenda_item_journals",
- "agenda_items",
- :agenda_item_id,
- %i[title duration_in_minutes notes position work_package_id]
+ association: :agenda_item_journals,
+ id_attribute: :agenda_item_id
+ ).attributes_changes(
+ %i[title duration_in_minutes notes position work_package_id],
+ key_prefix: "agenda_items"
)
end
diff --git a/spec/lib/acts_as_journalized/journable_differ_spec.rb b/spec/lib/acts/journalized/differ/association_spec.rb
similarity index 84%
rename from spec/lib/acts_as_journalized/journable_differ_spec.rb
rename to spec/lib/acts/journalized/differ/association_spec.rb
index b48525ed38b..47d1612b215 100644
--- a/spec/lib/acts_as_journalized/journable_differ_spec.rb
+++ b/spec/lib/acts/journalized/differ/association_spec.rb
@@ -30,8 +30,8 @@
require "spec_helper"
-RSpec.describe Acts::Journalized::JournableDiffer do
- describe ".association_changes" do
+RSpec.describe Acts::Journalized::Differ::Association do
+ describe "#attribute_changes" do
context "when the objects are work packages" do
let(:original) do
build(:work_package,
@@ -51,9 +51,12 @@ RSpec.describe Acts::Journalized::JournableDiffer do
])
end
+ let(:instance) do
+ described_class.new(original, changed, association: :custom_values, id_attribute: :custom_field_id)
+ end
+
it "returns the changes" do
- params = [original, changed, "custom_values", "custom_field", :custom_field_id, :value]
- expect(described_class.association_changes(*params))
+ expect(instance.attribute_changes(:value, key_prefix: "custom_field"))
.to eq(
"custom_field_1" => ["1", ""],
"custom_field_3" => ["", "2"]
@@ -79,9 +82,12 @@ RSpec.describe Acts::Journalized::JournableDiffer do
])
end
+ let(:instance) do
+ described_class.new(original, changed, association: :custom_values, id_attribute: :custom_field_id)
+ end
+
it "returns the changes" do
- params = [original, changed, "custom_values", "custom_field", :custom_field_id, :value]
- expect(described_class.association_changes(*params))
+ expect(instance.attribute_changes(:value, key_prefix: "custom_field"))
.to eq(
"custom_field_1" => [nil, "t"],
"custom_field_2" => ["1", "2"]
@@ -90,7 +96,7 @@ RSpec.describe Acts::Journalized::JournableDiffer do
end
end
- describe ".association_changes_multiple_attributes" do
+ describe "#attributes_changes" do
let(:original) do
build(:journal, project_life_cycle_step_journals: [
build_stubbed(:project_life_cycle_step_journal, life_cycle_step_id: 1, active: false),
@@ -112,10 +118,14 @@ RSpec.describe Acts::Journalized::JournableDiffer do
])
end
+ let(:instance) do
+ described_class.new(original, changed,
+ association: :project_life_cycle_step_journals,
+ id_attribute: :life_cycle_step_id)
+ end
+
it "returns the changes" do
- params = [original, changed, "project_life_cycle_step_journals", "project_life_cycle_steps", :life_cycle_step_id,
- %i[active start_date end_date]]
- expect(described_class.association_changes_multiple_attributes(*params))
+ expect(instance.attributes_changes(%i[active start_date end_date], key_prefix: "project_life_cycle_steps"))
.to eq(
"project_life_cycle_steps_1_active" => ["false", "true"],
"project_life_cycle_steps_2_active" => [nil, "true"],