Archive for Uncategorized

Skip Validations

How many times have you felt like skipping validations on a particular model while writing rspec tests ? Last night, I was trying to write rspec tests for one of my models which tested transactions of money between two users. My user model has tens of validations and I totally wanted to skip them all. I just wanted to test the functionality that the money was transacted between the two users correctly. Thats when I used some of Ruby's metaprogramming capabilities to skip validations. Once I was done writing my tests (phew !!) , I decided to turn this into a Rails Plugin, enter SkipValidations ( http://github.com/balpreetspankaj/SkipValidations/tree/master).

This is a very simple plugin. Just install the plugin and then use the skip_validations on any ActiveRecord::Base class and give it the list of models fr which you want no validations. Simple ! for example,

User.skip_validations(:user, :person) {SellRequest.match_requests }

will skip validations for both User and Person model while running the SellRequest.match_requests function. This plugin made my testing a lot more smoother. Hopefully this is useful for you guys too.

Leave a Comment

Facebooker: Creating an Iframe Application (Incorrect Signature)

I started developing a Facebook app for one of my existing websites.  Since, I already had a working website and I prefer normal html and js over fbml and JS over FBML and FBJS, I decided to take the IFrame route. While working with my app, I ran into this bizarre problem where clicking on any link after loading the facebook app, gave me an invalid Signature exception. After digging my way through the Facebooker (Rails plugin for Facebook app dev) code, I find that the expected signature is nil inside the "verify_signature function". Turns out you need to append all your links in a Facebook iframe app with the fb_sig params. So, I created a function in Application.rb and set it as a before_filter

before_filter :set_facebook_params
ensure_application_is_installed_by_facebook_user
 
protected
def set_facebook_params
@fb_params = params.inject({}) do |collection, pair|
collection[pair.first] = pair.second if pair.first =~ /^fb_sig/
collection
end
end

The above code will extract out all the fb_sig params from the url and store it in @fb_params. Now add this hash to every url that you create inside you Iframe. This was the fix that I came in the middle of the might, and I am sure this may not be the best fix. But this works. Let me know if there is another workaround.

Leave a Comment