Skip to content

Commit

Permalink
Merge pull request #696 from ycp3/enum-double-audit
Browse files Browse the repository at this point in the history
fix: normalize enum changes before filtering duplicate audits on touch
  • Loading branch information
danielmorrison authored Feb 27, 2024
2 parents 4ffc6c1 + 5ca65b8 commit 9b68cf3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ def audited_changes(for_touch: false, exclude_readonly_attrs: false)
all_changes.except(*self.class.non_audited_columns)
end

filtered_changes = normalize_enum_changes(filtered_changes)

if for_touch && (last_audit = audits.last&.audited_changes)
filtered_changes.reject! do |k, v|
last_audit[k].to_json == v.to_json ||
Expand All @@ -258,7 +260,6 @@ def audited_changes(for_touch: false, exclude_readonly_attrs: false)

filtered_changes = redact_values(filtered_changes)
filtered_changes = filter_encrypted_attrs(filtered_changes)
filtered_changes = normalize_enum_changes(filtered_changes)
filtered_changes.to_hash
end

Expand Down
8 changes: 8 additions & 0 deletions spec/audited/auditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,14 @@ class CallbacksSpecified < ::ActiveRecord::Base
expect(user.audits.last.action).to eq("update")
expect(user.audits.last.audited_changes.keys).to eq(%w[suspended_at])
end

it "updating nested resource through parent while changing an enum on parent shouldn't double audit" do
user.status = :reliable
user.companies_attributes = [{name: "test"}]
expect { user.save }.to change(user.audits, :count).from(1).to(2)
expect(user.audits.last.action).to eq("update")
expect(user.audits.last.audited_changes.keys).to eq(%w[status])
end
end

context "after updating" do
Expand Down
1 change: 1 addition & 0 deletions spec/support/active_record/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class Owner < ::ActiveRecord::Base
has_associated_audits
has_many :companies, class_name: "OwnedCompany", dependent: :destroy
accepts_nested_attributes_for :companies
enum status: {active: 0, reliable: 1, banned: 2}
end

class OwnedCompany < ::ActiveRecord::Base
Expand Down

0 comments on commit 9b68cf3

Please sign in to comment.