Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track custom events in paper_trail?

I am using paper_trail for audit trail. Along with create, update and delete events I want to track few custom events like view(record), sent(email) etc. How can we introduce such custom events while auditing a model?

like image 514
Amit Patel Avatar asked Mar 24 '12 12:03

Amit Patel


2 Answers

I have found a tweak to add custom events in paper_trail managed Versions:

Version.create(item_type: "Campaign", item_id: campaign.id, event: "Sent")

Maybe this is not right solution, but it helped me to achieve the goal. I would like to explore paper_trail more to find a better solution.

like image 195
Amit Patel Avatar answered Oct 12 '22 23:10

Amit Patel


Following the paper trail flow, and having paper trail hooked to your touch events:

record.paper_trail_event = 'notified'
record.touch

If you don't want to have that hook in place you can:

record.versions.create!(event: 'notified')

The main problem with the second approach is that it won't apply any of the PaperTrail scoped params, nor any other dynamic field you may have defined for that model PaperTrail config.

You will need to set those manually. For the request.whodunnit it would be like:

record.versions.create!(event: 'notified', whodunnit: current_user.id)
like image 26
robertodecurnex Avatar answered Oct 12 '22 23:10

robertodecurnex