bookmark_borderRuby on Rails Presentation

I will be making a Ruby on Rails presentation at the August php meetup.

This would be my second presentation on RoR, after the one at BarCampDelhi.

The image “” cannot be displayed, because it contains errors.

And this time there will be no ppts 😉

Thanks to ValueOne for hosting the meetup.

bookmark_borderrails captcha and testing using mock objects

To prevent bots from Signing up on our application, we added a captcha to our User model. We used the validates_captcha plugin available at

The plugin, selects text from a String array for the captcha challenges, so, Sur, my co-programmer, hacked the plugin a little to add random text and improved images.

After adding the plugin we realized that all our tests which validate the User model had started failing. Programmatcially a test case or a bot are not much different, they are both scripts.

Mocks to the rescue.

Here is what I wrote in a file named add_captcha_to_active_record.rb under test/mocks/test directory.

require File.expand_path(File.dirname(__FILE__) + 

module AngryMidgetPluginsInc #:nodoc:

	module Validations #:nodoc:

		# module Captcha
		module Captcha #:nodoc:

			# module InstanceMethods
			module InstanceMethods #:nodoc:


				def validate_captcha(options = {}) #:nodoc:


		end#module Captcha

	end#module Validations

end#module AngryMidgetPluginsInc

What I have done by adding this file is, I have asked the test scripts to use the validates_captcha method in /test/mocks/test/add_captcha_to_active_record.rb instead of the validates_captcha method in plugins/validate_captcha/lib/add_captcha_to_active_record.rb

Well actually Rails does all the magic, I just need to give the same file name as the one I want to mock and then define the method that I want to override.

The original validates_captcha method checks that the text entered on the signup screen is the same as that stored on the server. If these do not match it adds a validation error to the base class, the User class in our case.
In the mock validates_captcha method we just do nothing. So a validation error will never be added to the base class.

And now our tests are running and passing again.

I also noted the presence of a directory /test/mocks/development/ … well that means I can even mock classes in the development environment.
One of our application, connects to Paypal to make a payment and then returns back. During development we had commented out this code so that we are not sent to paypal everytime. Now I know how I can avoid commenting or changing the code by using mock objects.
I will be working on that soon and blog about my experiences here.

bookmark_borderkijiji: classified site with a clean interface

I am searching for a new office space…. and i landed up at kijiji.

Kijiji is a new venture within eBay, created by a small team ofentrepreneurial employees. We’re working hard to spread the word andrespond to your needs. We’d love to hear from you!

The site is clean, intutive and best of all offers a RSS feed for search. And has the option to skype the advertiser.

bookmark_borderWhat the Flock

I downloaded Flock today, just to test create blog posts directly from Flock into socialroots repository, as a part of the project.

But I ended up using it the whole day. Just didn’t feel like going back to FF. Both of them are built on top of the mozilla engine and have the same shortcuts, this really reduce the (psychological and habitual) barriers to the change.

I added a couple of feeds that I used to read via bloglines to Flock; and now i am writing this blog post also in Flock.

Have yet to try out flickr and delicious functions inbuilt in flock.

Even Flock UI seems better than FF ( or is it just that i am liking the change)

Give Flock a try if you still haven’t.

bookmark_borderemacs for windows: meadow

Meadow is a tool that I came across on my last visit to Japan.
It is a GNU Emacs imlpementation for Windows and is fairly popular in Japan.
Though i had tried the windows version of xemacs before, I like the meadow interface much more.

You can get it here.
There is an english version of the wiki here.

I sometime do a little bit of rails hacking on my windows pc too … so i installed the ruby mode.
If you would want to add ruby-mode, you can find ruby-mode.elc under your ruby installation.
Copy it to lisp/progmodes/ under meadows home.

To customize Meadow further… here is a good link ( tis in japanese, but you should understand the lisp)

bookmark_bordervalidating Rails association

Should ActiveRecord add validations based on associations defined in the models?

class User < ActiveRecord::Base 
  has_many :posts 

class Post < ActiveRecord::Base 
  belongs_to :user 

If i create an instance of a post with a user_id that does not exist in the Users table, I wish ActiveRecord threw up a validation error on saving this instance of post.
It does not do so by default, so here are the validations that I add to my models to implement this.

class User < ActiveRecord::Base 
  has_many :posts 

class Post < ActiveRecord::Base 
  belongs_to :user 
  validates_associated :user 
  validates_presence_of :user 

Now if I try and save a post without a user_id or with a user_id that is not found in the Users table, an exception is thrown.

Till now I always use to create foreign keys in my database, as a safety net. So that the db throws an error if my data is not consistent.
After adding these validations, I hope not to have foreign keys in the database anymore.