0

I have implemented Audited and everything works well. The only thing I can't figure out is how to add an "audit_comment" when I am deleting a record. I can successfully add it when updating or creating, but I dont see anything that would allow me to add a comment on delete.

My example is that I can either delete a record directly or it gets deleted by a callback from a related association. So I want to add the comment to the audit based on the situation..."Removed directly by user" or "Removed through parent removal"

Am I missing something in the audited documentation?

cal1801
  • 135
  • 1
  • 11
  • Does it skip the normal Rails deletion callbacks? – Dave Newton Sep 27 '21 at 17:09
  • It doesn't skip them, all the normal rails deletion callbacks still work. But the way to add a comment to the audit record is by doing something like: record.update_attributes(name: 'something', audit_comment: 'Making a comment') and we can't do that on a delete/destroy call. – cal1801 Sep 27 '21 at 17:29
  • 1
    But you can update a column at any time by setting its `audit_comment` field. – Dave Newton Sep 27 '21 at 17:35

1 Answers1

2

You need to add the comment before destroy, something like this:

model.audit_comment = 'some random comment'
model.destroy

As per describe here https://github.com/collectiveidea/audited/blob/master/lib/audited/auditor.rb#L11

To store an audit comment set model.audit_comment to your comment before a create, update or destroy operation.

And more on the code here https://github.com/collectiveidea/audited/blob/master/lib/audited/auditor.rb#L303

def audit_destroy
  unless new_record?
    write_audit(action: "destroy", audited_changes: audited_attributes, comment: audit_comment)
  end
end
Vibol
  • 1,158
  • 1
  • 9
  • 23
  • Wow…sign that I need to take a step away. Of course I can do that. Thanks for pointing out that obvious solution… – cal1801 Sep 27 '21 at 18:36