Thursday, July 30, 2009

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
b.run :users

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

Bootstrapper.for :production do |b|
end

Bootstrapper.for :test do |b|
end

Bootstrapper.for :staging do |b|
end

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

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@github.com:jasonong/config-files.git
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

Thus,

-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@192.168.1.2:working/Joy\ Ride.mp3 .

Even this will fail.

scp jasonong@192.168.1.2:working/"Joy Ride.mp3" .

Finally,

scp jasonong@192.168.1.2:working/"Joy\ Ride.mp3" .

Transfered!

Easy command line search with grep and find

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

Examples:

# 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


.tilda/config_0
.tilda/config_1


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"

Update

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"