Skip to content

Instantly share code, notes, and snippets.

@xwmx
Last active December 14, 2015 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xwmx/5028526 to your computer and use it in GitHub Desktop.
Save xwmx/5028526 to your computer and use it in GitHub Desktop.
simple gravatar from email
%a{:href => user_path(@user)}
= image_tag(@user.gravatar_url({:size => 250,
:default => default_gravatar,
:width => "200px",
:height => "200px"}))
module GravatarHelper
def default_gravatar
'mm'
end
# image_url
# Rails currently only provides path helpers for assets. In this application
# the path also includes the asset host config, which is currently set up
# for multiple asset hosts. However, the asset host setting doesn't include
# the scheme / protocol since they are designed to be protocol agnostic.
# As a result, the URLs don't work when referenced externally, such as when
# passed as the 'default' parameter value in the gravatar URL. This helper
# constructs a full URL with the scheme.
def image_url(image, opts = {})
%Q|#{request.protocol.gsub(%r{//$}, '')}#{image_path(image)}|
end
end
class User < ActiveRecord::Base
# ...
def gravatar_url(options = {})
options = {:size => 80}.merge(options)
hash = Digest::MD5.hexdigest(self.email.downcase)
gravatar = "http://www.gravatar.com/avatar/#{hash}?size=#{options[:size]}"
if options[:default].present?
"#{gravatar}&default=#{URI.escape(options[:default])}"
else
gravatar
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment