Last active
December 14, 2015 04:29
-
-
Save xwmx/5028526 to your computer and use it in GitHub Desktop.
simple gravatar from email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%a{:href => user_path(@user)} | |
= image_tag(@user.gravatar_url({:size => 250, | |
:default => default_gravatar, | |
:width => "200px", | |
:height => "200px"})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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