Friday, July 31, 2009

Make Faker work with Factory Girl

If you use the code below, faker will not churn out unique names

Factory.define :user do |u|
u.first_name Faker::Name.first_name
u.last_name Faker::Name.last_name

However putting curly braces around faker makes it work!

Factory.define :user do |u|
u.first_name { Faker::Name.first_name }
u.last_name { Faker::Name.last_name }

Thursday, July 30, 2009

Has many association in Factory Girl

Factory.define :user do |user| "My Name"
user.addresses {|addresses| [addresses.association(:address)]}

Google styling scheme for tables

This is what I implemented after studying the CSS for tables that Google uses. table {
background: white none repeat scroll 0 0;
border-collapse: collapse;
cursor: default;
font-family: arial,helvetica;
font-size: 10pt;
margin: 0;
} table th {
background: #FFFFFF url(title-bg.gif) repeat-x scroll left bottom;
font-weight: bold;
text-align: center;
border: 1px solid #EEE;
padding: 6px;
} table td {
border: 1px solid #EEE;
padding: 2px 3px;
} table {
color: #666;
text-align: right;
} table td {
background: #FAFAFA;

How to handle uninitialized constants in Rails

I had this in my code.

klass = Object.const_get(params[:controller].singularize.camelize)

Which was working fine until I had a controller that doesn't have a matching model. Eg. Home

NameError: uninitialized constant Home

To handle this I needed to do a rescue to the error

rescue NameError
return true

Seed data into development database using Bootstrapper and Factory Girl

There's only so much development an application using unit tests and cucumber before we need to fire up the application on the browser in development mode. Trouble is we've written so many associated models and there did not contain any data in development database.

So comes seeding of data into my development database. I could do one of the following:

- Manually do SQL insertions of records into database
- Manually create and save objects in "script/console"
- Setup rake task to create records
- Use migration to insert seed data
- Use a SystemSettings model to create objects
- Use plugins like Seed_Fu and object creations
- Use plugins like Seed_Fu and fixtures
- Use Bootstrapper plugin with Factory Girl

Since I'm already using Factory Girl heavily with shoulda and cucumber, it makes sense to use them for my database seeding.

Bootstrapper gives nice convenience methods like

rake db:bootstrap
rake db:bootstrap:reset

You can specify different environments (test, development, staging, production, etc.) and run seeding modularly within the db/bootstrap.rb file

Bootstrapper.for :development do |b|
b.truncate_tables :addresses :users

Factory(:us_address, :state => "ME")
Factory(:us_address, :state => "IL")
Factory(:us_address, :state => "CA")

Bootstrapper.for :production do |b|

Bootstrapper.for :test do |b|

Bootstrapper.for :staging do |b|

Bootstrapper.for :users do |b|
3.times{ Factory(:user) }
Factory(:user, :login => "admin",
:password => "sekret",
:password_confirmation => "sekret")

You can then specify the bootstrapping that you wanna make

rake db:bootstrap BOOTSTRAP=users
rake db:bootstrap RAILS_ENV=production
rake db:bootstrap BOOTSTRAP=users RAILS_ENV=production

Wednesday, July 29, 2009

Host your configuration files on github

My .vimrc and .bashrc is pretty highly customized. As such it doesn't make sense for me to rewrite them on my various machines (laptop + netbook + pc). Even copy and pasting them ain't no fun.

The solution is to host a master copy of these files online. Github is great for that. It offers a free repository that allows you to version your files using git. So I've created a github repository for my config files.

cd ~/apps
mkdir config-files
cd config-files
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin
git push origin master
cp ~/.bashrc .
cp ~/.vimrc .
git add .
git commit -m 'Added bash and vim config files'
git push origin master

So now that's left is to symlink the actual config files to my local repository.

cd ~
ln -s apps/config-files/.bashrc .bashrc
ln -s apps/config-files/.vimrc .vimrc

So I can do git clone and symlink these config files on my other machines. Best way to synchronize my files and versioning thrown in too!

chmod reference

4 = r--
5 = r-x
6 = rw-
7 = rwx


-rw-rw-r-- == chmod 664

Convert mp3 to aac using Soundconverter and gstreamer

I needed to convert my old Rooster - Joy Ride mp3 ringtone to aac format so I can use it on my iPhone.

After mucking around abit, found a neat solution.

First up install soundconverter which gives you a nice GUI to play with.

sudo apt-get install soundconverter

Then install gstreamer plugins with the appropriate codecs & encoders

# This package contains the aac encoder
sudo apt-get install gstreamer0.10-plugins-bad-multiverse

# This package contains the mp3 encoder
sudo apt-get install gstreamer0.10-plugins-ugly-multiverse

Go ahead and enjoy your conversions!

SCP files with spaces in filenames

I wanted to transfer a custom ringtone to my iPhone.

Joy Ride.mp3

Doing the following Secure FTP command from my iPhone will fail.

scp jasonong@\ Ride.mp3 .

Even this will fail.

scp jasonong@"Joy Ride.mp3" .


scp jasonong@"Joy\ Ride.mp3" .


Easy command line search with grep and find

I've been using grep and find for searches on command line.


# Search recursively for 'User' in file contents under current directory
grep -r 'User' .

# Search recursively for 'User' in file contents under current directory and display 2 lines before and after
grep -r -2 'User' .

# Search recursively for 'User' in file contents under current directory and display 2 lines before and after with color
grep -r -2 --color 'User' .

# Search for filenames that contains user.
find . -name 'user.*'

Combine them together:

grep -r -2 --color 'User' $(find . -name 'user.*')

How to change Tilda font size & open URL links

I used to love YaKuake, the "Quake" like drop-down console that you can activate at the touch of a customized button (it's F1 for me). But Yakuake relies on KDE for GUI which makes it look UGLY on Ubuntu's default Gnome desktop manager. Found an GTK alternative aptly named ~ Tilda (I remembered Counterstrike console was triggered by ~)

But Tilda's default font size's really too big. The GTK preference window didn't have any options to choose fonts & size. Mucking around .tilda folder in home directory shows 2 config files


Where there's a config option for font size

font="Monospace 13"

Ah that's why it's so huge! Thought changing it will be a simple act of replacing the "Monospace 13" to "Monospace 10". However after restart Tilda keeps getting to original font string. Doing tilda --help shows there's a -f option. So all I have to do to change the font is execute the following during Gnome startup

tilda -f "Monospace 10"


tilda config files should only be edited with tilda closed. Else it will save the settings that before tilda was edited!

To open URL links, use "CTRL + Left click"

Monday, July 27, 2009

Webrat's fill_in method on cucumber

Consider the following:

In feature:

And I fill in "Telephone (Home)" with "61234567"

In webrat steps:

When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
(field, :with => value)

The error encountered:

Could not find field: "Telephone (Home)" (Webrat::NotFoundError)

At first I thought the regex couldn't parse "(Home)" properly as "(" & ")" are control characters in regex. Turns out it's because the way webrat's fill_in method looks for the field. I had to use the following instead for the nested form field.

And I fill in "enrollment_addresses_attributes_0_telephone_home" with "61234567"

Super easy and fast cd ripping using abcde on the command line

Am ripping my new Pat Matheny cd to mp3 using abcde on the command line now. Just type in "abcde" and answer a few simple questions and hey presto!

~/projects/aims. Yes, my Master? abcde
Executing customizable pre-read function... done.
Getting CD track info... Querying the CD for audio tracks...
Grabbing entire CD - tracks: 01 02 03 04 05 06 07 08 09 10 11 12
Checking CDDB server status...
Querying the CDDB server...
Obtaining CDDB results...
Retrieving 1 CDDB match...done.
---- Pat Metheny Group / Letter From Home ----
1: Have You Heard
2: Every Summer Night
3: Better Days Ahead
4: Spring Ain't Here
5: 45/8
6: 5-5-7
7: Beat 70
8: Dream of the Return
9: Are We There Yet
10: Vidala
11: Slip Away
12: Letter From Home

Edit selected CDDB data? [y/n] (n):
Is the CD multi-artist? [y/n] (n):
Grabbing track 01: Have You Heard...
cdparanoia III release 10.2 (September 11, 2008)

Ripping from sector 0 (track 1 [0:00.00])
to sector 28939 (track 1 [6:25.64])

outputting to /home/jasonong/projects/aims/abcde.a20e7d0c/track01.wav

Wednesday, July 22, 2009

Search for codes in your files quickly with grep

Sometimes when I do unit tests or cucumber tests I'll encounter an error that I've roughly aware of the offending code but not sure which files it resides in. That's where grep comes in useful.

Search for occurrences of ".name" in view files recursively

grep '.name' -r app/views

Get occurrences with its surrounding 2 lines before and after

grep '.name' -r -2 app/views

Monday, July 20, 2009

Error using cucumber, selenium and firefox 3

Cucumber and Webrat works great for Behaviour Driven Development (BDD). In certain situations it's better to couple Cucumber with Selenium especially when we need to test javascript or have a look at the test visually.

When I first started Cucumber and Selenium on my Ubuntu. Selenium-server throws up saying that firefox 3 path isn't defined. Thankfully a blog article by Tze Yang presents a solution.However I still get the annoying error every now and then when firefox 3.0 was upgraded. This is because of the firefox executable is a symlink to a shell script.

ls -l /usr/bin/ | grep firefox

lrwxrwxrwx 1 root root 11 2009-06-13 13:32 abrowser -> firefox-3.0
lrwxrwxrwx 1 root root 11 2009-06-13 13:32 abrowser-3.0 -> firefox-3.0
lrwxrwxrwx 1 root root 33 2009-07-21 00:34 firefox -> firefox-3.0
lrwxrwxrwx 1 root root 32 2009-06-13 13:32 firefox-3.0 -> ../lib/firefox-3.0.11/

lrwxrwxrwx 1 root root 31 2009-07-01 00:42 firefox-3.5 -> /home/jasonong/.firefox/firefox

The trick then is to change the firefox symlink to the actual firefox 3 executable.

sudo rm /usr/bin/firefox
sudo ln -s /usr/lib/firefox-3.0.11/firefox-3.0 firefox

That should get you some nice cucumber + selenium action.

Blog Archive