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.

Bookmark and Share

Leave a Comment

Facebook Iframe Application: Resize Iframe

If you are using Rails and Facebooker, and you have a resizable Iframe application and you are having problems with resizing, do the following:

a) Make sure that the Iframe is set to Resizable in the Facebook appplication settings.

b) Instead of calling just <%= init_fb_connect "XFBML" %> (as mentioned in all the tutorials), use

<% init_fb_connect "XFBML" do %>
FB.CanvasClient.startTimerToSizeToContent();
<% end %>

This should automatically resize your Iframe in the facebook canvas

Bookmark and Share

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.

Bookmark and Share

Leave a Comment

SMERF and issues with MySQL

SMERF is the meta form generator plugin for Rails. The plugin I would say is very useful. It takes in a form definition file, defined in YAML and creates a form on the fly. These forms are associated to users and the responses to the forms are also persisted in the db. Ideal for making a quick survey for the users on your websites. I was using this plugin to create a survey for the upcoming fashion website chictini.com, and ran into a weird issue.

SMERF plugin, actually parses a form definition YAML file, and creates a SmerfForm object out of it. This SmerfForm object is actually persisted in your db, for fast future accesses. Now this SmerfForm object is serialized into a text field in a MySQL table. Everything works fine when you have small forms. But as soon as your forms get bigger, you hit a MySQL issue. The max size of a text field in MySQL is 64 KB. So if the serialized data is more than 64 KB, you start getting weird errors. The serialization does nnot throw an error, neither does saving the object in DB. But when you try to access the serialized data, you basically get incomplete serialized data, which is nothing but garbage for your purposes.

Solution:  Change the column which stores the serialized data to longtext for MySQL. You can create a migration for this which would look like this:

 
class UpdateColumnInSmerf > ActiveRecord::Migration
def self.up
execute "ALTER TABLE smerf_forms MODIFY COLUMN cache LONGTEXT"
end
 
def self.down
execute "ALTER TABLE smerf_forms MODIFY COLUMN cache TEXT"
end
end
 

This was was one of the annoying issues which kept me awake till 3 am.

Bookmark and Share

Comments (1)

Div inside a p : Invalid HTML

I was trying to center an element using the p tag. Like :
<p style="margin-left:auto; magin-right:auto; width: 50 em;">
<%= show_simple_captcha %>
</p>

show_simple_captcha is a helper method from the simple captcha rails plugin. This view helper generates a div which helps in captcha validation. Now, this created a problem. This piece of code started to behave randomly in IE. Even the source generated in Firefox is invalid HTML. After much scratching arnd, I found that <div> tags are not allowed inside <p> tags. Strange !! I dont have a reason of the top of my head as to why this is disallowed, but this is invalid W3C HTML.

Bookmark and Share

Leave a Comment

action_mailer_tls and Ruby 1.8.7

If you are using action_mailer_tls rails pluin and you shifted to Ruby 1.8.7, your app might break. I recently ran into this problem, coz my host providers shifted to Ruby 1.8.7 and I started getting errors when I was trying to send emails. The problem is because the check_auth_args function has changed in Ruby 1.8.7. Now it requires only 2 args as opposed to 3. The action_mailer_tls plugin uses this function but uses three arguments. So, to get back running again, make the following changes:

1).  Go to your action_mailer_tls plugin installation
2). Inside the do_start function, replace the call to "check_auth_args" with the following snippet:

 
no_args = method(:check_auth_args).arity
if no_args == 2
  check_auth_args user, secret if user or secret
else
  check_auth_args user, secret, authtype if user or secret
end
 
Bookmark and Share

Comments (3)

Ruby 1.8.7 String#chars compatibility problem with rails

Recently, I started getting weird "LoadError" with my Rails app on Dreamhost. I had an "Article" model which contained many "Photo" models. When the "Article" controller tried to refer to the photos in its object, I got the following error:
LoadError: Expected {full server path}/photo.rb to define Photo.

I had a photo.rb file which had class Photo defined. I was using attachment_fu plugin to implement photos. After some head scratching, I found that the error was in photo.rb file. Hence photo.rb was not loading correctly, and therefore a load error.

The actual error happened coz the Dreamhost guys had updated Ruby to 1.8.7 which has some incompatibility with Rails 2.0.2 that I was using. When I tried to create a Photo object on the command line, I got this error:
NoMethodError: undefined method '[]' for #<Enumerable:enumerator></code>

Ruby 1.8.7 has String#chars. This returns an Enumerator object but Rails 2.0.2 expects an ActiveSupport::MultiBye::Chars object.

We can avoid this conflict, by putting the following code in the config/initializers/ directory.

  1.  
  2. unless '1.9'.respond_to?(:force_encoding)
  3. String.class_eval do
  4. begin
  5. remove_method :chars
  6. rescue NameError
  7. # OK
  8. end
  9. end
  10. end
  11.  
Bookmark and Share

Comments (3)

Insert comments in an erb file

How does one insert comments in an erb file. The normal http tag to insert comments do not work well when there are embedded ruby statements in the html.

<!-- <b>Hello</b><%= @article.author %> -->

The above statement will not stop the evaluation of article.author. Instead, @article.author will still be evaluated and placed as a comment in the generated html file. Say the author was Balpreet, then we have a html comment:

<!-- <b>Hello</b>Balpreet -->

So, what if we want to just comment stuff out, so that the embedded ruby statements are not evaluated at all. There are basically two ways that I think we can achieve this:

1. Insert block level comments using embedded ruby.
<%
=begin %>
<b>Hello</b><%= @article.author %><%
=end %>

2. Insert conditional
<% if false %>
<b>Hello</b><%= @article.author %>
<% end %>

Bookmark and Share

Leave a Comment

Rfeedparser and Hpricot

RFeedParser is one of the ruby gems to parse rss feeds. Its a translation of Mark Pilgrims's Universal Feed Parser from Python into Ruby. It has a few dependencies and requires you to install a few other gems. The installation instructions are simple and the usage is simple.

One of the dependencies for this was hpricot. Everything was working fine till one day, I updated my hpricot gem and did a cleanup of my rubygems. I started getting an error:

balpreet-pankajs-macbook:digg balpreetpankaj$ ruby bloo.rb
/Library/Ruby/Site/1.8/rubygems.rb:142:in `activate': can't activate hpricot (= 0.6, runtime), already activated hpricot-0.6.164 (Gem::Exception)
from /Library/Ruby/Site/1.8/rubygems.rb:158:in `activate'
from /Library/Ruby/Site/1.8/rubygems.rb:157:in `each'
from /Library/Ruby/Site/1.8/rubygems.rb:157:in `activate'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.2/lib/active_support/dependencies.rb:510:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.2/lib/active_support/dependencies.rb:355:in `new_constants_in'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.2/lib/active_support/dependencies.rb:510:in `require'

After a little digging arnd with rubygems, I figured out that the rfeedparser has a requirement of running with a hpricot gem with version = 0.6. I changed that to >= 0.6 and things started working fine. I was using version 0.9.951 of rfeedparser. If you use rfeedparser and run into a similar problem, this is how you fix it:

1) Update the gemspec for rfeedparser. Go to your gem installation dir. You can find this by doing:
gem env gempath
Then edit the $gemPATH/specifications/rfeedparser0.9.951.gemspec and update all references of hpricot from "=0.6" to ">=0.6".
2). Go to $GEMPATH/gems/rfeedparser-0.9.951/lib/rfeedparser.rb and again update the reference of hpricot.

This should get you rolling with all new versions of hpricot.

Bookmark and Share

Comments (1)

Creating a headline rotator

I was recently reading the book Learning Jquery by Jonathan Chaffer and Karl Swedberg . After reading the chapter on Rotators and Shufflers, which shows how you can create a headline rotator, I decided to implement one for my own website, adevelopedworld.com. adevelopedworld.com is a website which features social entrepreneurs from all around the world. We constantly get updates from the social entrepreneurs that we have featured, and we had to manually update the homepage to show these updates. After implementing the headline rotator, we just keep adding the updates to a text file, and the homepage gets automatically updated. And to top it, the homepage looks all the more dynamic.

The tutorial on how to create the rotator in the book is very complete. I just had to make one change. The tutorial is based on an old version of jQuery which supported XPath expressions. XPath expressions are no longer a part of JQuery. But there are two solutions :
a). Either add the XPath plugin for jQuery, OR
b). Change the XPath expression to a normal jQuery selector expression.

I used the latter.

A live demo of the headline rotator can be found at adevelopedworld.com.

Bookmark and Share

Comments (2)