August 30, 2010
Overriding attr_accessible with Inherited Resources

At Kisko we use José Valim’s excellent Inherited Resources whenever we see fit.

Last night while building a quick administration interface, I wanted to disable attr_accessible for the whole Admin namespace, since administrators can change any attributes on any model. Also, I didn’t want to override any controller actions.

I ended up with the following code on Admin::AdministrationController, a controller that every other controller in the Admin namespace inherits from.

That’s the sort of code that reminds me how neat Ruby is.

// joao

August 26, 2010
Realtime Client Push to Rails Application with Pusher

One Wednesday in August my friend calls and tells me there’s a soccer tournament that he’d like to do reporting on and publish it live online. He intends to post scores, photos and do live play-by-play like text reporting. All he needs is the app to do that and the tournament is in two days, so the time window is short. So I need to get busy quickly.

Soon we can upload photos and post live updates of what would be happening on the pitch. We’d have a “live page” where all the action would get posted and we would like to have the newest content on the screens of our audience as soon as possible. One way would be to have the client browsers making Ajax requests, say every 30 seconds, and pull new stuff if there were any. But that’d surely result in a lot of unnecessary requests. There has to be a better way.

It turns out there’s a realtime push service named Pusher. With Pusher, the client browsers connect to the service with WebSockets and listen to a channel for events. Your Rails application talks to Pusher service and triggers events. 

Pusher is in public beta, so I signed up to try it out. They have a good Quickstart Guide which covers everything you need to get up and running.

You’ll need the Pusher gem that deals with the server communication so go ahead and install it.

  $ gem install pusher

Next, I created config/initializers/pusher.rb Rails initializer to hold Pusher gem configuration. I added app_id, key and secret to the initializer so that the interface would be properly configured for me in the application.

On the live page we had an scrolling image gallery and the play-by-play feed. So I bound the client to a few events that would be triggered when new content would be posted. These are defined in JavaScript. 

Whenever I would create a new entry to play-by-play feed, “entry-posted” event was sent to the “live” channel and the entry was added to the top of the stack of entries and faded in. When I uploaded a new photo to the gallery, “image-posted” event was sent to the “live” channel and the photo would be added to the image gallery.

In the controllers for entries and photos, I’d trigger Pusher events like this.

That’s it! As you can see, Pusher is super simple to use and very useful. You can offload the content push to Pusher service and don’t have to tax your app server with Ajax requests. Also, updates are pushed to the clients immediately instead of some delay that one might have if one had a recurring Ajax request happening to pull possible new content.

// Lauri

August 5, 2010
Comparing Rails locale files for missing translations

If you ever had to support more than one language in a Rails app, you’ve probably ran into this problem. At some point you’ll either find a missing translation on the application itself, or wonder if there are any.

Here’s a very short script that can help you make sure all is well. Just tune the first two lines to match your needs and run it.

// joao

July 29, 2010

Over the past few weeks I’ve been working on a deployment tool that will make everyone’s life a lot simpler. If you use Git, nginx and Unicorn, this is the way to go.

We’re planning to release this open source gem on Whyday (19th of August). Until then, we’ll be adding new features and testing it in production.

We’re hosting a Ruby Brigade meetup on Whyday at 18:00, so feel free to join the launch party!

Over the next couple of weeks, I’ll try to announce the planned features.

// joao

July 26, 2010
Kisko Link Bag #3

Here’s some interesting links from our Campfire transcripts.

July 19, 2010
Kisko Link Bag #2

Here’s some links that we’ve found interesting during the last week.

July 14, 2010
Secure, expiring download links with Nginx and Rails

What to do when you need secure links to static assets, for example eBooks or video downloads? One solution for this is to use the SecureDownload module for nginx. The SecureDownload module uses a shared secret to generate secure, expiring links. The best thing about this approach is that the actual file serving is handled by nginx which performs way better than having your Rails app do it.

Please note that this tutorial assumes that you already have some idea of how nginx works and how it is configured. Make sure that you understand what you’re doing before doing it.

Since nginx cannot load modules at runtime, you’ll have to compile it yourself, taking care to include the SecureDownload module (direct link: Ngx_http_secure_download.tar.gz).

Here’s a fairly simple configure for nginx (make sure you adjust it as needed for your system and needs, more on that on the nginx wiki):

Also remember to adjust the last line of the configure so that it matches the location where you extracted the SecureDownload module.

The nginx wiki has examples from a few systems (including Debian and RedHat), so you should be able to recreate your normal nginx quite well. I also have a .deb file with the SecureDownload module baked-in in my Launchpad PPA.

Once you’ve configured, compiled, and installed nginx you’ll need to configure your site to use it. Here’s an example configuration file for a Unicorn + Nginx setup:

In your controller you’ll need to do something like this:

That’s the rough idea. You’ll probably need to make some adjustments to get this to work with your application and system.

// matt

July 14, 2010
Campfire Link Harvester

For the last post I wrote I needed to get a bunch of links that we at Kisko Labs had found interesting over the last week or so.

Naturally I turned to Campfire and the Tinder gem and knocked out a quick and dirty Ruby script to harvest the links from Campfire. Of course you’ll still have to weed out uninteresting and confidential links yourself.

/ matt

July 10, 2010
Kisko Link Bag #1

This is a collection of links we found interesting over the last week or so, mostly picked from our Campfire transcripts.

July 6, 2010
Rails 3, Resque, and Devise

Update 1: These instructions should work just fine for any Rack application mounted in Rails 3.

Update 2: After talking to José Valim about this, it turns out that in the latest Device (1.1rc2 and up) you can also do this in the routes file, like this:


So you have your fancy new Rails 3 app working with some Resque awesomeness, but now you’d like to integrate the Resque web interface into your administration. Easy with Rails 3’s new mountable Rack apps!

First of all add the resque gem to your Gemfile:

And add this line to your config/routes.rb:

Then fire up your preferred app server and navigate to http://localhost:3000/resque

This is great and all, but now all and sundry can access your queues and wreak all sorts of havoc. Devise and Rack to the rescue (pun not intended).

Create a new initializer (config/initializers/resque.rb) with the following code:

Make sure to change the scope to what ever is appropriate for you application. You might also need to adjust the strategies being used (in our case database_authenticatable and rememberable).

// matt

// joao