Module: AvatarHelper

Defined in:
app/helpers/avatar_helper.rb

Instance Method Summary collapse

Instance Method Details

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

This returns the html code for an avatar image of the given user.

  • If the user has uploaded an avatar image using refile, this one is used.

  • Next, the gravatar of the user’s email is tried.

  • The fallback image is defined in the ‘user_avatar_default_url` method.



9
10
11
12
13
14
15
16
17
18
# File 'app/helpers/avatar_helper.rb', line 9

def user_avatar(user, options = {})
  options[:size] ||= 36
  (:span, class: 'avatar') do
    if user.avatar_id?
      image_tag Refile.attachment_url(user, :avatar, :fill, options[:size], options[:size]), class: 'img-rounded'
    else
      user_gravatar(user, options)
    end
  end.html_safe
end

#user_avatar_default_urlObject



66
67
68
# File 'app/helpers/avatar_helper.rb', line 66

def user_avatar_default_url
  "https://wingolfsplattform.org/assets/avatar_128.png"
end

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



49
50
51
52
53
54
55
56
# File 'app/helpers/avatar_helper.rb', line 49

def user_avatar_url(user, options = {})
  options[:size] ||= 36
  if user.avatar_id?
    Refile.attachment_url(user, :avatar, :fill, options[:size], options[:size])
  else
    user_gravatar_url(user, options)
  end
end

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

This returns the html code for an avatar image of the given user. This image is just provided by gravatar.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/avatar_helper.rb', line 23

def user_gravatar(user, options = {})

  email = user.email
  options[:size] ||= 36
  options[:gravatar] ||= {}
  options[:gravatar][:size] ||= options[:size]
  #options[:alt] ||= "Gravatar: #{email}"
  #options[:title] ||= options[:alt]
  options['data-toggle'] ||= 'tooltip'
  options[:gravatar][:secure] = true
  
  options[:class] ||= ""
  options[:class] += " img-rounded"

  # Default Url
  # Instead of 
  #     URI.join(root_url, asset_path('avatar_128.png'))
  # we use a string at the moment, in order to make this work
  # locally as well. Otherwise a 'http://localhost/...' would be 
  # submitted to gravatar as source of the default image.
  # 
  options[:gravatar][:default] ||= user_avatar_default_url
  
  gravatar_image_tag(email, options)
end

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



58
59
60
61
62
63
64
# File 'app/helpers/avatar_helper.rb', line 58

def user_gravatar_url(user, options = {})
  options[:gravatar] ||= {}
  options[:gravatar][:default] ||= user_avatar_default_url
  options[:gravatar][:size] ||= 36
  options[:gravatar][:secure] = true
  gravatar_image_url(user.email, options)
end