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

A way to automatically limit the number of stored versions #213

Closed
snoblenet opened this issue Mar 22, 2013 · 11 comments
Closed

A way to automatically limit the number of stored versions #213

snoblenet opened this issue Mar 22, 2013 · 11 comments
Assignees
Milestone

Comments

@snoblenet
Copy link

I know there are tools for manually deleted versions created before a certain date.

I'm looking to configure PaperTrail globally so it only ever stores n many versions of each instance.

something like

config.version_limit = 5

If there are n versions stored, and PaperTrail wants to store a new one, it automatically deletes the oldest to make room.

Is this possible?

@matzke
Copy link

matzke commented Mar 22, 2013

+1

It would be nice, if i would not have to run cron or similar. When creating a new version, the older ones can be deleted by this event.

@ghost ghost assigned batter Mar 22, 2013
@batter
Copy link
Collaborator

batter commented Mar 22, 2013

Agreed this seems like it would be a nice feature, the caveat being that the version referencing a 'create' event (which could be important for some users if they use that feature) would also be automatically deleted, so we may need an option to preserve that one.

Perhaps #212 could be merged in a way that would allow it to serve this purpose, as well as the scenario proposed by @seanmarcia in the pull request. I realize these are certainly different scenario's, however, a module named Cleaner certainly sounds appropriate to handle this type of duty.

@batter
Copy link
Collaborator

batter commented Mar 22, 2013

By the way if you wanted a simple implementation of this until we have a more permanent solution, you could do something like this:

# config/initializers/paper_trail.rb
class Version < ActiveRecord::Base
  class << self
    attr_accessor :version_limit # maximum number of versions per item
  end

  after_create do
    return unless self.class.version_limit
    preceding_siblings = sibling_versions.preceding(self)
    preceding_siblings.last.destroy if preceding_siblings.size > self.class.version_limit
  end
end

You could then change the limit (globally) by using Version.version_limit = 5

@seanmarcia
Copy link
Contributor

I'd definitely like to see more use cases, I'd be happy to modify or provide additional cleaner methods. I realize that mine was a solution specific to a problem we were having.

@snoblenet
Copy link
Author

Thanks. My only use case is that in the admin part of the app I want to be able to fix any mistake that I make, including mistakes that I might not realise until I've made a few more changes to the instance. Keeping, say, the five most recent versions of an instance gives me plenty of insurance. Keeping, say, no more than the five most recent versions stops my database from swelling uncontrollably. I'll try the temporary solution listed above.

@batter batter closed this as completed in dbb4f28 May 23, 2013
@ashanbrown
Copy link

Just out of curiosity, is there a specific reason this limit ended up being global vs per model?

@batter
Copy link
Collaborator

batter commented Oct 1, 2014

No, not really, I think it was just the implementation that was suggested, but could have gone either way.

@ashanbrown
Copy link

OK, just wondering. If I need this feature, I'll just submit a pull
request. Sounds like you might be open to it.

Andrew

On Wed, Oct 1, 2014 at 11:52 AM, Ben Atkins notifications@github.com
wrote:

No, not really, I think it was just the implementation that was suggested,
but could have gone either way.


Reply to this email directly or view it on GitHub
#213 (comment)
.

@batter
Copy link
Collaborator

batter commented Oct 1, 2014

Always open to pull requests.

@vanboom
Copy link

vanboom commented Jan 14, 2017

+1 for model specific version limits

@jaredbeck
Copy link
Member

+1 for model specific version limits

Please see #915 if you'd like to help implement this feature or have something constructive to add.

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

7 participants