Customizing Generators in Rails 3

January 13, 2010

As you probably already know, Rails 3 is just around the corner. There are some pretty nice features being added and one of them is the ability to customize the way the generators work. I personally prefer Haml over ERB, RSpec over Test::Unit and Factory Girl over Fixtures. So let's see how we can configure a Rails 3 app to do that.

First, follow Yehuda's instructions on how to create a Rails 3 app. Next, you have to tell Rails that you want to use Haml, RSpec and Factory Girl. First, add this somewhere in the Gemfile:

gem "haml"

only :test do
  gem "rspec"
  gem "rspec-rails"
  gem "factory_girl"
end

Then, re-run the bundler and initialize the Haml plugin:

$ gem bundle
$ bin/haml --rails .

Finally, you have to install the custom generators for the frameworks that you want to use. I found a repo on github that already had RSpec, so I forked it and added Haml and Factory Girl. Clone the repo into the lib/generators directory of your app:

$ git clone git://github.com/pjb3/rails3-generators.git lib/generators

Now, in the config/application.rb file in your app, near the bottom there is a section related to config.generators. Put this in that section:

config.generators do |g|
  g.template_engine :haml
  g.test_framework :rspec, :fixture => true, :views => false
  g.fixture_replacement :factory_girl, :dir => "spec/factories"
end

Here is where we reap the benefits of the modularity in Rails 3. What this says is that we want to use Haml as the template enging, we want to use RSpec as the test framework and we want to generate fixtures with our generated specs, but we don't want to generate view specs and that instead of fixtures, we actually want to use factory girl and we want the factories to be put into spec/factories. Whew! So does this all work?

$ script/generate rspec:install
$ script/generate scaffold person name:string dob:date age:integer male:boolean bio:text

At this point you should see that Rails has generated what we want, which is scaffolding that uses Haml for the views, RSpec for the tests and Factory Girl for the fixtures. Run the migrations, start the server and open the browser http://localhost:3000/people to see your scaffolding in action.

Now if you try to actually run rake spec, you'll get an error, at least I do. I'm not sure that RSpec 1.X is going to ever work with Rails 3. I think the intent is for RSpec 2 to be compatible with Rails 3, so keep an eye out for that.

Posted in Technology | Tags Rails, HAML, RSpec, FactoryGirl

Comments Comments Feed

1. RSpec 2 will only be compatible with Rails 3.X and up. We are in the process of working out the Rails 3 compatibility bits right now.

# Posted By Chad Humphries on Wednesday, January 13 2010 at 12:57 PM

2. Thanks a million for this write-up! I was struggling for a day to get this working. I didn't realize I had to use 'script/generate rspec:install' instead of 'script/generate rspec'. Just to make sure I understand this correctly: The generator will create our spec files for models, etc., but we can't run the specs until RSpec 3 is available? How does this affect Cucumber?

# Posted By Curt on Tuesday, January 19 2010 at 8:23 PM

3. Great post Paul! I have to admit that I had no idea from where to start with generators. Thanks

Just one note, this fork here looks more up-to-date:
http://github.com/pjb3/rails3-generators

I suggesting cloning the generators from there.

# Posted By Marco on Friday, January 22 2010 at 12:39 PM

4. Hi and thanks for the article! But for me, the model generator still creates normal fixture even if i put

g.fixture_replacement :factory_girl, :dir => "spec/factories"

line there. Where is documentation about these configuration? Cheers!

# Posted By Jakub on Saturday, January 23 2010 at 2:03 PM

5. Based on this article (thanks!) I abstracted the HAML generators into a self-contained package.

See http://github.com/psynix/rails3_haml_scaffold_generator for more details.

# Posted By JohnM on Sunday, February 7 2010 at 7:53 AM

6. Thank you for the self-contained Haml package JohnM. The one in the article wasn't working for me.

# Posted By Peng Zhong on Monday, February 8 2010 at 5:11 AM

7. You may want to update the bundle install command for those using bundler 0.9.x. Instead of 'gem bundle' you run 'bundle install'.

Thanks for the rails 3 writeup!

# Posted By BJ Neilsen on Tuesday, February 9 2010 at 4:14 PM

Add a Comment

(If you leave this blank, your IP address will be displayed instead)

(Optional, will not be displayed on the site)