Tuesday, December 05, 2006

Implement: Starting a Rails Project

So you got Ruby on Rails (RoR) installed on your computer - now you want to start using it. There are a couple things I do when starting a project that may come in helpful for you. They may not be for everyone - so give them a read before you implement.

# lets start our project named 'myproject'
rails --database=mysql myproject
cd myproject

# I use Subversion, so lets import into a new svn project
svn import http://server/svn/myproject/trunk/ -m "adding rails structure"

# confirm all is good
cd ..
svn checkout http://server/svn/myproject/trunk mp

# tag your starting spot
svn copy http://server/svn/myproject/trunk http://server/svn/myproject/tags/initial-release -m "initial rails structure"
cd mp

# I don't want to keep my log and temporary files in svn
svn remove log/*
svn commit -m "removing log files from vc"
svn propset svn:ignore "*.log" log/
svn update log/
svn commit -m "setting a property to ignore *.log files"
svn propset svn:ignore "*" tmp/sessions tmp/cache tmp/sockets tmp/pids
svn update tmp/
svn commit -m "ignoring temporary files"

# now we move the default database.yml and ignore it
# for installations
svn move config/database.yml config/database.yml.example
svn commit -m "moving database file to example file"
svn propset svn:ignore "database.yml" config/
svn config/
svn commit -m "always ignore the installed database file"

One very kewl thing a RoR project can do is something called edge rails. There are various ways to make use of this idea and I like one solution that I will reveal here. Essentially edge rails embeds your rails module into the vendor/ folder of your project - you can take that a step further, using Subversion, and make the rails update separate from your project.

# Lets create an svn property to link to an external repository
rm -rf vendor/rails
svn propset svn:externals "rails http://dev.rubyonrails.org/svn/rails/trunk vendor/rails" vendor
svn update

And though the name may be called 'edge', it does not always need to be bleeding edge, if stability is more important than features. You can checkout a specific version of rails.

# instead of the last propset, use this for v1.1.6
svn propset svn:externals "rails http://dev.rubyonrails.org/svn/rails/tags/rel_1-1-6/ vendor/rails" vendor

Thats about it really. Oh, you can do a lot of other small tweaks as well. In fact there are some great ruby tools to automate this (as well as other migration or implementation) actions.

Don't forget to commit your final changes and then do another copy to tag this level of your source.

In an earlier posting, I had shown how to install Rails to Edgy. In this article I installed RubyGems manually, circumnavigating the DEB package. Using Edge Rails you could install the rails DEB with:

sudo apt-get install rails

At that point, create your project as normal, follow these instructions to configure your installation while also using Edge Rails with subversion. This means you can use the stable version of rails as that comes with Ubuntu but then use Edge rails within your project for a newer version. If you do this, be sure to run:

ruby vendor/rails/railties/bin/rails .

We do this so that your new project is upgraded to the latest rails version. This is not safe to do if you have already started to add content to this project.

No comments: