Setting up a Rails 3 app with RSpec, Cucumber, MongoDB and jQuery

With Rails 3 released, it comes with a set of new switches that let you opt out various stuff when generating apps. This makes it a bit easier to use alternatives to the default Active Record, Test::Unit etc. than it was with previous versions of Rails. Some of the options are:

-O, [--skip-activerecord]  # Skip Active Record files
-J, [--skip-prototype]     # Skip Prototype files
-T, [--skip-testunit]      # Skip Test::Unit files

You can get a complete list of all the available options by running the command:

1    $ rails new --help

Creating the app

1 $ sudo gem install rails
2 $ rails new YourApp -OJT
3 $ cd YourApp

The gem command will install the latest version of Rails 3. The rails command creates a new app named 'YourApp' without Active Record (-O), Test::Unit (-T), and the Prototype javascript files (-J).

Configuring the app and the required gems

To get things working you first have to install all the required gems. Since Rails 3 introduces Bundler, the gem management is as simple as it can get.

Open your Gemfile at the root of you application and specify the gems:

 1 source "http://rubygems.org"
 2 
 3 gem "rails", "3.0.0"
 4 gem "bson_ext"
 5 gem "mongo_mapper"
 6 
 7 group :test, :spec, :cucumber do
 8   gem "rspec"
 9   gem "rspec-rails", ">= 2.0.0.beta"
10   gem "capybara"
11   gem "cucumber"
12   gem "database_cleaner"
13   gem "cucumber-rails"
14   gem "spork"
15   gem "launchy"
16 end

Then, install all of them by simply running:

1 $ bundle install

Finally, you have to tell the generators that you are not using the default stuff. You do that by editing the config/application.rb and add these lines:

1 config.generators do |g|
2   g.orm             :mongo_mapper
3   g.template_engine :erb
4   g.test_framework  :rspec
5 end

Setting up the connection to MongoDB

Create a new file, config/initializers/mongo.rb with the following content:

1 logger = Logger.new("log/mongodb-#{Rails.env}.log")
2 MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => logger)
3 MongoMapper.database = "YourApp-#{Rails.env}"

If you use different databases in the different environments (e.g. dev, production), go ahead and put some if-else logic in here.

Generating the Cucumber and RSpec stuff

1 $ rails g rspec:install    
2 $ rails g cucumber:install --capybara --rspec --skip-database

That's it. You'll now have a folder features for your Cucumber features and a folder spec for your RSpec specs.

Adding jQuery

1 $ curl http://code.jquery.com/jquery-1.4.2.min.js > public/javascripts/jquery-1.4.2.min.js
2 $ curl http://github.com/rails/jquery-ujs/raw/master/src/rails.js > public/javascripts/rails.js

The first line downloads jQuery 1.4.2 and saves it to your public/javascripts/ folder. The second line downloads rails.js which is a wrapper around jQuery for Rails.

Finally, specify that the javascripts should be loaded as part of the default scripts by opening the file config/application.rb and change

1 config.action_view.javascript_expansions[:defaults] = %w()

to

1 config.action_view.javascript_expansions[:defaults] = %w(jquery rails application)

And, you're done!