Search code examples
rubysinatrapaper-trail-gem

How to suppress warning on PaperTrail gem with Sinatra app?


DEPRECATION WARNING: PaperTrail.track_associations has not been set. As of PaperTrail 5, it defaults to false. Tracking associations is an experimental feature so we recommend setting PaperTrail.config.track_associations = false in your config/initializers/paper_trail.rb . (called from require at /Users/george/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68) Run options:

Since this is not a Rails app, there is no config/initializers/paper_trail.rb. Looked at https://github.com/airblade/paper_trail/blob/master/lib/generators/paper_trail/install_generator.rb, but didn't see a generator for the config file.

If I do

require 'paper_trail'
PaperTrail.config.track_associations = false

it still emits the warning.

Also tried:

def PaperTrail; end
PaperTrail.config.track_associations = false
require 'paper_trail'

This is a "Classic" Sinatra app.


Solution

  • The reason why this is happening is due the the call to require 'paper_trail' initializing a PaperTrail.config object: this line results in this file getting required, a part of which gets executed resulting in initializing the config singleton. We can solve this by first setting up the config object and then requiring the top-level paper_trail file later.

    # app.rb
    
    require 'sinatra'
    require 'paper_trail/config'
    
    config = PaperTrail::Config.instance
    config.track_associations = true
    
    # require models here
    Dir["#{Dir.pwd}/models/*.rb"].each { |file| require file }
    
    # rest of app code
    

    And the models won't need any change:

    # models/article.rb
    
    require 'paper_trail'
    
    class Article < ActiveRecord::Base
      has_paper_trail
    end
    

    Thus, when the require 'paper_trail' call gets executed, we already have the correct configuration setup and the warning won't be displayed.