Installing Gnome Do

June 26th, 2008

I have been using Quicksilver on my MacBook for a couple of months now, kinda used to it now since I can launch applications and do most of the daily tasks without having to point-and-click too much.

What if you want something similar that runs on your Linux system? Ladies and gentlemen, say hello to Gnome Do.

gnome-do.png

To install it, add the following PPA repositories into your sources list:

deb http://ppa.launchpad.net/do-core/ubuntu hardy main
deb-src http://ppa.launchpad.net/do-core/ubuntu hardy main

Reload Synaptic, then search for Gnome-Do. If you are a CLI guy, do this:

sudo apt-get update
sudo apt-get install gnome-do

When installed, you can Gnome-Do from Applications > Accessories > Gnome Do.

The default hotkey is <Super>-Space, you can easily edit this from Gnome-Do’s Preferences. There’s also easy access to all the official/third-party plugins from the Preferences dialog.

gnome-do-prefs.png

A few plugins that I find useful:

  • Files and Folders: indexes files in your specified directories.
  • Flickr: upload photos to Flickr
  • GNOME Dictionary: add ‘define’ function to Do
  • GNOME Screenshot: take screenshots of whole screen or current window, with delay
  • Twitter: tweet from Do!

Ruby on Rails: Polymorphic Association

June 19th, 2008

While working on our industrial project today, we stumbled across this situation where we need a base class (Document) that are able to refer to different type of documents (ie. Report, Screen, etc).

We found two ways of dealing with this situation, one of which is by using Single Table Inheritance (STI); and the other one we are fond of, is called Polymorphic Association.

According to [1], Single Table Inheritance is:

… an inheritance hierarchy of classes as a single table that has columns for all the fields of the various classes

We decided to go with Polymorphic Association, because we wanted different classes for different subtypes of documents. We wanted to avoid having a really large single table to store everything, and using STI might leave us with many empty fields in a row.

A little explanation of Polymorphic Association, based on my understanding. Polymorphic Association uses two fields in the base class as the foreign keys, to refer to different subclasses. It stores the ID of the subclass, and the type (model).

This is the relationship we hope to achieve:

Inheritance

I have created models for the above classes:

script/generate model Document
script/generate model Report
script/generate model Screen
script/generate model Process

We start by declaring the interface, and associate them:

We declare the interface in models/document.rb:

class Document < ActiveRecord::Base
  belongs_to :content, :polymorphic => true
end

Then, we associate the subclasses with the interface.

In models/process.rb:

class Process < ActiveRecord::Base
  has_one :document, :as => :content
end

In models/report.rb:

class Report < ActiveRecord::Base
  has_one :document, :as => :content
end

In models/screen.rb:

class Screen < ActiveRecord::Base
  has_one :document, :as => :content
end

Now, in Document’s migration file, create the table:

create_table :documents do |t|
  t.string :title
  t.string :author
  t.references :content, :polymorphic => true
  t.timestamps
end

In the rest of the migration files (create_report, create_screen, create_process), fill in the appropriate columns for your table, and finally run the migration.

rake db:migrate

What you will see in your database table (documents) is that Rails created two fields, namely content_id and content_type in your table. Those are the fields used to reference the subclasses.

So far, this method is able to accomplish what we had in mind.

If anyone out there have a better solution to this problem, drop me a comment. Cheers.

ref:

  1. P of EAA: Single Table Inheritance

Firefox 3: Help Set A World Record

June 18th, 2008
Download Day

Firefox 3 is scheduled to be released in about an hour.

The guys at SpreadFirefox wanted to set a world record for the most software downloaded in 24 hours, so pledge yourself and help make it happen!

Firefox 3 has more than 15,000 improvements, and its much faster than the previous version, plus it does not have the memory problems previous versions had.

Ruby on Rails: Many-to-Many Relationship

June 17th, 2008

In Rails, there are two ways (that I know of) to define many-to-many relationship, one of which is by using has_and_belongs_to_many (a.k.a HABTM), and the other one is by using through association.

Differences between the two methods can be seen in this comparison. Episode 47 of Railscasts also give a good understanding of the two methods, which is how I learned about the two different methods to define many-to-many relationship.

Our examples below are based on the following scenario (a simplified version of the project I am working on):

An application can have many categories.

A category has many applications.

We need some sort of join table to define the categorisation.

I have generated two models: app and category.

script/generate model app name:string
script/generate model category name:string

HABTM Method

In order to generate the join table, I did:

script/generate migration create_apps_categories

In your migration file:

def self.up
  create_table :apps_categories, :id => false do |t|
    t.references :app
    t.references :category
    t.timestamps
  end
end

In models/app.rb:

class App < ActiveRecord::Base
  has_and_belongs_to_many :categories
end

In models/category.rb:

class Category < ActiveRecord::Base
  has_and_belongs_to_many :apps
end

Through Association

A different approach is used here, particularly useful when you need extra fields in your join table.

Create a model called Categorisation:

script/generate model categorisation app:references category:references

Now, in models/categorisation.rb:

class Categorisation < ActiveRecord::Base
  belongs_to :app
  belongs_to :category
end

In models/app.rb:

class App < ActiveRecord::Base
  has_many :categorisations
  has_many :categories, :through => :categorisations
end

Finally, in models/category.rb:

class Category < ActiveRecord::Base
  has_many :categorisations
  has_many :apps, :through => :categorisations
end

All these information are based on my understanding, I don’t know if its the right way of doing it, since I am fairly new to Rails. Leave a comment if there’s a better way, or if I am doing it wrongly.

Unable to connect to MySQL from PHP (on Mac OS X)

June 13th, 2008

When I tried to connect to MySQL from PHP, using mysql_connect, it gives me the “Can’t connect to MySQL through socket” error. And I am definitely sure, I added the user into MySQL, and had the correct password.

mysql_connect('localhost', 'username', 'very_encrypted_password');

The thing is, it works if I use 127.0.0.1 instead of localhost, strange..

MySQL:

$ mysql --version
mysql  Ver 14.12 Distrib 5.0.51a, for apple-darwin9.2.2 (i686) using  EditLine wrapper

PHP:

$ php --version
PHP 5.2.5 (cli) (built: Mar  4 2008 22:57:15)

After asking for help from Colin, who gave me a head start and some google’ing, I found a solution.

Quoted from the site [1]:

One problem that has come about with MySQL and Leopard is the location of the mysql.sock file. Previously, the default location for this file was in the /tmp directory. That location has now moved to the /var/mysql directory. PHP will look for it there. Unfortunately, the default location from the MySQL will still place it in the old location.

First of all, stop MySQL. Then open up /etc/my.cnf:

sudo vim /etc/my.cnf

In my case, I only had a couple of lines there:

[mysqld]
binding = 127.0.0.1

Add these line, to the appropiate headings:

[mysqld]
socket = /var/mysql/mysql.sock
[client]
socket = /var/mysql/mysql.sock

Then, create the directories and set the proper permission:

sudo mkdir /var/mysql
sudo chown _mysql /var/mysql

Finally, restart MySQL, and try it out!

ref:

  1. Working with PHP 5 in Mac OS X 10.5 (Leopard)