Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

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.

Thursday, November 30, 2006

Implement: Installing Rails on Edgy

Just got my new System76 laptop (I will do a review once I play with it for a while) and one of the first things I did was get my Ruby on Rails environment up and running. It came pre-installed with Ubuntu 6.10 Edgy Eft.

Since Gems do not adhere to the upgrade path of Debian or Ubuntu, we will get around the package manager for this one component.

Ensure restricted and universe repositories are enabled.

I prefer a terminal:

sudo apt-get update
sudo apt-get install ruby rake ri rdoc irb
wget http://rubyforge.org/frs/download.php/11289/rubygems-0.9.0.tgz
tar xvzf rubygems-0.9.0.tgz
cd rubygems-0.9.0
sudo ruby setup.rb
sudo gem install rails --include-dependencies


Ruby on Rails is setup!

You will need a database, PostgreSQL or MySQL are both excellent. Lets setup the latter:

sudo apt-get install mysql-client mysql-server libmysql-ruby


Don't forget to configure your database - particularily setting the root MySQL password.

If you want to edit with Emacs, then continue:

sudo apt-get install emacs ruby-elisp


That is really it. You should be ready to create your first RoR project simply by entering:

rails myproject

Monday, November 27, 2006

Code: Ruby - Fun With ActiveSupport

If you have discovered the joy of coding in Ruby you will understand my enthusiasm! Not for years have I stayed up all night to learn a new programming language - excited at each new aspect, anxious to try what I have learned in a project. Ruby is simply a joy to use!

I am a newbie to Ruby but perhaps you will find something useful from my chaotic thoughts here.

One of these things is ActiveSupport - which many of you may know is a dependency within Rails. Yet you don't need to code a Rails project to enjoy this great library. For those of you not in the know, ActiveSupport is a collection of utility classes and standard library extensions.

Lets take a look at some of what you can do with ActiveSupport.

Inflections
>> "job".pluralize              #=> "jobs"
>> "job".singular #=> "job"
>> 23.ordinalize #=> "23rd"
>> 10.ordinalize #=> "10th"
>> "readers_best_work".humanize #=> "Readers best work"


Numeric
>> 10.bytes             #=> 10
>> 10.megabytes #=> 10485760
>> 10.terabytes #=> 10995116277760
>> 20.seconds #=> 20
>> 20.hours #=> 72000
>> 20.fortnights #=> 24192000
>> 20.months #=> 51840000
>> 20.minutes.ago #=> Mon Nov 27 11:53:23 Pacific Standard Time 2006
>> 20.minutes.from_now #=> Mon Nov 27 12:37:39 Pacific Standard Time 2006


Time
>> now = Time.now       #=> Mon Nov 27 12:19:21 Pacific Standard Time 2006
>> now.at_midnight #=> Mon Nov 27 00:00:00 Pacific Standard Time 2006
>> now.next_week #=> Mon Dec 04 00:00:00 Pacific Standard Time 2006
>> now.seconds_since_midnight #=> 44361.859
>> now.to_s #=> "Mon Nov 27 12:19:21 Pacific Standard Time 2006"
>> now.to_s(:short) #=> "27 Nov 12:19"
>> now.to_s(:rfc822) #=> "Mon, 27 Nov 2006 12:19:21 Pacific Standard Time"