James Wilding's Weblog

Month: January, 2010

Using @query with Sunspot::Rails

Using sunspot-rails for search in your Rails app? If like me you’re using @query to represent a search term within your Rails controllers, you might find that your searches turn up empty when they shouldn’t.

There’s a simple reason for this: used inside an Object.search block, @query evaluates to an internal Sunspot::Search object — not to your search term. Don’t worry: it’s easy to get round this issue; either rename your instance variable, or use the search block with an argument:

This ensures that @query is evaluated in the correct context.

Deadlines are Dangerous

If you have a deadline, you’re in one of two situations. Either you’ve decided for yourself when a piece of work will be finished, and formalised that prediction, or you’ve been set a finish date by someone else. Either the deadline is your own, or it isn’t.

Deadlines you set for yourself aren’t deadlines: you can change them any time you want. An extra day, and extra week — who cares, aside from you?

External deadlines are the real thing though, and they’re bad news. As soon as you’re in a situation where you need a deadline, you’re loosing touch with the reality of your work: things have ceased to be about quality, and suddenly it’s all about time.

This is bad because under deadlines, quality of work usually suffers. Deadlines are usually unrealistic in relation to what needs to be done; they focus instead on when things need to be done by. Of course, when you’re concentrating on how long something takes, instead of how well it’s done, mistakes are more likely to happen. (No, you can’t focus on both at once.)

Deadlines says more about how much of a rush you’re in than they do about how well you can work. If you want to pick an end date for a project, let that date evolve naturally from the way you work: pick a date that realistically represents how long it will take you to do a good job, not a deadline that’s imposed because of some arbitrary “must be done by” requirement.

IP Lookup Services: A Short Critique

There are loads of “what’s my IP” websites out there, but I’ve never seen one that actually does what I want it to. In my mind, a good IP lookup site should do just two things:

  1. Tell me my IP address
  2. Let me copy my IP address with minimum fuss.

That’ll be all, thank you: I don’t need to know where in the UK I’m based (I, uh, know that already…thanks — and normally the information is wrong, anyway). I don’t need a ‘wealth’ of useful information, either: most of it is superfluous and just gets in the way.

How can websites that only have to accomplish one task get it so wrong? From a usability point of view, most of these websites are awful. More features are not better — simplicity is key.

Why I never use PHP

I haven’t used PHP for any kind of web development for three years: here’s why.

PHP is ugly

Open brackets, close brackets, semi-colons, parentheses: PHP has too much language “noise” for my taste. Languages like Python have shown that it’s perfectly possible to treat whitespace as a formal part of your syntax, and even if you’re not a fan of indenting, there are other choices out there which don’t force you to use lots of special characters.

PHP is difficult to read

Following on from the language noise point, I find PHP code more difficult to read than, say Ruby — mainly because of the extra characters. This is a really important point for me: I want to be able to read code naturally, in a similar way to English, because, well, I’m lazy :)

Joking aside, I think the barriers to learning a new language are lowered when reading the language is as close to reading English as possible (for English speakers).

PHP is old

PHP feels like an old language. Maybe in five years I’ll be saying the same thing about Ruby! I’m not saying the PHP is out of date, just that it feels weighed down by a lot of the programming languages defaults from five or ten years ago.

PHP is not dynamic enough

I know PHP has object-oriented programming, but I’ve been spoilt by Ruby’s meta programming abilities :) It’s nice to be able to define methods on the fly, while some code is running. There’s a level of introspection with Ruby that I never got with PHP.

Conclusions

I freely admit that this is personal opinion: PHP is a well respected language, plenty of people use it, and I think it’s great for hacking together apps that will work on almost any server without much fuss (Shaun Inman’s Mint, which I’ve recently installed, is a good example — it works on almost any server that has PHP and MySQL. I can’t imagine him having written it using Rails!).

But personal tastes are all that matter when it comes to your own work, and personally, I’m happy that I don’t have to see all those brackets every day :)

#Fail is Lazy

Yes, fail fails. Mainly because most people use it to describe some vague sense of disappointment that [insert software/corporation/website/person here] isn’t working, without any suggestions about how to make the problem better.

Now of course, I’m not suggesting that you need to provide constructive feedback every time you complain. Everyone’s entitled to a moan. But “fail” sometimes smacks of arrogance: “I’m too important to even begin to explain why I’m disappointed here. The simple fact of my discontentment is enough.”

As a Buddhist, I think it’s necessary to avoid taking our mind at face value: a knee-jerk reaction to a problem, whatever word we use to describe it, doesn’t help us at all. All we’re doing is training our mind to react in an even more knee-jerk fashion next time, until we get pissed off at everything that goes wrong, and can’t do anything constructive to make things better.

A better reaction: “why am I disappointed — do I need to be?”. Don’t take your emotional reactions at face value: learn to be objective.

Long story short: “fail” is the 2010 equivalent of “am I bothered?“. Use it with humour, sure, but just don’t take it too seriously – it could ruin your life ;)

Microsoft’s Tablet PC is Shit

So, Steve Balmer from Microsoft gave a talk recently about a new “slate PC” from Microsoft. Can you sense my joy?

The best parts of this talk are Balmer’s pauses as he struggles to find the right word to describe the tablet (“slate PC”, “slate TV”, “product”, “PC”). Here’s a tip: if you — the head of a multi-national software and hardware company — don’t know what to call your product, then something is very wrong.

Here’s a transcript of the BBC’s video of Balmer’s talk. Forgive my cynicism: I have no love for Microsoft.

The new form factors that are coming this year are in what we would call “slate PCs”

No, Steve, they’re in what you call “slate PCs” and everyone else would call “tablet PCs”.

Here are three different slate TV form factors

Wait, they’re TVs now? Have you taken your pills…?

We’re talking about something that’s almost as portable as a phone, and as powerful as a PC running Windows 7.

So that’s three times the size of a phone, and not very powerful.

Maybe I’ll just drill down a little bit on this example from Hewlett Packard. This is a sort of a prototype of a coming HP slate PC that’ll be here later this year…

Oh, yes, please: drill down. Sigh. A “sort of a prototype” of an as-yet unreleased upcoming product that will be here sometime “later” in the next twelve months. Wow, that’s exciting.

It’s a beautiful little…product

The pause says it all.

This great little…PC, which will be available later this year, I think many customers are going to be very excited about.

I’d like to meet these “many” customers. Have they taken their pills?

I’m running the Kindle software for PC – as you can see, I can flip through the book using the touch experience, I can experience the book in full colour…

The touch experience. Because that’s the best phrase to describe touch screen technology. When I use my iPhone, I’m always saying to friends, “you know what I really love about this phone? The touch experience.” (The book, by the way, is in black and white.)

…and I can of course go and buy and download more content from Amazon right from within this application.

Woohoo! Content!!! We all love content. Except we call it “books” or “music”.

Great speech Steve. I know the other Steve is going to be really worried ;)

How to get Monit alerts via Twitter on Brightbox

By default Monit’s alerts arrive via email, but it’s quite easy to configure Monit to send alerts via Twitter instead.

These instructions are based on a Brightbox server, because that’s what I use, but they should work on any *nix server.

Step 1: Dedicated Twitter account

You’ll need a Twitter account dedicated to your Monit alerts (I’ll call this the Monit account): sign up at Twitter.com and skip all the ‘add people’ stuff — this account won’t be very sociable. You also want to make sure that your alerts aren’t visible to the general public, so go to the ‘Settings’ page and check ‘Protect my Tweets’ to make your account private.

Now when someone — a colleague, a fellow developer — follows your Monit account, you’ll be prompted to approve them before then can see your tweets.

Step 2: Follow yourself

Follow your new Monit account from your real Twitter account — you’ll have to approve the follow request, so there’ll probably be a bit of switching between accounts, but you only have to do it once. When you’re done, switch back to your real Twitter account, and you’ll be ready to see Monit alerts in your Twitter feed.

Step 3: Email to Twitter

Monit doesn’t provide a way to send alerts directly to Twitter, but luckily there’s a great service called Twittermail which can convert Monit’s email alerts into tweets.

Sign up for a Twittermail acount using your Monit Twitter account and make sure to opt out of all the extra ‘features’ like auto-follow: you just want a Twittermail email address. Grab the random address from the settings page, under ‘Twittermail’, and save it on your clipboard for later.

Step 4: Configure Monit

SSH into your Brightbox and edit your Monit email alert settings (if you’re using a different host, your config file may be in a different place):

The file should already have a line containing your email address: change this to use your new Twittermail address:

Run sudo monit reload, and you should see a new tweet from Monit — via your Monit Twitter account — in your Twitter feed.

Step 5 (Optional): Configure Monit’s alert messages

I recommend creating a custom short format for Monit’s alert messages — edit the line in /etc/monit/conf.d/email-alerts.monitrc:

Check the Monit docs for message formatting options.

How to be Productive, Part Zero

The thing about productivity is, knowing the word and saying you want it isn’t enough. Even feeling productive isn’t always enough, unless you have some control over what it is that you’re producing. Cows are productive — plenty of milk — but you don’t see them winning business awards.

The key to being productive is knowing what you want to do. That simple. Once you have that knowledge, you’ll naturally find ways to reach your goal: if you decide to learn to drive, next thing you know you’ll be booking some driving lessons. You just need to make a clear decision!

This is the nature of the human mind, and you don’t need a book or a theory or a psychology degree to make it work. Motivation comes first: everything else will happen by itself.

Brightbox: First Impressions

I’ve recently started using Brightbox to host a rebuilt Rails application for grain.org — I’d heard good things about Brightbox, and wanted to give them a try. Here are my first impressions.

Pricing

Brightbox servers start at £39/month for a virtual server with 10GB storage and 256MB RAM. You get 100GB of bandwidth per month, and five connections to a MySQL cluster (which is running very smoothly for me so far).

Myself and grain.org chose the slightly more expensive £69/month plan, which provides twice as much storage, memory, and bandwidth, and also includes New Relic RPM Bronze which would otherwise cost from around £25/month.

Signup

I didn’t sign up for the hosting account, but I’m told that Brightbox were able to re-activate an old account which the client cancelled a few months back. That seemed like a good start! As far as I can tell, the signup process was smooth and fast: I have access to a new Brightbox within a day.

Server Software

Brightbox servers use Ubuntu, which I have some experience with. It’s not to everyone’s taste, but I feel at home with it :) So far, the operating system has been very easy to use — I needed to install a some software that wasn’t included by default, and all it took was apt-get install package_name.

Brightbox servers also come with sudo, which is handy. The structure of the server is pretty standard, and you get a dedicated user (“rails”) for application deployment. SSH is fairly easy to configure, with instructions on the Brightbox Wiki, but I’ve been unable to stop SSH asking me for a password, which is a pain — that may be something I’ve done wrong, though, I can’t say.

Deployment & Application Management

One of my favourite things about Brightbox so far is their gem. This uses Capistrano and massively simplifies what would otherwise the complicated task of deploying a Rails application to an Apache/Mongrel set up. It’s even easy to change DNS records for a particular app. I love Capistrano, so bonus points there.

Support

I’ve had very little interaction with Brightbox support so far. I sent an email to their sales team before making the final decision about whether to use them for hosting, which email went un-replied for a few days until I emailed them a second time. To be fair, this is a one-off and hardly enough to judge quality of support on.

Community

One thing that really attracted me to Brightbox is their engagement with the Ruby and Rails community. It’s obvious that they’re genuinely interested in this software, and not just out to make some money off whatever’s cool on the internet this week. I know they’ve released several pieces of software (Ruby and Ubuntu-related, I think) back into the community, which is a big, big plus point in my eyes.

Conclusions

I’ve been using Brightbox for nearly a month now. So far, I’m impressed with the ease of deployment, server software, and community engagement — I’m hoping their support is of a similarly high quality too. It’s early days, but if you’re looking for a host for your Rails app, I’d recommend that you give Brightbox a try.

You still need to configure your web server to accept requests for your new domains

Email to Twitter

Checkout Twittermail, a free service which turns emails into tweets. Useful!