Upgrading Rails Engines to Rails 3.2

Wtih the recent release of Rails 3.2, we have been trying to upgrade all our engines at Sage to use Rails 3.2. Everything works great, except scaffolding.

If, for example, I run this command:

bundle exec rails g scaffold thing name:string

And then:

git status

I see the following:

# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#	spec/dummy/app/assets/javascripts/things.js
#	spec/dummy/app/assets/stylesheets/scaffold.css
#	spec/dummy/app/assets/stylesheets/things.css
#	spec/dummy/app/controllers/things_controller.rb
#	spec/dummy/app/helpers/things_helper.rb
#	spec/dummy/app/models/thing.rb
#	spec/dummy/app/views/things/
#	spec/dummy/db/
#	spec/dummy/log/development.log

As you can see, all my generated files have been put into my dummy application.

The reason for this is that the way engine generator tasks are discovered has changed in Rails 3.2. In 3.1, the engine’s script/rails would just load the dummy’s script/rails file, and the ENGINE_PATH modified to make sure everything goes in the correct place.

To fix this, just change the content of script/rails to the following:

#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

ENGINE_ROOT = File.expand_path('../..', __FILE__)
ENGINE_PATH = File.expand_path('../../lib/your_engine_name/engine', __FILE__)

require 'rails/all'
require 'rails/engine/commands'

Of course, remember to replace your_engine_name by the name of your engine.

Then if we try again:

bundle exec rails g scaffold thing name:string

And look at git:

git status

We see:

# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#	app/assets/javascripts/rails_engine31/
#	app/assets/stylesheets/rails_engine31/
#	app/assets/stylesheets/scaffold.css
#	app/controllers/rails_engine31/
#	app/helpers/rails_engine31/
#	app/models/
#	app/views/rails_engine31/
#	db/

So now we have the correct paths for our generated files.