Archive for the ‘Uncategorized’ Category

Increment Ruby on Rails migration number

Thursday, 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

Friday, 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.

View YouTube videos in high def

Wednesday, 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

Wednesday, 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

Saturday, 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

Monday, 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

Sunday, 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

Sunday, 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

Thursday, 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

URL Anchors conflict with JavaScript focus routines

Wednesday, April 4th, 2007

I have been using some simple JavaScript to focus and select the contents of an form input box when a page first loads. But, was finding that it only works intermittently.

h3. Websphere Portal

To add some context, we’re using Websphere Portal – and it has some unique behavior. When you load a page that has a portlet embedded in it, it adds a hidden anchor near the top of the portlet. The portal-generated URL to the page includes an anchor to the portlet that generated the “render event”.

The standard behavior is that the browser jumps to the position of the referenced anchor when the page has finished loading.

But, if you have an inline JavaScript command to set focus to a field, it will only have focus for the small period of time. This is between the JavaScript commands running, and when the page finishes loading. Note: this is the behavior in Firefox.

h3. Solutions

There are two options for resolving this issue:

# Disable the use of anchors; or
# Connect your initialisation/focusing routines to the “onload” event