Its all about Ruby On Rails
Archive for December, 2008
Testing ActiveScaffold based Controllers
Dec 17th
If we are using ActiveScaffold in a controller, we might want to write test for that controller too. So we can write functional tests for all action that ActiveScaffold added to our controller like index, create, edit, update etc…
Since all these action are added by ActiveScaffold then these tests must go with ActiveScaffold plugin tests itself and I think they are at their place.
So we just need to test that ActiveScaffold is configured for that particular controller and uses the right model by:
assert_not_nil CampusController.active_scaffold_config assert CampusController.active_scaffold_config.model == Campus
Ofcourse, we also need to write tests for custom actions added by us.
Generating association while generating model
Dec 14th
May be you guys are already aware of, but in Rails 2.2 we can specify belongs_to association while generating model. So if we issue:
script/generate model Post title:string author:belongs_to
We will get a Post model like:
class Post < ActiveRecord::Base belongs_to :author end
Also it will automatically add the foreign key column in the migration.
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.string :title
t.belongs_to :author
t.timestamps
end
end
def self.down
drop_table :posts
end
end
Nothing much but saves some keystrokes.
Using Amazon SimpleDB with Rails
Dec 13th
Last week I tried to use simpleDB with rails, here are some slides I prepared:
Ruby script to install bulk gems
Dec 8th
Recently I had to setup many rails servers and for that I had to install some gems again and again by issuing traditional “gem install xyz” command several times. This was actually very annoying.
To avoid issuing “gem install xyz” command again and again, while setting up new machine, I wrote a utility script in ruby. This script read a list of gems and install them by avoiding ri and rdoc installation for gem.
This script saved lots of my time. Here is the Ruby script to install bulk gems, if you would like to give it a try.
Download this script and save as .rb. Run this script by “ruby install_gems.rb” and follow the instructions on screen.
Skiping installation of ri and RDoc documentation while installing gems
Dec 3rd
Probably most of the time you would like to skip ri and RDoc installation while installing some new gems, specially on production server.
I do like to skip ri and RDoc documentation while installing gems on my development machine, because it takes more time to generate ri and RDoc then actual installation of gem.
We can skip rdoc and ri documentation while installing a gem by:
gem install rails --no-ri --no-rdoc
I am sure that you would not like to give –no-ri and –no-rdoc every time you install a gem. To avoid this situation, you can create/update $HOME/.gemrc file with following option:
gem: --no-ri --no-rdoc
Now every time you install a gem, it will skip installation of ri and RDoc for the gem.
My .gemrc file looks like:
--- :update_sources: true :sources: - http://gems.rubyforge.org/ - http://gems.github.com :benchmark: false :bulk_threshold: 1000 :backtrace: false :verbose: true gem: --no-ri --no-rdoc