Skip to content

Instantly share code, notes, and snippets.

@adonaldson
Created January 26, 2013 07:55
Show Gist options
  • Save adonaldson/4640884 to your computer and use it in GitHub Desktop.
Save adonaldson/4640884 to your computer and use it in GitHub Desktop.
Example of a gravatar helper method for Rails (3)
# Assumes two things
# i) Your user class's to_s method outputs something useful - perhaps their name or email address
# ii) Your user class has an 'email' attribute
#
# This will work with any ruby class, it doesn't have to be an activerecord model
#
# usage: gravatar_tag(my_user, size: '32x32', alt: 'This is the alt text', title: 'Any other options get passed into image_tag')
module UsersHelper
def gravatar_tag(user, image_options = {})
image_options[:size] ||= '80x80'
image_options[:alt] ||= image_options.delete(:alt) || user.to_s
pre_hash = user.email.strip.downcase
post_hash = Digest::MD5.hexdigest(pre_hash)
base_url = request.ssl? ? 'https://secure.gravatar.com' : 'http://www.gravatar.com'
url = "#{base_url}/avatar/#{post_hash}?s=#{image_options[:size].to_i}"
image_tag url, image_options
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment