Thursday, June 08, 2006

faster tests

If you are using sqlite, this plugin is a nice way to speed up your tests

Monday, May 22, 2006

assertions when using rjs

rjs is a pretty neat way to avoid javascript code and implement the required functionality using ruby/rails itself.

When rails comes across a rjs template, it renders the html page and inserts the JavaScript into that page and returns it to the browser for execution.

Which is good, until you figure out that the functional testing assertion framework that ships with Rails suddenly does not hold good ! Basically the asserts work on the assumption that after an action is performed, the response object holds valid HTML code which it can hook into and perform the assertions, whereas when we use rjs the respose body actually contains Javascript code too.

Came across this nice assertions plugin(rjs_assertions) which helped us test the code which had the rjs bits. Good stuff.

Thursday, April 20, 2006

Creating a Synchronous call using Prototype

Tried to make a Synchronous Ajax call using the Script.aculo.us Library. Damn how do you do it ?
new Ajax.Request('/foo/bar', {asynchronous:true});
Looking at the code, setting the 'asynchronous' flag to 'false' should do the trick. Unfortunately, this does not seem to work.

new Ajax.Request('/foo/bar', {asynchronous:false});
Wierd

But got a chance to peek into prototype.js. Wow. Javascript can be beautiful.

Sunday, April 16, 2006

App Wide settings

We recently played a card which read something along these lines - 'As a user of this system I should be able to set some application settings in the database'

Till date we had kept all the 'settings' (for the lack of a better word) in a module and we were using it off the module.
module Prefs
BASE_DIR='blah'
REMOTE_SERVER='blah blah'
...
end
usage in the code was
  Prefs::BASE_DIR
Had not heard about this Settings plugin by Alex Wayne, but I think we came up with a simpler solution using the 'const_missing' on an object. The Settings plugin uses 'method_missing' to do pretty much the same, but it needed constants to be called like this - Prefs.constant(which I feel is confusing) rather than Prefs::CONSTANT.
module Prefs
def self.const_missing(constant)
const_set(constant, Preference.value(constant))
end
end
So basically whenever we access a constant on Prefs (like Prefs::BASE_DIR) the const_missing is triggered and we dip into the Preference table to fetch the constant and set it on Prefs.

So a bunch of hardcoded constants were easily turned into a few lines of code that fetched the information from the database. And the best part was that we did not have to go and change the way we access constants all over the code.

Friday, April 07, 2006

What ?

just came across this today !



Looks like .id is going to be deprecated soon and we're going to be heading the .object_id soon !

Thursday, March 30, 2006

create YAML Fixtures

Very cool.
This makes it very simple to convert your data into YAML fixtures to be used in tests.

Saturday, March 25, 2006

Test Databases - do a rake migrate if you change the structure

Some of the functional tests were failing because of a missing column in a table. Took us some time to figure out the real issue.

Added an extra column 'x' using rails migration. Ran the functional tests to make sure that they all pass.

They failed, complaining that there was no column 'x' present.

Decided to check the test_database to see if that new column was created in the table. And it was not.

The way rails handles testing, it creates a test_database using the structure of the tables in the development_database and populates the tables using the fixtures. Hence if the development_database is not current (w.r.t the migration scripts) then the test database too does not have the latest structure.

Ran the migrate task. Got the development_database synched up and ran the tests.
Voila. All passed :-)

Tuesday, March 21, 2006

Puzzling behavior

Was debugging some code today that was working on a windows box, but was not working in a Linux box.

class MapController
model :Roads

def draw_roads
render :text => "No Routes to show" if no_roads
end
end
On the linux box, draw_roads would always look for a draw_roads.rhtml template and go ahead with the execution. Whereas in a Windows box, the check would be performed and accordingly a render of "No Routes to Show" would happen or draw_roads.rhtml would have gotten rendered.

After some debugging and placing of puts statements in the code figured that the issue was with the

model :Roads

bit. It should have been

model :roads

But still not sure why this worked on a windows box and not a linux box !!

Javascript development

Stumbled upon an interesting Plugin for Eclipse today. JSEclipse. Very useful when you dabble a bit in Javascript at work and during play

Saturday, March 18, 2006

Ruby Hacking Guide

Awesome News.

Vincent has started translating the Ruby Hacking Guide into English !!
Currently only one chapter has been translated. And its awesome. The original book is in Japanese. Had tried converting it into English using Google Translate, but things did not make too much sense.

After reading the chapter on Objects in the translated version, lotsa small things started falling into place. Like for example I finally understood why we have symbols in Ruby.

Great going Vincent.