Google Apps OpenID with Rails and Devise

Key points

The story

I’ve been working on a client project consolidating most of their various internal databases, spreadsheets and ad-hoc lists into a coherent and centralized web application – employee lists, inventory and so on. Early on the client said to me: “Since we are using Google Apps for email, calendar, document sharing and pretty anything else it can do, it would be really nice if we could simply login to the application you are building using our Google Apps logins.” I said: “Sure, how hard can it be?” It was a fair request that made a lot of sense. After all, everybody hates another login to remember, so wouldn’t it be nice if the employees was simply – “as by magic” – instantly signed in to this new application?

I knew a lot of people was doing sign in with ordinary Google accounts. I had also implemented it myself for darebusters, although the easy way using the very nice RPX solution from JanRain. I could see that I could integrate with Google Apps using both OpenID and OAuth. I had some vague ideas about OAuth being “newer” and “cooler” than OpenID, and also I was using Warden and Devise for authentication, for which a nice OAuth extension existed, so I set out to authenticate against Google Apps with OAuth.

Continue reading

Delayed Job with i18n

I have darebusters.com running on Heroku and I have a worker running Delayed Job (DJ) activated. This allows me to handle heavy tasks such as video processing and payment transactions asynchronously, so the dynos can get right back to serving requests.

I also use DJ to send out emails, and a typical asynchronous call sending a message from one user to another – scheduling a delayed job – used to look like this:

UserMailer.send_later(
  :deliver_message,
   @recipient,
  current_user,
  params[:message]
)

Since darebusters.com is an internationalized website currently offering users the choice between English and German, this has proved a slight challenge, however. We have the usual internationalization (i18n) bells and whistles: Flags to select the language, and remembering the user’s language selection in session, cookie and in the users database table. By default, a delayed job will simply run with the default locale of the site. It has no way of accessing the context it was invoked in, so it cannot set the locale from session or cookie. The job only knows what it can work it from the arguments it was created with.

Continue reading

IronRuby on Rails

Rails, say hello to .NET.

IronRuby went 1.0 at April 12th, and it runs Ruby on Rails for real. I demonstrated it myself to a packed room of (mostly) .NET developers at Community Day. There was a lot of buzz around IronRuby more than two years ago – back then, the hype around Ruby on Rails was at its peak and the .NET developers wanted in. It took a lot longer for IronRuby to go gold than many of us had hoped, and meanwhile ASP.NET MVC made its entry. Many ASP.NET developers were aware that the “webform” abstraction with its events, viewstate etc. is leaky and broken, and when Microsoft finally presented this “official” alternative to webforms they rallied to it and rejoiced, for now they had their own Rails-clone, but with a proper programming language and thorough IDE-support.

My presentation compared ASP.NET MVC to Ruby on Rails. The conclusion is ambiguous. I thought I’d be able to crush ASP.NET MVC with the pure greatness of Ruby and Rails, making it obvious to everyone which was the better framework and language. While Rails do support a lot things that ASP.NET MVC is nowhere near of having, the latter does – at least with the recently released version 2 – have the central elements of a proper MVC framework in place. What’s more surprising is that C# 4.0 is so much less clunky than C# 1.0, so in some comparisons of syntax Ruby is not even the obvious winner when it comes to readability. Take this example:

Before LINQ, C# would have never been able to map a collection in a such an elegant manner. In my biased opinion, I still find the Ruby version prettier and more readable, but the fact is that C# is not that far behind. It’s another syntax paradigm for sure, but it’s compact and it’s readable – that’s not bad at all.

Continue reading

Bundler and Rails 3 environments

I usually write very long blog posts. Not this time! This is just a small tip, the first of hopefully many learnings I’ll make as I am working with my first project in Rails 3.

Bundler (Github | Website) is the new way to manage gem dependencies in Rails and other Ruby applications. If you haven’t heard about it, there is (of course) a great introduction at Railcasts. It’s pretty easy and straight-forward to use, especially if you are used to the config.gem format of Rails.

However, one very common use case in Bundler’s Gemfile that is not explicitly documented anywhere I’ve seen, is how to configure gems to be installed in both development and test environments, but not in staging and production environments. You have probably seen the examples of how you can assign a gem to a specific environment using the :group option or method. So you’d probably write something like this:

group :test do
  gem "rspec-rails", ">= 2.0.0.beta.1"
end

group :development do
  gem "ruby-debug"
end

Continue reading

The culture of development communities: Ruby vs. .NET

I have worked professionally with Ruby on Rails for the past 4 years and definitely consider myself a part of the Ruby community. Before that, however, I was a .NET consultant for many years – in fact I worked with .NET from the early betas and I was very exited about replacing VBScript and classic ASP with C# and ASP.NET. Being at MIX 10 – Microsoft’s conference for developers and designers working with web and mobile technologies – has made me realize both how much the .NET community has changed since I left it – and how much it hasn’t. In this article I outline the differences in culture in the Ruby and .NET development communities as I perceive them, and voice my opinion on what could be changed.

