Module: AvatarHelper

Defined in:
app/helpers/avatar_helper.rb

Instance Method Summary collapse

Instance Method Details

#avatar_for(user, options = {}) ⇒ Object



5
6
7
8
9
10
11
# File 'app/helpers/avatar_helper.rb', line 5

def avatar_for(user, options={})
  size = options.fetch(:size, 24)

  return "<div class=\"avatar avatar-empty\" style=\"width:#{size}px; height:#{size}px\"></div>".html_safe unless user

  "<img class=\"avatar user-#{user.id}\" src=\"#{gravatar_url(user.email, size: size * 2)}\" width=\"#{size}\" height=\"#{size}\" alt=\"#{user.name}\" />".html_safe
end

#gravatar_image(email, options = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'app/helpers/avatar_helper.rb', line 14

def gravatar_image(email, options={})
  return "" if email.blank?

  size = options.fetch(:size, 24)
  alt = options[:alt]
  "<img class=\"avatar\" src=\"#{gravatar_url(email, size: size * 2)}\" width=\"#{size}\" height=\"#{size}\" alt=\"#{alt}\" />".html_safe
end

#gravatar_url(email, options = {}) ⇒ Object



25
26
27
28
29
# File 'app/helpers/avatar_helper.rb', line 25

def gravatar_url(email, options={})
  url = "https://www.gravatar.com/avatar/#{Digest::MD5::hexdigest(email)}?r=g&d=retro"
  url << "&s=#{options[:size]}" if options.key?(:size)
  url
end