简体   繁体   English

禁用gem的Railtie初始化程序

[英]Disable a gem's Railtie initializer

Is there a way to disable a railtie that is loaded by a gem by default ? 有没有一种方法可以禁用默认情况下由gem加载的Railtie?

The developers of the gem did not make it modular and once putting the gem in the Gemfile, the require will automatically load the railties this way: gem的开发人员没有将其模块化,一旦将gem放入Gemfile中,require将自动以以下方式加载railies:

require 'some_gem'

module SomeGem
  module RailtieMixin
    extend ActiveSupport::Concern

    included do
      rake_tasks do
        require 'some_gem/rake_tasks'
      end

      initializer 'some_gem.configuration' do
        config.after_initialize do
          ...
        end
      end

      initializer 'some_gem.controller_methods' do
        ...
      end
    end
  end
end

I'd like to have some control, and ideally disable only the 'some_gem.controller_methods' , is it possible to do this ? 我想拥有一些控制权,并且理想情况下仅禁用'some_gem.controller_methods' ,可以这样做吗? without monkeypatching ? 没有胡闹? without patching the gem ? 没有修补宝石?

This is probably not a good idea but if you must do that you can do something like the following to find the initializer instance and filter out the initializer that you don't want before the initializer is run. 这可能不是一个好主意,但是如果必须这样做,则可以执行以下类似操作来找到初始化程序实例,并在运行初始化程序之前过滤掉不需要的初始化程序。

module MyModule
  class Railtie < Rails::Railtie
    config.before_configuration do
      Rails.application.initializers.find { |a| a.name == 'some_gem.configuration'}.context_class.instance.initializers.reject! { |a| a.name == 'some_gem.configuration'}
    end
  end
end

This doesn't exactly answer your question, but you can always use 这不能完全回答您的问题,但是您始终可以使用

gem 'whenever', :require => false

in your Gemfile. 在您的Gemfile中。 This way, the gem won't be loaded and the initialization code won't run until you call 这样,除非您调用,否则gem不会被加载,并且初始化代码也不会运行

require 'whenever'

See: Bundler: What does :require => false in a Gemfile mean? 请参阅: Bundler:Gemfile中的:require => false是什么意思?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM