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
end

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 }
end

Thursday, July 30, 2009

Has many association in Factory Girl


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

Google styling scheme for tables

This is what I implemented after studying the CSS for tables that Google uses.

div.data-table table {
background: white none repeat scroll 0 0;
border-collapse: collapse;
cursor: default;
font-family: arial,helvetica;
font-size: 10pt;
margin: 0;
}

div.data-table 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;
}

div.data-table table td {
border: 1px solid #EEE;
padding: 2px 3px;
}

div.data-table table td.data-seq {
color: #666;
text-align: right;
}

div.data-table table tr.data-row-1 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
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