Disable a gem's Railtie initializer

1.5k Views Asked by At

Is there a way to disable a railtie that is loaded by a gem by default ?

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:

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 ? without monkeypatching ? without patching the gem ?

2

There are 2 best solutions below

1
MikeMarsian On

This doesn't exactly answer your question, but you can always use

gem 'whenever', :require => false

in your Gemfile. This way, the gem won't be loaded and the initialization code won't run until you call

require 'whenever'

See: Bundler: What does :require => false in a Gemfile mean?

0
AirWick219 On

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