# 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