From 140b89fb3b4cf48b2f67ca12193808b55155df7f Mon Sep 17 00:00:00 2001 From: Thomas Scalise Date: Wed, 29 May 2024 09:58:16 +0200 Subject: [PATCH] Changes the way old audits are combined. Now we keep the first value of each attribute and the last one, instead of the last 2. --- lib/audited/auditor.rb | 10 +++++++++- spec/audited/auditor_spec.rb | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/audited/auditor.rb b/lib/audited/auditor.rb index c9c867ae..766e62ce 100644 --- a/lib/audited/auditor.rb +++ b/lib/audited/auditor.rb @@ -188,7 +188,15 @@ def own_and_associated_audits # Combine multiple audits into one. def combine_audits(audits_to_combine) combine_target = audits_to_combine.last - combine_target.audited_changes = audits_to_combine.pluck(:audited_changes).reduce(&:merge) + combine_target.audited_changes = audits_to_combine.pluck(:audited_changes).reduce do |h1, h2| + h1.merge(h2) do |_key, this, other| + if this.is_a?(Array) && other.is_a?(Array) + [this.first, other.last] + else + other + end + end + end combine_target.comment = "#{combine_target.comment}\nThis audit is the result of multiple audits being combined." transaction do diff --git a/spec/audited/auditor_spec.rb b/spec/audited/auditor_spec.rb index a8af7bcb..cdd455fd 100644 --- a/spec/audited/auditor_spec.rb +++ b/spec/audited/auditor_spec.rb @@ -706,7 +706,7 @@ class CallbacksSpecified < ::ActiveRecord::Base audits = user.audits expect(audits.count).to eq(3) - expect(audits[0].audited_changes).to include({"name" => ["Foobar", "Awesome"], "username" => ["brandon", "keepers"]}) + expect(audits[0].audited_changes).to include({"name" => ["Brandon", "Awesome"], "username" => ["brandon", "keepers"]}) expect(audits[1].audited_changes).to eq({"activated" => [nil, true]}) expect(audits[2].audited_changes).to eq({"favourite_device" => [nil, "Android Phone"]}) end