Skip to content

Thread safe disabling of PaperTrail in a block

Jay Dorsey edited this page Mar 22, 2024 · 2 revisions
# This appears to be thread-safe, do your own checking to confirm
PaperTrail.request(enabled: false) do
  # Do some work you don't want PaperTrail to store versions for
end

Ruby on Rails with RSpec

For Rails applications using RSpec, you can disable with:

# config/environments/test.rb

RSpec.configure do |config|
  ...
  config.after_initialize { PaperTrail.request.enabled = false }
end

To allow papertrail to be conditionally enabled for a spec or set of specs you can use tags:

# spec/rails_helper.rb

RSpec.configure do |config|
  ...
  config.around(:each, :papertrail) do |example|
    PaperTrail.request(enabled: true) { example.call }
  end
end
# any/spec/file_spec.rb

RSpec.describe 'MySpec', :papertrail do
  # Also works on individual test blocks with it, context, etc.
end

This has been tested running using parallel_tests and appears to behave as expected (papertrail is enabled for tagged specs)