The Disco Blog

Can you dig it?

Lickety-split Custom Validations in Rails

Have a highly specific, yet custom validation for a particular field on one of your Rails model objects? Don’t want to create a ActiveModel::Validator type? Not a problem!

You can just as easily create a method that can be invoked as part of the validation process. For example, imagine a field dubbed uri in some model object; this field must begin with a protocol (i.e. http or https). You can create a validation method like so:

Custom validator
1
2
3
4
5
def uri_should_start_with_protocol
  if !uri.start_with?('http://') && !uri.start_with?('https://')
    errors.add(:uri, 'Web Address should start with http:// or https://')
  end
end

This method resides in your model. You can then register this method as a validation for your model like so:

Wiring the validation
1
validate :uri_should_start_with_protocol

Now if the uri field doesn’t contain http or https, model.save will return false. Done!

Backgrounding Tasks in Heroku With Delayed Job

Long running web requests are bad. They create resource contention among other waiting incoming requests; what’s more, with HTTP servers like Unicorn (which is rapidly becoming the de facto HTTP server for Rails apps running on Heroku), long running requests are summarily terminated after some threshold (for example, 30 seconds) leaving users with a nasty timeout error (and, of course, other users complaining that it takes forever for your site to do anything). Luckily, with frameworks like Delayed Job, backgrounding long running tasks couldn’t be any easier.

MongoDB to CSV

Every once in a while, I need to give a non-technical user (like a business analyst) data residing in MongoDB; consequently, I export the target data as a CSV file (which they can presumably slice and dice once they import it into Excel or some similar tool). Mongo has a handy export utility that takes a bevy of options, however, there is an outstanding bug and some general confusion as to how to properly export data in CSV format.

Callbacks in Appcelerator Titanium Modules

I recently found myself implementing both an Android and iOS Appcelerator module for App47’s respective Agent libraries. Like PhoneGap plugins, Appcelerator modules are a way to bridge an Appcelerator app with native code running on a device; in this case, the native code happens to be App47’s Android and IOS Agents, which capture usage analytics and facilitate a few security features. Naturally, these Agent libraries are coded in Java and Objective-C.

In the end, what I wanted to implement was a JavaScript-ish callback associated with a native App47 Agent call. Alas, it took me a lot of digging to achieve this goal.

Mobile for the Masses: Activities and Icons in Your Android Application Lifecycle

My third article in IBM developerWorks’ series Mobile for the Masses is now out! This hip article covers the lifecycle events associated with Activity classes and then shows you how to implement menus and/or activity bars (for newer devices) along with associating icons for an app itself and its corresponding activity items.

As the article summary states:

The Activity class is the workhorse of an Android mobile app, and it’s also where you can fine-tune your app’s interactions with both the user and the mobile device. Get things working exactly the way you want them in your app’s lifecycle, then use icons and action bars to guide users through UI navigation and other app features.

Check out ”Mobile for the masses: Activities and icons in your Android application lifecycle” and add navigation with style to your Android mobile apps! If you missed the first article, check out ”Mobile for the masses: A gentle introduction to Android”; what’s more, don’t forget to read my second article, entitled ”Mobile for the masses: Take a swipe at it! Programming gestures in Android”.

Stay tuned for more articles in this series and don’t forget to watch for mobile focused articles on this blog as well.

ElasticSearch on EC2 in Less Than 60 Seconds

Curious to see what all the ElasticSearch hubbub is about? Wanna see it in action without a lot of elbow grease? Then look no further, friend – in less than 60 seconds, I’ll show you how to install ElasticSearch on an AWS AMI.

You’ll first need an AWS account along with an SSH key pair. If you don’t already have those two steps done, go ahead and do that. The steps that follow suggest a particular AMI; however, you are free to select the instance type. Micro instance types are free to use; consequently, you can get up and running with ElasticSearch in less than a minute for free.

Mobile-isticly Optimized in 10 Seconds

Anyone ever told you that your website isn’t mobile optimized? Or have you ever seen a lilliputian-looking website on your device? You know, one that renders so small you are forced to squint as you enlarge various parts of the site with your fingers just to read it?

Websites render this way on mobile devices because they lack a simple meta tag. While the subject of mobile website optimization can be rather involved (especially when dealing with CSS media queries, which take longer than 10 seconds to understand), there is a simple trick that can at least can make your website render normally on a mobile device. And it can be done in 10 seconds.

The Democratization of Search

Over the past year and a half, I’ve watched ElasticSearch grow from a seemingly part-time code experiment into a thriving ecosystem. Not only has the number of client libraries grown from 1 to over 25 (and counting!); it’s now a commercially sponsored project to the tune of $34 million (a $10M series A and a $24M series B) with 200,000 downloads a month.

Search is the touchstone of the Internet; without search the Internet wouldn’t be all that useful. Google’s meteoric rise and its resultant eponymous name for search is clear evidence as to the importance of search. Search, however, hasn’t always been that easy nor affordable to implement.

Hold Your Horses! It’s Still Only a Two Horse Race.

There has been a lot of excitement in recent months regarding a few new entrants to the mobile operating systems arena including Firefox OS, Tizen, and Ubuntu Touch. These relatively nascent projects, which are all built on top of a Linux kernel, appear to have fully embraced HTML 5 apps; indeed, HTML 5 apps on these operating systems are first class citizens that can run without a browser. Moreover, the ubiquity of HTML 5 means the same app can run on all 3 of these operating systems plus Android and iOS!

What’s also exciting is that there are some major players behind these projects – Mozilla is, of course behind Firefox OS, Canonical is behind Ubuntu Touch, and quite a few large players are behind Tizen, including Samsung. Firefox OS even appears to be courting carriers with an opportunity to run their own app stores. This app store opportunity naturally has carriers foaming at the mouth to re-assert some semblance of control: behold the Verizon app store! All apps are $0.99 and we’ll just bill you at the end of the month (like we used to do for all those ringtones you were accustomed to download back when we were in control of all things device-related before Apple destroyed that cash cow).

The Rails, CloudFront, and Heroku Performance Hat-trick

Amazon CloudFront is a pay-as-you-go global content delivery network (or CDN) that provides high availability and high performance serving of static assets. Basically, it means users have to wait less time to view your web app regardless of their location on the globe.

It’s easy to configure a Rails app to take advantage of CloudFront; what’s more, if your Rails app is hosted on Heroku, there’s a nifty gem, dubbed heroku-deflater, that’ll enable HTTP compression of static assets (other than images).

To get these three entities to play nicely together requires a few simple steps. Let me show you how.