Increment Ruby on Rails migration number

May 1st, 2008

We recently needed to create a new database migration, and needed to insert it into at certain position. Normally what I have done in the past is to alter the migration index of an existing file or two.

But this time, it needed to go in at 003. I was going to have to manually increment about 30 or 40 migration scripts. So, it was time to apply my Ruby skills to do it automatically.

Here is the result:

So when you run it, you will get output such as:

Instructions

  1. Cut and paste the file, or download it from increment_migrations.rb.
  2. Save it somewhere
  3. Make it executable chmod 755 /path/to/increment_migrations.rb
  4. The change directory to ./db/migrations
  5. then run /path/to/increment_migrations.rb 3
  6. and if the output looks as expected
  7. finally run /path/to/increment_migrations.rb 3 RUN

Finally, thanks to the client for permission to publish. Thanks for giving back to the community!

Work-around for running Rails/Apache in a subdirectory

April 11th, 2008

I’ve just configured Rails to run in a subfolder on a client’s website. Ran into a few issues, that are worth documenting:

In this configuration, you can visit the Rails site by visiting something like http://www.hostname.com/coolsite/

1. There is a Rails bug (ticket 10913) that effects links to assets. I have a link to our combined stylesheets defined as:

The bug is that Rails looks for all of your stylesheets in /public/coolsite/stylesheets/*.css, where ‘coolsite’ is the name of your subdirectory. Obviously, it should not be including the ‘coolsite’ folder in the path.

The workaround I use, is to create a soft link after capistrano deploys the code to our production server. I added this to my /config/deploy.rb file.

Now, whenever you do a cap:deploy, a soft link will be created in your public software, and this will allow Rails to resolve the stylesheet (and other asses files) in the faulty path. When the ticket is fixed, just remove the capistrano task.

Thoughts on Java and Ruby/Rails

April 4th, 2008

Over the last four months, I’ve been doing both Java and Ruby work.

Inland Revenue are building an external authentication system in Java Portlets on Websphere Portal. And, Sharesight are building an online share portfolio manager in Ruby on Rails.

I’ve noticed how different the two environments are when using 3rd party libraries. Take these two examples:

Using Display-tag for rendering multi-page tables

Display-tag is a Java library that has been around for a long time. I remember using it back at Westpac in 2005. It provides a JSP tag that you can use to render large collections – either on a single page, or over multiple pages. And provides the ability to switch between each of those pages with the expected next/previous and page 1/2/3/etc links. It also can sort the collection by clicking on the column headings.

Anyway, we ran into a problem. The functional specification document wanted the paging link positioned in a specific manner. This differed from what display-tag generated by default. The developer writing the tag spent a lot of time poring over the (good) documentation trying to find out how to make the links appear in the desired position.

Eventually, he came to the conclusion that the Display-tag library wouldn’t do it. The only way the achieve the desired outcome would be to use some CSS magic.

Using Mail queue for asynchronous email

Mail Queue is a Ruby on Rails plugin that converts the normally synchronous mail sending capability of Rails to an asynchronous queue. The emails get stored in a database table, and you use a background process such as cron or Backgroundrb to send the stored email.

It was all going very well, but we wanted to enhance the plugin to add the ability to archive the sent emails, rather than delete them. This would allow us to diagnose problems a lot easier.

In a Rails plugin, you have direct access to the source code. The plugin is stored in the vendor/plugins/mail_queue directory of your Rails project. We opened the mail_queue.rb file, found the code we needed to change, made the enhancement, and saved the file.

The differences

With Java 3rd party libraries, they are packaged into a standard JAR file. Inside the JAR file are all the pre-compiled class files. It’s difficult to see how the library is coded. If I wanted to change how a Java library worked, I’d have to:

  1. Download the “source” distrubution
  2. Unzip the distribution to a temp directory
  3. Find the code, and make the change
  4. Search for the build instructions
  5. Download Ant, or Maven, or similar
  6. Do I have the correct JDK version
  7. Hopefully the build works
  8. Replace the old JAR with the new build

Contrast that with the Rails approach:

  1. Find the plugin code and make the change
  2. Restart your server

It’s the dynamic nature of the Ruby language that contributes to the ease-of-modification. Being able to replace the change/build/package steps with just a change step makes a huge difference to productivity.

Conclusions

  1. Ease of modification means greater use of plugins
  2. Dynamic languages are the future

View YouTube videos in high def

March 26th, 2008

YouTube offer a higher definition version of their videos. It doesn’t seem to be available by default. You need to append this string to the end of your URL, and reload the page:

If you have Firefox, you can add this as a Bookmarklet. Then when you are viewing a video on the YouTuby website, click this bookmarklet, and the page will reload in HD.

Here is the bookmarklet

Right-click, and select “Bookmark this link”, or just drag it into your Bookmarks Toolbar.

View in HD

More details are available on the YouTube blog

spec’ing with BigDecimal

March 26th, 2008

If you are creating specs comparing BigDecimals to a hard coded float value, you may find that some comparisons just dont work even when the numbers are supposedly the same (see Floating point accuracy problems)

What we are testing is the value and the type, the correct way of achieving this may be…

If you want to round the big decimal to a float with 2 decimal places you could also use

Installing Git on Mac OS X 10.5 Leopard

March 15th, 2008

I’m a recent convert to Git, and have had trouble finding decent installation instructions for Leopard. Here are the steps I followed to successfully install Git on Mac OS X 10.5.2 Leopard.

Prerequisites

  1. The OS X Developer tools installed from the Leopard DVD
  2. Download the latest Git source package from git.or.cz
  3. Download the latest version of Gettext

Step 1: build and install Gettext

Gettext is required for building Git. If you don’t have it available, you’ll get an error such as:

First extract the archive:

Next, read any updated instructions and build it:

Finally, install it

Step 2: build and install Git

These instructions are fairly similar, except for a small configuration setting. Again, extract the archive:

Next, read any updated instructions and build it. If you omit the --prefix setting, git will be installed into your ~/bin directory.

Finally, install it

Once this is complete, you should be able to run it. Try this:

That should do it!

mail_queue

March 10th, 2008

We recently switched www.sharesight.co.nz to use mail_queue, a ruby on rails plug-in. This helped decouple our application from the mail server – improving the performance of page request/response times and isolating any mail server problems away from our application.

We have ended up making a few tweaks to the plug-in, detailed below:

The tweaks

  1. Added a sent flag onto the queued_mails, so we keep all mail sent out from our server. This proves may not be required but gives us a little confidence in seeing the sent mail, when it was queued and when it was sent, and provides an added bonus – if needed (in times of trouble) to reset the flag and have the mail sent out again.
  2. Added a failure count on the queued_mails, if a mail message is failing, we increase the count and the process will retry again during the next sweep – giving up on the message after several retries.
  3. Increase the size of the object to store the mail

Some problems we experienced

With large emails the ‘text’ field in mysql was not large enough to store the entire email message . The mail object then failed to be created and with no exception handling in place the email queue was blocked by one overlarge corrupt email – hence the failure count and the increase from text to mediumtext for the mail object.

With a large number of queued emails, the mail server started rejecting emails (due to our limited email account plan we are currently on) – and responding very slowly – this caused the MailQueue.process job to run before the previous job had completed – causing some emails to be sent twice – arghhh – hence see additional enhancements below.

Additional enhancements (TODO)

  1. Ensure that the process can not run – by either synchronising with a database flag (in_process) or exiting the process before the next one starts by keeping a tab on the time taken to run.
  2. Use backgroundrb to start the processing so the whole ruby environment does not have to be loaded every minute to check for mail.

Abletech develops Sharesight

December 16th, 2007

Able Technology has designed and developed Sharesight – an online share portfolio management system. The system has been developed using Ruby on Rails and MySQL with interfaces to ASX, NZX and Strikeiron. Registered users have access to alerts, automated dividends and reconstructions, annualised performance reports and tax reports.

Abletech invites anyone wishing to try out the system to use the ‘secret’ campaign code: abletech

“www.sharesight.co.nz”:http://app.sharesight.co.nz/signup?campaign_code=abletech

Updated: My Auctions Gadget for TradeMe

May 20th, 2007

There is a new release of the My Auctions Gadget for TradeMe. This new release adds a number of new features. Changes include:

* a new website – “www.trademe gadgets.com”:http://www.trademegadgets.com/my_auctions/
* time till auction ends
* visual refresh – cleaner layout

More information can be found in “my posting to the TradeMe Gadgets Google Group”:http://groups.google.com/group/trademe-gadgets/browse_thread/thread/c15dd6f98c2c123.

TradeMe Gadget for iGoogle

May 10th, 2007

I’ve been trialling the new TradeMe API that Rowan Simpson announced yesterday by building an iGoogle gadget called the “TradeMe Member Details Gadget”:http://www.trademegadgets.com/my_auctions/. It takes your TradeMe ‘username’ and displays a list of your current auctions and their prices.

Screenshots and further details are available on “Nigel’s Blog”:http://blog.wellies.org/post/1608836