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

options to get single value changes, or non joined changes of association

Ivan Kuchin4fdb23a1ca4a
diff --git a/app/models/journable/with_historic_attributes.rb b/app/models/journable/with_historic_attributes.rb
index c60ebf1ab3b..6ff5d61b6aa 100644
--- a/app/models/journable/with_historic_attributes.rb
+++ b/app/models/journable/with_historic_attributes.rb
@@ -252,7 +252,8 @@ class Journable::WithHistoricAttributes < SimpleDelegator
historic_journable,
__getobj__,
association: :custom_values,
- id_attribute: :custom_field_id
+ id_attribute: :custom_field_id,
+ all_values: :joined
).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 28fbafffa35..05356acdb2c 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,10 +30,11 @@
module Acts::Journalized::Differ
class Association
- def initialize(original, changed, association:, id_attribute:)
+ def initialize(original, changed, association:, id_attribute:, all_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
end
def attribute_changes(attribute, key_prefix:)
@@ -82,8 +83,21 @@ module Acts::Journalized::Differ
end
def combine_journals(journals, attribute)
- # TODO: is there real case where there will be more than one value?
- journals.map(&attribute).sort.join(",") if journals
+ return unless journals
+
+ if @all_values
+ values = journals.map(&attribute).sort
+
+ if @all_values == :joined
+ values.join(",")
+ else
+ values.excluding(nil, "").presence
+ end
+ else
+ value = journals.first.send(attribute)
+
+ value unless value == ""
+ 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 f67a00cab32..d6baf7393c0 100644
--- a/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb
+++ b/lib_static/plugins/acts_as_journalized/lib/journal_changes.rb
@@ -61,7 +61,8 @@ module JournalChanges
predecessor,
self,
association: :attachable_journals,
- id_attribute: :attachment_id
+ id_attribute: :attachment_id,
+ all_values: :joined
).attribute_changes(
:filename,
key_prefix: "attachments"
@@ -85,7 +86,8 @@ module JournalChanges
predecessor,
self,
association:,
- id_attribute: :custom_field_id
+ id_attribute: :custom_field_id,
+ all_values: :joined
).attribute_changes(
:value,
key_prefix: "custom_fields"
@@ -123,7 +125,8 @@ module JournalChanges
predecessor,
self,
association: :agenda_item_journals,
- id_attribute: :agenda_item_id
+ id_attribute: :agenda_item_id,
+ all_values: :joined
).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 7e0e6c74ebf..f94f7f59633 100644
--- a/spec/lib/acts/journalized/differ/association_spec.rb
+++ b/spec/lib/acts/journalized/differ/association_spec.rb
@@ -71,19 +71,60 @@ RSpec.describe Acts::Journalized::Differ::Association do
end
let(:instance) do
- described_class.new(original, changed, association: :custom_values, id_attribute: :custom_field_id)
+ described_class.new(original, changed, association: :custom_values, id_attribute: :custom_field_id, all_values:)
end
- it "returns the changes" do
- expect(instance.attribute_changes(:value, key_prefix: "custom_field"))
- .to eq(
- "custom_field_2" => ["2", "22"],
- "custom_field_3" => [nil, "3"],
- "custom_field_4" => ["4", ""],
- "custom_field_5" => ["", "5"],
- "custom_field_6" => ["", "6"],
- "custom_field_9" => ["1,2", "2,3"]
- )
+ subject(:result) do
+ instance.attribute_changes(:value, key_prefix: "custom_field")
+ end
+
+ describe "requesting all values changes joined" do
+ let(:all_values) { :joined }
+
+ it "returns the changes" do
+ expect(result)
+ .to eq(
+ "custom_field_2" => ["2", "22"],
+ "custom_field_3" => [nil, "3"],
+ "custom_field_4" => ["4", ""],
+ "custom_field_5" => ["", "5"],
+ "custom_field_6" => ["", "6"],
+ "custom_field_9" => ["1,2", "2,3"]
+ )
+ end
+ end
+
+ describe "requesting all values changes as array" do
+ let(:all_values) { true }
+
+ it "returns the changes" do
+ expect(result)
+ .to eq(
+ "custom_field_2" => [["2"], ["22"]],
+ "custom_field_3" => [nil, ["3"]],
+ "custom_field_4" => [["4"], nil],
+ "custom_field_5" => [nil, ["5"]],
+ "custom_field_6" => [nil, ["6"]],
+ "custom_field_9" => [["1", "2"], ["2", "3"]]
+ )
+ end
+ end
+
+ describe "requesting single value change" do
+ let(:all_values) { false }
+
+ it "returns the changes" do
+ expect(result)
+ .to eq(
+ "custom_field_2" => ["2", "22"],
+ "custom_field_3" => [nil, "3"],
+ "custom_field_4" => ["4", nil],
+ "custom_field_5" => [nil, "5"],
+ "custom_field_6" => [nil, "6"],
+ "custom_field_8" => ["1", "2"],
+ "custom_field_9" => ["1", "3"]
+ )
+ end
end
end
@@ -114,7 +155,8 @@ RSpec.describe Acts::Journalized::Differ::Association do
let(:instance) do
described_class.new(original, changed,
association: :project_life_cycle_step_journals,
- id_attribute: :life_cycle_step_id)
+ id_attribute: :life_cycle_step_id,
+ all_values:)
end
describe "by default" do
@@ -125,17 +167,38 @@ RSpec.describe Acts::Journalized::Differ::Association do
)
end
- it "returns the flat changes" do
- expect(result)
- .to eq(
- "project_life_cycle_steps_1_active" => ["false", "true"],
- "project_life_cycle_steps_2_active" => [nil, "true"],
- "project_life_cycle_steps_3_active" => ["true", "false"],
- "project_life_cycle_steps_3_end_date" => ["", "2024-01-18"],
- "project_life_cycle_steps_3_start_date" => ["", "2024-01-17"],
- "project_life_cycle_steps_4_end_date" => ["2024-01-17", "2024-01-18"],
- "project_life_cycle_steps_4_start_date" => ["2024-01-16", "2024-01-17"]
- )
+ describe "requesting joined changes for all values" do
+ let(:all_values) { :joined }
+
+ it "returns the flat changes" do
+ expect(result)
+ .to eq(
+ "project_life_cycle_steps_1_active" => ["false", "true"],
+ "project_life_cycle_steps_2_active" => [nil, "true"],
+ "project_life_cycle_steps_3_active" => ["true", "false"],
+ "project_life_cycle_steps_3_end_date" => ["", "2024-01-18"],
+ "project_life_cycle_steps_3_start_date" => ["", "2024-01-17"],
+ "project_life_cycle_steps_4_end_date" => ["2024-01-17", "2024-01-18"],
+ "project_life_cycle_steps_4_start_date" => ["2024-01-16", "2024-01-17"]
+ )
+ end
+ end
+
+ describe "requesting single value change" do
+ let(:all_values) { false }
+
+ it "returns the flat changes" do
+ expect(result)
+ .to eq(
+ "project_life_cycle_steps_1_active" => [false, true],
+ "project_life_cycle_steps_2_active" => [nil, true],
+ "project_life_cycle_steps_3_active" => [true, false],
+ "project_life_cycle_steps_3_end_date" => [nil, Date.new(2024, 1, 18)],
+ "project_life_cycle_steps_3_start_date" => [nil, Date.new(2024, 1, 17)],
+ "project_life_cycle_steps_4_end_date" => [Date.new(2024, 1, 17), Date.new(2024, 1, 18)],
+ "project_life_cycle_steps_4_start_date" => [Date.new(2024, 1, 16), Date.new(2024, 1, 17)]
+ )
+ end
end
end
@@ -148,21 +211,46 @@ RSpec.describe Acts::Journalized::Differ::Association do
)
end
- it "returns the grouped changes" do
- expect(result)
- .to eq(
- "project_life_cycle_steps_1" => { active: ["false", "true"] },
- "project_life_cycle_steps_2" => { active: [nil, "true"] },
- "project_life_cycle_steps_3" => {
- active: ["true", "false"],
- end_date: ["", "2024-01-18"],
- start_date: ["", "2024-01-17"]
- },
- "project_life_cycle_steps_4" => {
- end_date: ["2024-01-17", "2024-01-18"],
- start_date: ["2024-01-16", "2024-01-17"]
- }
- )
+ describe "requesting joined changes for all values" do
+ let(:all_values) { :joined }
+
+ it "returns the grouped changes" do
+ expect(result)
+ .to eq(
+ "project_life_cycle_steps_1" => { active: ["false", "true"] },
+ "project_life_cycle_steps_2" => { active: [nil, "true"] },
+ "project_life_cycle_steps_3" => {
+ active: ["true", "false"],
+ end_date: ["", "2024-01-18"],
+ start_date: ["", "2024-01-17"]
+ },
+ "project_life_cycle_steps_4" => {
+ end_date: ["2024-01-17", "2024-01-18"],
+ start_date: ["2024-01-16", "2024-01-17"]
+ }
+ )
+ end
+ end
+
+ describe "requesting single value change" do
+ let(:all_values) { false }
+
+ it "returns the grouped changes" do
+ expect(result)
+ .to eq(
+ "project_life_cycle_steps_1" => { active: [false, true] },
+ "project_life_cycle_steps_2" => { active: [nil, true] },
+ "project_life_cycle_steps_3" => {
+ active: [true, false],
+ end_date: [nil, Date.new(2024, 1, 18)],
+ start_date: [nil, Date.new(2024, 1, 17)]
+ },
+ "project_life_cycle_steps_4" => {
+ end_date: [Date.new(2024, 1, 17), Date.new(2024, 1, 18)],
+ start_date: [Date.new(2024, 1, 16), Date.new(2024, 1, 17)]
+ }
+ )
+ end
end
end
end