Wednesday, January 03, 2007

Engines are dead! Long live engines!

With the upcoming release of Rails 1.2, I thought it was a great opportunity to revisit the way that the engines plugin operates internally. Over the past year or so, the engines plugin has caused quite a stir (and picked up a few admirers, but despite misunderstandings and opinion pieces, people still seem to find it useful.

However, Rails is always changing, and so the engines plugin must change as well…

Engines 1.2: Engines are dead

When Rails 1.2 is released, the notion of ‘installing an engine’ will become meaningless. Let me make that really clear - there will be no meaningful distinction between an engine and a plugin anymore. To understand the implications of this, let’s look at what distinguished an engine from a plugin in the first place.

Init_engine.rb is gone - plugin load order control goes into Rails Core

The only distinguishing feature between an engine and a plugin was the replacement of a plugin’s init.rb file with init_engine.rb. That’s it. Everything else was just extra stuff that an plugin could contain. The reason why this was necessary is because the engines plugin needed to be loaded before any other plugins which used its features, but Rails itself couldn’t guarantee this. So, Engines.start took over that responsibility, and the notion of ‘an engine’ rather than ‘a plugin’ was born.

Thankfully, we can now do this in Rails natively. With the engines 1.2 release, init_engine.rb is gone in favour of using the supported config.plugins array:

  
  (in environment.rb)
  Rails::Initializer.run do |config|
    # other stuff
    config.plugins = ["engines", "your_plugin", "another_plugin", "and_so_on"]
    # etc ...
  end

They’re all just plugins now

Once the engines plugin is loaded, any plugins with app, db, or public_asset directories can automatically take advantage of the features that the engines plugin provides, without any further work from you. Controllers in your_plugin/app/controllers just work. Stylesheets, javascripts and images in your_plugin/public_assets subdirectories are made available automatically using the normal Rails helpers that you’re already used to.

Plugins can even contain migrations and routes, all shared with your main application in a way which promotes more efficient development without getting in your way or weighing down your application.

And Now…?

None of these features are officially released yet (we need Rails 1.2 before we can get that), but you can play around now by checking out the 1.2 release branch. Bear in mind that it’s never a good idea to run release candidate code in a production environment, naturally.

I’ll be posting more information about the changes and new features in Engines 1.2 here over the next few days; if you haven’t already, subscribe to the RSS feed. Happy coding!