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

How to extend the Version class #231

Closed
TvL2386 opened this issue Jun 7, 2013 · 8 comments
Closed

How to extend the Version class #231

TvL2386 opened this issue Jun 7, 2013 · 8 comments

Comments

@TvL2386
Copy link

TvL2386 commented Jun 7, 2013

Hi guys,

I want to add a way to create a flexible audit trail for my application.
What I want is to select Version objects based on created_at and item_type.
Best way to do this imho, is to add a scope to the Version class.
I've added the file app/models/version.rb and started to extend the Version class.

When I have this, I'd like to add more logic in the Version class to built an audit trail.

In development the changes are not picked up on. I wonder why this does not work.
I'm also wondering if I'm trying to do something evil here.

Please help me out!

Kind regards,
Tom

@batter
Copy link
Collaborator

batter commented Jun 10, 2013

@TvL2386 - Please read the README section on Storing Metadata for an example of how to add an initializer for PaperTrail with which you can add / modify the Version class.

The class should get placed in config/initializers/paper_trail.rb.

@TvL2386
Copy link
Author

TvL2386 commented Jun 11, 2013

The problem of development not picking up on changes remains if I put my version class definition in config/initializers/version.rb. I have to restart development everytime to see the result.

@batter
Copy link
Collaborator

batter commented Jun 11, 2013

That's true. But it is the way to do it for the time being. Maybe we can look at trying to put a reloader in for version 3, I'm not sure what that would entail.

@abepetrillo
Copy link

I'm also looking for an alternative to this, defining methods in initialisers isn't my idea of fun. I actually found this on a project I was working on and decided to move it. If I find a way around it I'll let you guys know :)

@TvL2386
Copy link
Author

TvL2386 commented Jul 2, 2013

Highly appreciated abepetrillo!

@abepetrillo
Copy link

Unfortunately I've ran out of time a little bit, it doesn't solve the having to restart the server issue, but it fixes the location, I put this in my initializer:

require "#{Rails.root}/app/models/version.rb"

So the model is in the right place, and it has a corresponding spec/models/version_spec.rb.

It might be worth looking into requiring the class definition in config/application.rb, but I doubt it'll help!

@joshuapinter
Copy link

joshuapinter commented Nov 11, 2020

FYI, in 2020 as of Version 10 (likely around Version 4+), here's how you extend PaperTrail::Version to add some instance methods:

config/initializers/paper_trail.rb

# Extend PaperTrail::Version with some of our own methods we use.
#
module PaperTrail
  class Version < ActiveRecord::Base # Cannot use ApplicationRecord here so you may need to disable Rubocop for this line with `rubocop:disable Rails/ApplicationRecord`.

    include PaperTrail::VersionConcern

    def no_change?
      self.changeset.empty? 
    end

  end
end

@pepetorres1998
Copy link

There is a snippet of code in the docs that misleads to make PaperTrail::Version abstract by adding it inside app/models/paper_trail instead of config/initializers/

https://github.com/paper-trail-gem/paper_trail#configuration

# app/models/paper_trail/version.rb
module PaperTrail
  class Version < ActiveRecord::Base
    include PaperTrail::VersionConcern
    self.abstract_class = true
  end
end

I already forked the gem, is it okay if I send a pull request to change that README.md part of the file?
To actually make it abstract it needs to be on config/initializers/paper_trail.rb

# config/initializers/paper_trail.rb
module PaperTrail
  class Version < ActiveRecord::Base
    include PaperTrail::VersionConcern
    self.abstract_class = true
  end
end

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

5 participants