Wednesday, June 11, 2008

Javascript Closure

I think most people writing a lot of javascript eventually run into a misunderstanding about closures. Take a look at the code below. The code dynamically creates three buttons that show an alert box when clicked upon. What do you think will happen when you click each button?






The problem is that the animal variable used in the button's onclick event is changed in the loop and thus the event will use the value that was last assigned to the variable. In order to get the onclick event to use the value of the variable at the time the onclick is defined you will need to define an execution context which will capture this value. This can be done using a function. While this can be done with a regular function here is a nifty little example of doing this with an anonymous function:






Here is a slightly different (possibly simpler) way to create the function closure:




Friday, May 30, 2008

SQL Server information_schema.views Length Limitation and rails_sql_views

Recently I was running into a problem with a plugin called rails_sql_views (part of the Active Warehouse project). This is a great plugin created by some friends of mine that helps you manage views in a rails project. The plugin manages the proper creation of the view in the schema.rb file. For the SQL Server adapter the code to produce view comes from a query that looks like this:



The problem was that the view queries were being cut off. The cut off point seemed to be around 4000 characters. I mentioned this to my colleague Semergence and he recommended I look into the column definition for the view_definition column in the information_schema.views table. Sure enough, the column was defined as a nvarchar(4000). Who would be crazy enough to create a view that required more than 4000 characters right? Well, errrr, me I guess.

So armed with this 4000 number my google searches became very effective and turned up this from Joseph Scott. In the blog article Joseph reveals the sp_helptext stored procedure. Using this stored procedure you can easily get the full text of a views create statement, however, you do have to reconstruct it as it returns the result in separate rows under the key "Text". Here is the simple code:

Saturday, May 24, 2008

attachment_fu with Optional Attachment

Note: attachment_fu has changed a bit, see the update to this post here.

attachment_fu is the most popular plugin for providing file upload to a Rails application. One thing you need to be aware of with attachment_fu is that it is meant to be used by making a model just for the attachment that will be the child relationship in a has_many or has_one. An example would be a User model that has such a relationship with an attachment_fu Avatar model. If instead you put the attachment_fu model directly in the User class and then allow that attachment to be optional, you will run into two problems:

  1. on update without an image fall down go boom
  2. on destroy without an image fall down go boom


Recently we had an application where this mistake was made and the problem wasn't noticed until the application was deployed and people started creating models without an attachment - fall down go boom.

It is, however, possible to patch attachment_fu so that it will work as an optional attachment. Information about such a patch can be found here. The provided patch only takes care of the problem of updating a record that doesn't have an attachment but the comments suggest a solution for the problem of destroying such a record as well.

With that said, Paperclip looks like the new kid on the Rails file upload block designed to avoid such problems. I think for my next file upload requirement I'll definitely take a look at Paperclip.

Saturday, May 10, 2008

Ruby Singleton Methods Handy for Quick and Dirty Debugging

In a previous post I talked about needing to dump ActiveRecord models in favor of record sets when producing large reports from a Rails application. While this works well, the record set returned when doing a select_all is an array of hashes. And, being a hash, if you misspell the column name and type, for example:



nil will be returned and you will be none the wiser. Rubies singleton methods can quickly be used to detect such mistakes. Merely override the [] method with a method that will raise an error if the key can't be found:



By appending the singleton method override for [] you can quickly find out if you have made a typo somewhere in your view when specifying the column names of the record set.

Tunnel Behind a Firewall to Connect with rdesktop (or mstsc)

Ok, ok, so we really have a couple of Windows computer at work that are being used to serve up Rails applications. They are firewalled so they don't accept rdesktop connections over their public IP. We do, however, have a trusty Linux box running an ssh server that has access to these Windows boxes via our internal network. This makes it possible to work from home, restarting mongrels and such, by tunneling through the Linux server. First you need to set up the tunnel. This is done as follows:



That is, create a tunnel through linux.box.name.com that connects to the internal network computer 10.0.6.13 on port 3389. Here, I have also set up the local port to be 3389 because it is the default rdesktop port and thus I have one less thing to specify to the rdesktop command. After this tunnel is set up you can connect simply:



To do this from a Windows machine you will need to install cygwin or putty to set up the tunnel and of course the command to connect is then mstsc.

Wednesday, May 07, 2008

will_paginate with acts_as_taggable_on_steroids

How to paginate when searching tags with the combination of will_paginate and acts_as_taggable_on_steroids. Here is the paginate call:



Here, conditions_for_doohickies represents a function that produces the proper conditions from the supplied params. This can, of course, be done as an AND or an OR. If you want to return doohickies that have any of the tags (OR) you would do it like this:



And, if you want to return doohickies that have all of the tags (AND) you would do it like this:

Saturday, April 19, 2008

ImageMagick Bulk Image convert Fun

ImageMagick can do an amazing number of command line manipulation functions. ImageMagick's convert command, for example, can convert an image from one format to another and do a bunch of transformations along the way. Recently I needed to convert a whole bunch of SVG files deeply nested in a directory structure into transparent PNGs. convert, combined with a little bash magick, makes this a quick and painless task:



Let's break that down.


  1. Recursively find every file ending in .svg and pass each one separately to exec

  2. For each exec invoke a shell that executes a command from the given string with the filename getting passed as ${1}

  3. In the command call convert, changing white in the SVG to transparent in the PNG where the first argument is the SVG to convert, and the second argument is the destination PNG filename



Job complete.