Disable a gem's Railtie initializer
Asked Answered
C

2

6

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 ?

Carbonyl answered 5/4, 2018 at 9:3 Comment(2)
You could submit a PR, wait for new release and just use that. It's not the fastest way, though.Linearity
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.Linearity
S
5

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
Smalltime answered 18/10, 2018 at 20:55 Comment(0)
L
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?

Luteal answered 5/4, 2018 at 13:55 Comment(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 :SCarbonyl

© 2022 - 2024 — McMap. All rights reserved.