Make Rails Engines 2 reload in development mode

UPDATE 2: I’m now using a simpler method which appears to be working - albeit with one caveat, which is due to a conflict between the Asset Packager plug-in and the Engines AssetHelpers extensions (I’ve simply disabled those extensions).

In config/environments/development.rb:

config.after_initialize {
  Dependencies.load_once_paths = []
}

Tip found on this ticket on the old Rails Trac.


UPDATE: I’ve since stopped using this method. While it did allow for everything short of library files to be reloaded between requests, making the development process far less painful, I ran into a problem with ActiveRecord where deeply-nested (3 associations) models were behaving strangely on reload (they’d work fine first time, but not afterwards).


Since moving to Rails/Engines 2.1, I’ve been irritated with having to restart Mongrel each time I make a change to a controller or model in my engine. This wasn’t an issue in Engines 1.2 as it had a load of custom initialisation code, but with 2.x moving closer to the way Rails itself works - see this post on the rails plugin initialization process by Rick Olson for more details - it’s become a problem.

Unfortunately the solution Rick proposed in his post wasn’t working for me, but a thread on the Engines development mailing list has provided a solution - add the following to the init.rb file of your engine and you’ll no longer have to restart your web server to see changes in your files.

I’ve wrapped the code in a check to see if we’re running in the development environment, as in the majority of cases you’ll want things to work as normal in production.

if Rails.env == 'development'
  Rails::Plugin.class_eval do
     def reloadable!
       load_paths.each { |p| Dependencies.load_once_paths.delete(p) }
     end
  end
 
  reloadable!
end

Tags: , , , ,

2 Responses to “Make Rails Engines 2 reload in development mode”

  1. Tom Says:

    This didn’t work for me unfortunately. There is actually a new config option in Rails 2.1 which should automatically enable reloading of plugins:
    config.reload_plugins = true
    See this changeset, although that still didn’t work for my engine models :/

  2. Cameron Says:

    Hey Tom, that’s a shame it didn’t work for you - it’s working for me at the moment!

    I hadn’t seen the reload_plugins config option - unfortunately it’s causing asset_packager to fail, but it may be that plugin just needs updated.

    Will be keeping an eye on this - hopefully the plugin maintainers will update to make sure it works. :)

Leave a Reply