Monday, June 28, 2010

Rspec with Rails 3

First of all, we will need to setup Rspec on Rails 3,
sudo gem install rspec
sudo gem install rspec-rails --pre

Then we include them in our gemfile,
gem 'rspec'
gem 'rspec-rails'

Then we will run the generator:install,
rails g rspec:install
which will provide us with all the files/directory that we need to use Rspec

For a more robust TDD/BDD, we could install autotest. This would assure that each time we edit the test file, the Rspec test will be conducted automatically without the need of us typing the command out.
gem install autotest
gem install autotest-fsevent
gem install autotest-growl (For some reason i still couldn't set it up to work with Growl for OSX)

Open the autotest file,
mate ~/.autotest

Add these 2 lines to the file
require "autotest/fsevent"
require "autotest/growl"

Kick it off with
autotest

If we are testing model, be sure to do
rake db:test:prepare
or Rspec will complain.

Friday, June 18, 2010

Upgrading a Rails 2 App to Rails 3

Most of us have Rails 2 Application in our stash and it would be nice to support the latest Rails 3 since some day the Rails 2 methods and architecture will be deprecated.

Some of the useful reads are Simon Carletti's Blog, Rails Dispatch, and omgbloglol.

So what you can do to upgrade an existing app is by running a (rails generate new app) on an existing Rails 2 application.

Say your previous Rails 2 app is in a folder titled "blogApp", then run
rails blogApp


It doesn't exactly forcefully overwrite the entire folder, but it prompts you if you wanna keep this particular file or if you wanna update it, and it also adds all the files that Rails 3 need like gemfile bundler, new rails.rb in the script folder, and so on.

Based on omgbloglol, it would probably be a save bet to overwrite these files:-
  • Rakefile
  • README
  • config/boot.rb
  • public/404.html (unless you’ve customized it)
  • public/500.html (unless you’ve customized it)
  • public/javascripts/* (if you don’t have a lot of version dependent custom JavaScript)
  • script/* (they probably wouldn’t work with the new Rails 3 stuff in their old form anyhow)
And depending on our needs, we might or might not wanna overwrite these :-
  • .gitignore (unless you don’t really care; the new standard one is pretty good)
  • app/helpers/application_helper.rb
  • config/routes.rb
  • config/environment.rb
  • config/environments/* (unless you haven’t touched these as many people don’t)
  • config/database.yml
  • doc/README_FOR_APP (you do write this, don’t you?)
  • test/test_helper.rb
By doing that, we pretty much have the boilerplate of a Rails 3 application and all we have to do is tweak the codes to conform to Rails 3.
  1. Moving gem to gemfile Bundler
  2. Moving the configurations from config/environment.rb to config/application.rb
  3. Routers new syntax
  4. etc..
And you can find the steps for the upgrade at omgbloglol, which to me is pretty comprehensive.

Let the Rails 3 Migration begin.