Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whodunnit == User object? #522

Closed
toobulkeh opened this issue Apr 16, 2015 · 5 comments
Closed

Whodunnit == User object? #522

toobulkeh opened this issue Apr 16, 2015 · 5 comments

Comments

@toobulkeh
Copy link

I really like Audited's and PublicActivity's methods of tying to the user who performed the edit.. even if that user could eventually be deleted.

I think it would be beneficial if papertrail supported something more than a string. Essentially just a polymorphic association instead of just a single string.

@batter
Copy link
Collaborator

batter commented Apr 16, 2015

You are suggesting adding a custom accessor on the PaperTrail::VersionConcern module?

@toobulkeh
Copy link
Author

Hey Ben,
Not quite. I'm suggesting that the VersionConcern also belongs_to :user, :polymorphic => true so that instead of just :whodunnit we have :user_type and :user_id. Ala PublicActivity. This way we can do more specific user filtering for displaying audit logs based on User data instead of storing it in the current unassociated mechanism.

Here's how PublicActivity implements it: https://github.com/pokonski/public_activity/blob/master/lib/public_activity/orm/active_record/activity.rb#L12

@jaredbeck
Copy link
Member

Dan, I think you can accomplish this by implementing your own PaperTrail::Version class:

# app/models/paper_trail/version.rb
module PaperTrail
  class Version < ActiveRecord::Base
    include PaperTrail::VersionConcern
    belongs_to :user, :polymorphic => true
  end
end

You'd store the user_id in version metadata I guess.

I have some concerns about building this into paper_trail the gem.

  1. Not everyone has a User model
  2. Most people do have a User model, but don't need a polymorphic association
  3. How would it work when the associated user is deleted?
  4. What happens when people want to configure the association, e.g. passing other options to belongs_to?

@jaredbeck
Copy link
Member

Dan, I'm going to close this, but please let me know if my suggestion doesn't work for you, and I'll be happy to reopen it.

@toobulkeh
Copy link
Author

Great thoughts and notes @jaredbeck -- I really appreciate the feedback. I think that workaround will work, though it's a bit wordy (a few helper methods should solve that though).

If you leave the association as a polymorphic one, then anyone can use the relationship for any reason. That should answer your questions 1,2,4. PublicActivity handles deleted records quite nicely, with the default recipe checking on .present? and suggesting the user respond with which has since been deleted in the visual / view / partial layer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants