4

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 ?

Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
  • 1
    You _could_ submit a PR, wait for new release and just use that. It's not the fastest way, though. – Sergio Tulentsev Apr 05 '18 at 09:07
  • Alternatively, you could fork the gem, cut out what you don't need and use your frozen-in-time copy. It can be done quickly, but you either give up gem updates or gain maintenance problems. – Sergio Tulentsev Apr 05 '18 at 11:51

2 Answers2

4

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
AirWick219
  • 894
  • 8
  • 27
1

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?

MikeMarsian
  • 608
  • 1
  • 7
  • 21
  • 1
    Thanks, I've tried this and thought it would be a good approach, but it turned out the gem was doing a lot of things behind the scenes with its railties and cutting them completely wasn't a so good solution :S – Cyril Duchon-Doris Apr 05 '18 at 18:07