Michael MacDonald

Archive for July, 2009|Monthly archive page

Pseudo Natural Sort with Sphinx

In Rails on July 29, 2009 at 8:00 am

The Background:

  • items have a system code
  • all system codes begin with the same string of text eg S-
  • existing system codes have zeros to pad out the string so that all codes are of equal length eg S-001, S-011
  • users need to be able to search for system codes
  • using Sphinx and the awesome ThinkingSphinx gem

The Problem:

  • the business wanted to ditch the unnecessary zeros in the system codes to match the format of other codes in the system eg X-1, C-20, S-14
  • a search results table needed to be sortable by the system codes column eg S-1, S-2, S-10 instead of S-1, S-10, S-2 (the latter is what you get since it is an alpha sort on the whole string)
  • I flippantly proclaimed that we were smart enough to sort the system codes correctly and could easily ditch the redundant zeros

The Options:

  1. eat my words and keep the system codes as they are eg S-01, S-02, S-10
    • it would work but I don’t like letting the computer win
  2. as above but use a view helper to hide the zeros used for padding
    • not acceptable since it would display system codes as S-1 and users would need to search for S-01
  3. remove the prefix from the system codes, change them to integers and use a view helper to add back the prefix
    • not acceptable since the users need to search on the system codes eg S-1 instead of just 1
  4. or somehow get Sphinx and ThinkingSphinx to sort them properly
    • Sphinx can’t handle a natural sort but maybe there is a hack

The Hack:

has "CAST(MID(systems.code, 3, LENGTH(systems.code) - 2) AS UNSIGNED)", :type => :integer, :as => :system_code_sort

Yes, it is only a “pseudo” natural sort as it doesn’t sort the alpha characters but in my specific case it did the job and solved the specific need.

Advertisements

Four Ways to Rescue a Doomed Project

In Business on July 22, 2009 at 8:00 am

I’ve worked on or alongside many projects that were on the long, slow death march to failure. Unfortunately, when the shit began to hit the fan, inevitably the project leaders and managers would “demand” that everyone work harder in order to successfully deliver the project. It never worked. The projects failed, the project managers jumped ship, and staff moved onto other projects. It was always so demoralising.

So what’s the answer? How can you rescue a doomed project?

Read the rest of this entry »

Use response.capture instead

In Rails, Testing on July 15, 2009 at 8:00 am

Recently, I upgraded an app from Rails 2.1.0 to Rails 2.3.2. First, I upgraded rspec and rspec-rails to 1.2.7, raked my specs and they all passed. But after I upgraded to Rails 2.3.2, one particular spec failed with the following error message:

"undefined method downcase' for :extra_info:Symbol"

Read the rest of this entry »

Protecting paperclip files with nginx

In Rails on July 12, 2009 at 10:26 pm

In a Rails app I’m working on, there is a model that can have attachments and the paperclip gem is used to handle this. However, these files need to only be available to logged in users. They cannot be directly accessible to the public. For this, I used nginx’s X-Accel-Redirect response header. But, I couldn’t get it to work!

Read the rest of this entry »

Testing flash.now with RSpec

In Rails, Testing on July 12, 2009 at 9:56 pm

A recent Rails upgrade created some failing specs. One spec in particular caused me some frustration. It was a simple controller spec that was verifying the message of a flash.now warning. It worked find in Rails 2.1.0 but was failing in Rails 2.3.2. The solution was to simply inspect the response object!

Read the rest of this entry »

Hello again WordPress

In Uncategorized on July 12, 2009 at 9:46 pm
After nearly a decade of contemplating having a blog the last thing I needed was to embark on a year long project to create my own blog software, or to spend forever trying out loads of different blogging tools, or to try and make the perfect knockout blog design. I decided to just do it and resist the temptation to procrastinate.
My first attempt was to fire up iWeb and give that a shot. I chose a simple template, no fancy design, no twitter integration and no witty title. And so I now had my blank canvas on which I could share my intelligent and inspiring thoughts. Pity nothing came to mind. So I did what any good creative would do, I decided to redo my website.
I took a look around at the available blogging services and blogging software. I didn’t want to spend any money (not initially anyway) yet I wanted a service/tool that would provide me with heaps of features and flexibility so that I could create a great looking and functional blog quickly and easily so that I would have no more excuses and every reason to start blogging.
In the end, I decided to give WordPress.com another try. Yep, I’ve been here before many years ago. My account was still active but I never actually set up a blog. I was simply checking out the service. In the end I downloaded and installed the WordPress software and used it for keeping a private blog for a project I did. I think I added a dozen of entries before deciding that a wiki was a better idea. My opinion of Word Press (and WordPress.com) was that I didn’t like it.
So, this time around, I deliberately avoided WordPress. I wanted to see what other options there were (remember, I was trying to avoid doing the hard work of actually writing blog entries). But, in the end I succumbed and created this blog on WordPress.com. My initial impression is positive. It’s improved (as you would hope given the passage of time) in many ways and look at this: I’ve finally managed to start writing blog posts!

After nearly a decade of contemplating having a blog the last thing I needed was to embark on a year long project to create my own blog software, or to spend forever trying out loads of different blogging tools, or to try and make the perfect knockout blog design. I decided to just do it and resist the temptation to procrastinate.

Read the rest of this entry »