
At RailsConf 2009, David Heinemeier Hansson during his keynote mentioned that one of the central themes behind the Ruby on Rails and Merb merge was to “Have it your way”. Given the fact that Rails 3 is almost at a release candidate, I wanted to share my ideal Ruby on Rails setup.
My ideal application stack consists of Mongoid as my ODM, Haml as my template engine, jQuery as my JavaScript framework, RSpec as my testing framework, and factory_girl as my fixture replacement.
I will outline how to setup each of these individually for your Ruby on Rails 3 application.
To begin, generate a new Rails 3 application with the following command:
% rails new app_name -TOJ
The flags passed into the rails command will remove certain elements from the generation of our application.
Replacing ActiveRecord with Mongoid
Mongoid is an Object Document Mapper for MongoDB. It has a beautiful chaining criteria dsl for queries and is optimized for extremely large datasets.
To add mongoid to your project, add the following to your Gemfile:
gem ‘mongoid’, ‘>= 2.0.0.beta14’
gem ‘bson_ext’, ‘1.0.4’
Mongoid provides a generator to setup your project to create mongoid documents for model generation and setup a sample database connection configuration. To run this generator, first execute bundle install to ensure all missing dependencies are installed. Finally, run the mongoid:config generator:
% rails g mongoid:config
Replacing ERB with Haml
Haml was one of the primary reasons why I feel in love with working on Ruby on Rails applications. It just makes working with markup a pleasure.
Adding Haml to your project requires only 2 lines in your Gemfile:
gem ‘rails3-generators’
gem ‘haml’
The gem rails3-generators provides a variety of generators for Ruby on Rails 3 applications including factory_girl which we will be using later.
In config/application.rb, add the following to your application configuration:
config.generators do |g|
g.template_engine :haml
end
Replacing Prototype with jQuery
Using jQuery simplifies dealing with the DOM and provides all the tools necessary to do unobtrusive JavaScript in your web application. According to Hampton Catlin’s 2010 ruby survey, 78.8% of all ruby users prefer it over Prototype.
Run the following two commands in the root of your project directory:
curl -L http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js > \
public/javascripts/jquery.js
curl -L http://github.com/rails/jquery-ujs/raw/master/src/rails.js > \
public/javascripts/rails.js
Once you setup a layout file, include the two javascript files and your set.
Replacing Test::Unit with RSpec
With the latest beta release of RSpec 2, all generators and rake tasks are now exposed through a Railtie.
To replace Test::Unit as your default test framework, include the rspec-rails gem in both the :development and :test group in your Gemfile:
group :development, :test do
gem ‘rspec-rails’, ‘>= 2.0.0.beta.19’
end
The next step is to install rspec into the application. The rspec:install generator will create a spec directory and some skeleton files. Run the following in your project root:
% bundle install
% rails g rspec:install
Even though you are including RSpec as a development environment dependency, it is not completely loaded. It is only completely loaded when you invoke rails generate or rake. Read more details about this change at David Chelimsky’s post rspec-rails-2 generators and rake tasks – part II.
Replacing Fixtures with Factory Girl
The rails3-generators gem we included earlier also provides some factory_girl generators. This allows for the creation of a factory when we use the model generator from Rails.
Add a dependency to the factory_girl_rails to the test environment in your Gemfile:
gem 'factory_girl_rails', :group => :test
You will also need to manually configure your generators to use factory_girl in config/application.rb. Your generators configuration should now look like the following:
config.generators do |g|
g.template_engine :haml
g.fixture_replacement :factory_girl, :dir => "spec/factories"
end
Update: If you absolutely love this stack, be sure to check out rails-templater.




View Comments
I use Haml and always had to rename my files. Great tip on using the generators gem
Thanks Joey