Search code examples
ruby-on-railsrails-adminpaper-trail-gem

Customising Whodunnit information for Rails Admin using Paper Trail


I have been able to correctly append the name of a user into the Whodunnit field for actions by adding this to my application_controller

def user_for_paper_trail                                                                                     
    current_user ? current_user.name : 'Public user'                                                           
end

This works fine.

If a user makes a change using Rails Admin however, the whodunnit is still set to the user id.

Is there a method I can call that would tell paper trail what detail I want to store when a record is being updated via Rails Admin?


Solution

  • RailsAdmin has had some issues with this. You will find the following helpful: Display custom label for User in rails_admin paper_trail history.

    Also, because the field is supposed to store IDs (even though it's a string field), I'd keep the IDs and store something nonsensical for the 'Public user' such as '0' (referenced by a constant, of course). I would then override the whodunnit method to return 'Public user' in case super == '0'.

    That approach would be particularly helpful if you already have a bunch of versions with IDs in their whodunnit fields or if you have multiple users with the same name.

    P.S. To quickly confirm that RailsAdmin is completely ignoring / not ignoring the controller method you've defined, just force-raise an error in the method.