Archive for January, 2008

Migration: Adding/Removing columns are now much easier

You may have noticed by now, that in Rails 2.0 changeset 7422, you can specify columns you want to add/remove in your migration by passing attribute:type pairs to the migration generator.

For example, lets assume that we need to add a column ‘role’ in users table(User model). In this case generate a migration like:

script/generate migration AddRoleToUser role:string

Output:

class AddRoleToUser < ActiveRecord::Migration
  def self.up
    add_column :users, :role, :string
  end

  def self.down
    remove_column :users, :role
  end
end

Here AddRoleToUser plays the main role. ‘Add’ specifies the we want to add column(s) and ‘User’ separated by ‘To’ specifies the table.

Similarly, if we need to remove a column ‘role’ :

 script/generate migration RemoveRoleFromUser role:string

Output:

class RemoveRoleFromUser < ActiveRecord::Migration
  def self.up
    remove_column :users, :role
  end

  def self.down
    add_column :users, :role, :string
  end
end

Here RemoveRoleFromUser plays the main role. ‘Remove’ specifies the we want to remove column(s) and ‘User’ separated by ‘From’ specifies the table.

Isn’t it cool?

Ajax based online ruby API documentation

http://www.rubybrain.com/

Ruby Script for SVN commit notification with log message, list of updated files and readable colored SVN Diff

Some days ago I wrote a post about “SVN commit notification” which uses a perl script for sending commit notification with svn diff by mail. In this mail you can find svn diff from the last committed revision. I used to love this mail, soon I realized that it is a bit ugly and difficult to read. Also there were some important information missing. Like the name of user committing the code, the log message etc…

And then I started writing my own ruby script for same purpose but with some addition and modification. Commit Notification Script is that script, you can download and configure it with your SVN post commit hook as follows.

Add following line at the bottom of your post-commit file:

/usr/bin/ruby /var/www/repositories/project/hooks/commit-email.rb "$1"  "$2"

* Please remember to change the path of you commit-email ruby script.

Now open commit-email ruby file and modify the following section according to your requirement:

# You can edit below

# Subject prefix
sub = "[test_project@vinsol]"  # A project 'test_project' is maitained at vinsol

#list of users who will recieve commit notification
recipients = ["bansalakhil30.10@gmail.com", "akhil@vinsol.com"]

# email which will appear in from email field
from_user = "svn-notify@somedomain.com"

# your smtp settings here
ActionMailer::Base.smtp_settings = { :address => 'localhost', :port => 25, :domain => 'domain.com'}

# Do not edit below this line

You are done with that, now onwards whenever someone commits the code, you’ll get the commit notification mail like:

Commit Email Preview