How can I temporarily disable PaperTrail when reifying a version?
Asked Answered
L

4

6

I am using paper_trail for undo/redo functionality in my site and am having a problem when I call reify.save on a version in that on save and new PaperTrail::Version gets created.

Is there a way to turn off PaperTrail during the saving of a reified object?

I understand that PaperTrail.enabled = false is possible, but I don't want other changes being made a the same time to not be recorded.

My ideal solution would be something along the lines of:

PaperTrail.disable { version.reify.save }

Lyrate answered 21/9, 2016 at 11:46 Comment(0)
L
4

I once accomplished something similar by mixing in something like this:

def without_papertrail
  PaperTrail.disable
  yield if block_given?
  PaperTrail.enable
end

Then you can do something similar to your objective

without_papertrail { version.reify.save }
Lagoon answered 21/9, 2016 at 14:34 Comment(1)
This is exactly what I am using, but it still turns PaperTrail off completely instead of just turning it off for the objects being reified.Lyrate
S
3

You can disable paper trail for a particular model, using either of two syntaxes:

m = MyModel.find(123)
m.paper_trail.without_versioning do
  # No versioning of `m` happens in this block
end

Note: Since it is called on a model instance, it seems as though this might naturally disable versioning on just that instance, but this syntax disables versioning on the entire model.

The other syntax:

MyModel.paper_trail.disable
# No versioning of MyModel happens here
MyModel.paper_trail.enable
Satinwood answered 27/12, 2016 at 18:32 Comment(1)
The API was recently depcreated. The new API is: PaperTrail.request.disable_model(MyModel)Scale
S
2

As of today, gem version 10.3.0, the correct way to achieve this is, as per the gem documentation:

PaperTrail.request.disable_model(Banana)
# changes to Banana model do not create versions,
# but eg. changes to Kiwi model do.
PaperTrail.request.enable_model(Banana)
Silkaline answered 24/7, 2019 at 12:34 Comment(0)
P
0

from the readme: https://github.com/paper-trail-gem/paper_trail#7-testing

PaperTrail.enabled = false
Petra answered 24/4, 2019 at 2:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.