Search code examples
ruby-on-railsacts-as-audited

Audited Gem Add Comment on Destroy


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?


Solution

  • 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