I have a good friend named Oliver Kofoed who is a brilliant .NET developer. Oliver is an entrepreneur and has written the entire backend for Nonoba and Player IO, he has written his own object-relational mapper (ORM), templating system, multiplayer game server, content management system and so on. He has also been toying with several experimental .NET languages over the years (such as Boo) and even worked a bit on one of his own. Oliver is both curious and passionate about development, and he was in fact the one who initially told me about Ruby on Rails – I owe you one, Oliver ;)

I occasionally nudge Oliver about releasing some of his work as open source, but it has yet to happen. I don’t think Oliver mind sharing his stuff with the community, but the fact is that it takes a lot of work to prepare your code for open sourcing, especially if you haven’t build it with that in mind from the beginning. You need to take anything out that’s specific to your own applications, you have to write at least some documentation and you probably also have to make the code a bit more configurable and modular. To make that effort you need to get something in return from the community – you have to be reasonably sure that your efforts will be appreciated and that your peers will respect you for sharing valuable code with them.

Continue reading

Curiosities of HTML5

Molly Holzschlag talked about HTML5 (yes, apparently the convention is no space between HTML and 5) at the MIX 10 conference. I didn’t get any ground-breaking knowledge into the workings of, say, the <video /> tag, but I did get some insights into what’s happening with HTML5 that I’d like to share. For a more technical reference to HTML5, Holzschlag recommended Dive into HTML5.

MIX 10 keynote graphics
© 2010 MIX

You might have noticed that I did not link HTML5 to W3C’s specification of the language, but rather to the Wikipedia entry. The reason for that is not just the entry on Wikipedia is much easier to read than any academical spec, but also that W3C’s spec is not necessarily the official spec – or least not the only recognized spec. Molly Holzschlag explained how members of the XHTML 2.0 working group in 2004 grew tired of the slow progress and seemingly wrong direction of what was at the time supposed to be the next generation of HTML, and formed their own working group known as WHAT-WG. These “cowboys”, as Holzschlag likes to call them, very quickly (that is, they did in less than a year!) reached consensus on a new spec and further managed to get all the major browser vendors on board. As a result, W3C eventually shut down their XHTML 2 working group and started their own HTML5 working which adapts and formalizes the WHAT-WG spec into the the long legal specs we know and love … right.

Continue reading

Community Day ’10 is coming up

I did a well-received talk on ActiveRecord at last year’s Community Day. Community Day ’09 was the first of its kind in Copenhagen, and it was quite successful in bringing developers with different technical backgrounds together as well as attracting students – probably because of the free beer :)

Community Day in Copenhagen is back again this year, so reserve May 27 if you are near Copenhagen and like free tech-talks, networking and beer. This year Daniel Frost, the Microsoft evangelist that makes it happen, has involved me and several other developers actively in the planning of the day. With CD ’10 we have raised the level of ambition – bigger venue, more people, more talks and if course more fun.

We will have 20 sessions distributed on four concurrent tracks covering a surprisingly wide number of topics – very few of the talks are on Microsoft-technologies, in fact so few that we might loose a few of those .NET consultants who thinks anything non-MS are not worth listening to ;) Still, if you are doing anything at all related to the web (and most of us are, right?) you will surely find topics such as HTML 5, Single Sign On, Azure, Advanced jQuery etc. interesting.

Continue reading

Multiple Ruby versions with RVM

Rails 3 is just around the corner. It is optimized for the better performance and superior features of Ruby 1.9, but it also plays nicely with version 1.8.7. Ruby 1.8.6, on the other hand, cannot run Rails 3. This might not be a problem for you at all. If you are on a Mac, you were probably faced with Ruby 1.8.7 last year at the latest, when the upgrade to Snow Leopard changed the built-in Ruby version from 1.8.6 to 1.8.7. Most applications built for 1.8.6 runs fine on 1.8.7 – but many servers and deployment environments still runs only 1.8.6.

For me, the situation is like this: My standard version of Ruby on my development machine is 1.8.7. I deploy to many different environments, some (the ones I have a degree of control over) runs 1.8.7 and others (such as Heroku and some customer’s servers) runs 1.8.6. I can test and code away happily only to get some ugly exceptions when I deploy because I have called count on an array instead of length. Further, I also want to play with the new features of Ruby 1.9 and run Rails 3 on it. And that’s not to mention that I also use alternative Ruby implementations such as JRuby for projects where I need to tap into Java libraries.

Continue reading

Introducing darebusters.com

Most of us enjoy getting a link to something entertaining once in a while. In the early days of the World Wide Web, we send each other funny pictures via emails – today, we share our favorite videos via Twitter, Facebook and other social media. Most of these are so called “user-generated content” – that is; videos made by you and me for our own amusement and to get some feedback from other people. Many videos are – to put it nicely – mediocre, but the ones that spread and go viral are usually really good. Not because they necessarily feature great talent or professionalism, but simply because they are so damn entertaining.

For some reason – and I am sure people with a bit more insight into the human psyche than me can tell you exactly why – videos where people do something really stupid, disgusting, dangerous or daring are amongst the most popular. But what reward do the people who makes these videos get for their hard work? A lot of views and comments on Youtube? That’s nice, sure, but couldn’t we get something even better if the makers actually earned some money on entertaining the rest of us?

Continue reading