Adding Gravatars to your Rails application
For a good while I’ve been using a particular gravatar plugin to fetch gravatars for the people who leave comments on my mighty fine blog here. Unfortunately I found it to be restricting and the documentation poor. So rather than digging about the code to see how I could manipulate it to change the default gravatar(the nasty blue one was doing my tits in), I stumbled across this gem. It’s not actually a gem if you know what I mean, but it’s literally a few lines of code.
Anywho I went and updated it slightly so I can change the default gravatar to the snazzier identicon.
1 2 3 4 | def gravatar_url_for(email = null, size = 45, default = "identicon") hash = MD5::md5(email.downcase) image_src = "http://www.gravatar.com/avatar/#{hash}?s=#{size}&d=#{default}" end |
Just make sure to require the md5 library at the top of your helper file and your good to go!
To be frank, upon reading how the gravatar url’s must be constructed, I should have made something like this myself a long time ago, especially seeing how simple it is!
Enjoyed this article? Be sure to subscribe to my RSS feed.
