Search code examples
ruby-on-railsmetaprogrammingbundler

How would I autoload a gem without having to restart Rails?


I've got a Rails 5 project and I'm deep in metaprogramming land.

One of the requirements is that some of the gem dependencies (ie the Gemfile) can be modified on the fly by the system administrator. This is restricted to a couple of gems that might be added, and they all have similar ::Rails::Engine structures.

I've been playing several strategies, including <Module>.autoload :<class_name>, <file path> but it doesn't load any dependencies.

What I'd like to do is dynamically load an updated Gemfile.lock (basically Bundler.setup) while rails is running, without having to restart the server.

Can I run bundle.install, modify the autoload paths, run Bundler.setup again, something like that?

Failing that, would a bundle install then rails restart disrupt much?


Solution

  • After a lot of investigation, it looks like it's not a safe thing to do. Bundler handles dependencies just fine, and is better than what I could write in this case. Also, Rails handles restarts nicely (touch restart.txt) so I'll just do that.