Module: AvatarsHelper

Constant Summary collapse

DEFAULT_AVATAR_PATH =
'no_avatar.png'

Instance Method Summary collapse

Instance Method Details

#author_avatar(commit_or_event, options = {}) ⇒ Object



61
62
63
64
65
66
67
68
# File 'app/helpers/avatars_helper.rb', line 61

def author_avatar(commit_or_event, options = {})
  user_avatar(options.merge({
    user: commit_or_event.author,
    user_name: commit_or_event.author_name,
    user_email: commit_or_event.author_email,
    css_class: 'd-none d-sm-inline-block'
  }))
end

#avatar_icon_for(user = nil, email = nil, size = nil, scale = 2, only_path: true) ⇒ Object

Takes both user and email and returns the avatar_icon by user (preferred) or email.



20
21
22
23
24
25
26
27
28
# File 'app/helpers/avatars_helper.rb', line 20

def avatar_icon_for(user = nil, email = nil, size = nil, scale = 2, only_path: true)
  if user
    avatar_icon_for_user(user, size, scale, only_path: only_path)
  elsif email
    avatar_icon_for_email(email, size, scale, only_path: only_path)
  else
    default_avatar
  end
end

#avatar_icon_for_email(email = nil, size = nil, scale = 2, only_path: true, by_commit_email: false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/avatars_helper.rb', line 30

def avatar_icon_for_email(email = nil, size = nil, scale = 2, only_path: true, by_commit_email: false)
  return default_avatar if email.blank?

  Gitlab::AvatarCache.by_email(email, size, scale, only_path) do
    avatar_icon_by_user_email_or_gravatar(
      email,
      size,
      scale,
      only_path: only_path,
      by_commit_email: by_commit_email
    )
  end
end

#avatar_icon_for_user(user = nil, size = nil, scale = 2, only_path: true, current_user: nil) ⇒ Object



44
45
46
47
48
49
50
# File 'app/helpers/avatars_helper.rb', line 44

def avatar_icon_for_user(user = nil, size = nil, scale = 2, only_path: true, current_user: nil)
  return gravatar_icon(nil, size, scale) unless user
  return default_avatar if blocked_or_unconfirmed?(user) && !can_admin?(current_user)

  user_avatar = user.avatar_url(size: size, only_path: only_path)
  user_avatar || default_avatar
end


113
114
115
116
117
118
119
120
# File 'app/helpers/avatars_helper.rb', line 113

def avatar_without_link(resource, options = {})
  case resource
  when Namespaces::UserNamespace
    user_avatar_without_link(options.merge(user: resource.first_owner))
  when Group
    group_icon(resource, options.merge(class: 'avatar'))
  end
end

#default_avatarObject



57
58
59
# File 'app/helpers/avatars_helper.rb', line 57

def default_avatar
  ActionController::Base.helpers.image_path(DEFAULT_AVATAR_PATH)
end

#gravatar_icon(user_email = '', size = nil, scale = 2) ⇒ Object



52
53
54
55
# File 'app/helpers/avatars_helper.rb', line 52

def gravatar_icon(user_email = '', size = nil, scale = 2)
  GravatarService.new.execute(user_email, size, scale) ||
    default_avatar
end

#group_icon(group, options = {}) ⇒ Object



10
11
12
# File 'app/helpers/avatars_helper.rb', line 10

def group_icon(group, options = {})
  source_icon(group, options)
end

#project_icon(project, options = {}) ⇒ Object



6
7
8
# File 'app/helpers/avatars_helper.rb', line 6

def project_icon(project, options = {})
  source_icon(project, options)
end

#topic_icon(topic, options = {}) ⇒ Object



14
15
16
# File 'app/helpers/avatars_helper.rb', line 14

def topic_icon(topic, options = {})
  source_icon(topic, options)
end

#user_avatar(options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'app/helpers/avatars_helper.rb', line 103

def user_avatar(options = {})
  avatar = user_avatar_without_link(options)

  if options[:user]
    link_to(avatar, user_path(options[:user]))
  elsif options[:user_email]
    mail_to(options[:user_email], avatar)
  end
end


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/helpers/avatars_helper.rb', line 70

def user_avatar_without_link(options = {})
  avatar_size = options[:size] || 16
  user_name = options[:user].try(:name) || options[:user_name]

  avatar_url = user_avatar_url_for(**options.merge(size: avatar_size))

  has_tooltip = options[:has_tooltip].nil? ? true : options[:has_tooltip]
  data_attributes = options[:data] || {}
  css_class = %W[avatar s#{avatar_size}].push(*options[:css_class])
  alt_text = user_name ? "#{user_name}'s avatar" : "default avatar"

  if has_tooltip
    css_class.push('has-tooltip')
    data_attributes[:container] = 'body'
  end

  if options[:lazy]
    css_class << 'lazy'
    data_attributes[:src] = avatar_url
    avatar_url = LazyImageTagHelper.placeholder_image
  end

  image_options = {
    alt: alt_text,
    src: avatar_url,
    data: data_attributes,
    class: css_class,
    title: user_name
  }

  tag.img(**image_options)
end