How do I extend a class that is defined by a gem when I'm using rails 6 / zeitwerk?
I've tried doing it in an initializer using require
to load up the class first.
I've tried doing it in an initializer and just referencing the class to let autoloading load it up first.
But both of those approaches break auto-reloading in development mode.
I've tried putting it in lib/
or app/
, but that doesn't work because then the class never gets loaded from the gem, since my new file is higher up in the load order.
There is a similar question here, but that one specifically asks how to do this in an initializer. I don't care if it's done in an initializer or not, I just want to figure out how to do it some way.
What is the standard way of doing something like this?
I do have one nasty hack that seems to be working, but I don't like it (update: this doesn't work either. reloading is still broken):
the_gem_root = $LOAD_PATH.grep(/the_gem/).grep(/models/).first
require("#{the_gem_root}/the_gem/some_model")
class SomeModel
def my_extension
...
end
end
Bundler.require(*Rails.groups)
in application.rb, unless you have some custom setup. Can you post the relevant portion of your Gemfile? I'm wondering if your gem isn't in the development group or something. Which gem is this btw? – Trinettagem 'mailboxer', git: 'https://github.com/booleanbetrayal/mailboxer.git', branch: 'update_attributes-update'
I have this line inapplication.rb
Bundler.require(:default, Rails.env)
– Principate