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

document Differ::Association methods and rename all_values to multiple_values

Ivan Kuchin72c44e11137e
diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rb
index eb59ebe8fff..e48c013a1ee 100644
--- a/app/models/journable/with_historic_attributes.rb
+++ b/app/models/journable/with_historic_attributes.rb
@@ -253,7 +253,7 @@ class Journable::WithHistoricAttributes < SimpleDelegator
__getobj__,
association: :custom_values,
id_attribute: :custom_field_id,
- all_values: :joined
+ multiple_values: :joined
).single_attribute_changes(
:value,
key_prefix: "custom_field"
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
index bd1f08af905..054eb447091 100644
--- 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
@@ -30,18 +30,39 @@
module Acts::Journalized::Differ
class Association
- def initialize(original, changed, association:, id_attribute:, all_values: false)
+ # @param original [ActiveRecord::Base] the original object containing the association.
+ # @param changed [ActiveRecord::Base] the changed object containing the association.
+ # @param association [Symbol] the name of the association to be compared.
+ # @param id_attribute [Symbol] the attribute used to correlate association models.
+ # @param multiple_values [Boolean, :joined] whether there are multiple values per id_attribute:
+ # * when false - changes between first attribute values will be returned
+ # * when true - changes between sorted arrays of attribute values will be returned
+ # * when :joined - changes between sorted arrays will be joined with comma
+ def initialize(original, changed, association:, id_attribute:, multiple_values: false)
@original_by_id = association_by_id(original, association, id_attribute)
@changed_by_id = association_by_id(changed, association, id_attribute)
@ids = (@changed_by_id.keys | @original_by_id.keys).compact
- @all_values = all_values
+ @multiple_values = multiple_values
end
+ # Generates a hash of changes for a single attribute, with keys prefixed by a given string.
+ #
+ # @param attribute [Symbol] the attribute for which to return changes.
+ # @param key_prefix [String] the prefix to add to each key in the resulting hash.
+ # @return [Hash] a hash with key consisting of prefix and id, and value being two element array with changes.
def single_attribute_changes(attribute, key_prefix:)
attribute_changes(attribute)
.transform_keys { |id| "#{key_prefix}_#{id}" }
end
+ # Generates a hash of changes for multiple attributes, with keys prefixed by a given string.
+ #
+ # @param attributes [Array<Symbol>] the list of attributes for which to return changes.
+ # @param key_prefix [String] the prefix to add to each key in the resulting hash.
+ # @param grouped [Boolean] whether to group changes by id or return them flat. Default is false.
+ # @return [Hash] when not grouped, a hash with key consisting of prefix, id and attribute name, and value being two
+ # element array with changes. When grouped, a hash with key consisting of prefix and id, and value being
+ # a hash with keys being attributes names and values being two element array with changes.
def multiple_attributes_changes(attributes, key_prefix:, grouped: false)
attributes.each_with_object({}) do |attribute, result|
attribute_changes(attribute).each do |id, change|
@@ -85,10 +106,10 @@ module Acts::Journalized::Differ
def combine_journals(journals, attribute)
return unless journals
- if @all_values
+ if @multiple_values
values = journals.map(&attribute).sort
- if @all_values == :joined
+ if @multiple_values == :joined
values.join(",")
else
values.excluding(nil, "").presence
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 8ed905c7d66..1d57319b540 100644
--- a/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb
+++ b/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb
@@ -62,7 +62,7 @@ module JournalChanges
self,
association: :attachable_journals,
id_attribute: :attachment_id,
- all_values: :joined
+ multiple_values: :joined
).single_attribute_changes(
:filename,
key_prefix: "attachments"
@@ -87,7 +87,7 @@ module JournalChanges
self,
association:,
id_attribute: :custom_field_id,
- all_values: :joined
+ multiple_values: :joined
).single_attribute_changes(
:value,
key_prefix: "custom_fields"
@@ -126,7 +126,7 @@ module JournalChanges
self,
association: :agenda_item_journals,
id_attribute: :agenda_item_id,
- all_values: :joined
+ multiple_values: :joined
).multiple_attributes_changes(
%i[title duration_in_minutes notes position work_package_id],
key_prefix: "agenda_items"
diff --git a/spec/lib/acts/journalized/differ/association_spec.rb b/spec/lib/acts/journalized/differ/association_spec.rb
index 2167d2c1966..701e2d08cbd 100644
--- a/spec/lib/acts/journalized/differ/association_spec.rb
+++ b/spec/lib/acts/journalized/differ/association_spec.rb
@@ -71,7 +71,7 @@ RSpec.describe Acts::Journalized::Differ::Association do
end
let(:instance) do
- described_class.new(original, changed, association: :custom_values, id_attribute: :custom_field_id, all_values:)
+ described_class.new(original, changed, association: :custom_values, id_attribute: :custom_field_id, multiple_values:)
end
subject(:result) do
@@ -79,7 +79,7 @@ RSpec.describe Acts::Journalized::Differ::Association do
end
describe "requesting all values changes joined" do
- let(:all_values) { :joined }
+ let(:multiple_values) { :joined }
it "returns the changes" do
expect(result)
@@ -95,7 +95,7 @@ RSpec.describe Acts::Journalized::Differ::Association do
end
describe "requesting all values changes as array" do
- let(:all_values) { true }
+ let(:multiple_values) { true }
it "returns the changes" do
expect(result)
@@ -111,7 +111,7 @@ RSpec.describe Acts::Journalized::Differ::Association do
end
describe "requesting single value change" do
- let(:all_values) { false }
+ let(:multiple_values) { false }
it "returns the changes" do
expect(result)
@@ -156,7 +156,7 @@ RSpec.describe Acts::Journalized::Differ::Association do
described_class.new(original, changed,
association: :project_life_cycle_step_journals,
id_attribute: :life_cycle_step_id,
- all_values:)
+ multiple_values:)
end
describe "by default" do
@@ -168,7 +168,7 @@ RSpec.describe Acts::Journalized::Differ::Association do
end
describe "requesting joined changes for all values" do
- let(:all_values) { :joined }
+ let(:multiple_values) { :joined }
it "returns the flat changes" do
expect(result)
@@ -185,7 +185,7 @@ RSpec.describe Acts::Journalized::Differ::Association do
end
describe "requesting single value change" do
- let(:all_values) { false }
+ let(:multiple_values) { false }
it "returns the flat changes" do
expect(result)
@@ -212,7 +212,7 @@ RSpec.describe Acts::Journalized::Differ::Association do
end
describe "requesting joined changes for all values" do
- let(:all_values) { :joined }
+ let(:multiple_values) { :joined }
it "returns the grouped changes" do
expect(result)
@@ -233,7 +233,7 @@ RSpec.describe Acts::Journalized::Differ::Association do
end
describe "requesting single value change" do
- let(:all_values) { false }
+ let(:multiple_values) { false }
it "returns the grouped changes" do
expect(result)