如何使用 Sinatra 应用程序抑制 PaperTrail 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:

由于这不是 Rails 应用程序,因此没有 config/initializers/paper_trail.rb。查看了 https://github.com/airblade/paper_trail/blob/master/lib/generators/paper_trail/install_generator.rb,但没有看到配置文件的生成器。

如果我这样做

require 'paper_trail'
PaperTrail.config.track_associations = false

它仍然发出警告。

也尝试过:

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

这是一个 "Classic" Sinatra 应用程序。

发生这种情况的原因是调用 require 'paper_trail' 初始化 PaperTrail.config 对象:执行 this line results in this file getting required, a part of which 导致初始化 config 单例。我们可以通过首先设置配置对象然后在稍后请求顶级 paper_trail 文件来解决这个问题。

# 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

并且模型不需要任何更改:

# models/article.rb

require 'paper_trail'

class Article < ActiveRecord::Base
  has_paper_trail
end

因此,当执行 require 'paper_trail' 调用时,我们已经有了正确的配置设置,不会显示警告。

我在 class Application 的定义中将 PaperTrail.config.track_associations = false 添加到 config/application.rb