avatars_helper.rb 3.37 KB
Newer Older
1 2
# frozen_string_literal: true

3
module AvatarsHelper
4 5
  def project_icon(project, options = {})
    source_icon(project, options)
6 7
  end

8 9
  def group_icon(group, options = {})
    source_icon(group, options)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  end

  # Takes both user and email and returns the avatar_icon by
  # user (preferred) or email.
  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

  def avatar_icon_for_email(email = nil, size = nil, scale = 2, only_path: true)
25
    user = User.find_by_any_email(email)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    if user
      avatar_icon_for_user(user, size, scale, only_path: only_path)
    else
      gravatar_icon(email, size, scale)
    end
  end

  def avatar_icon_for_user(user = nil, size = nil, scale = 2, only_path: true)
    if user
      user.avatar_url(size: size, only_path: only_path) || default_avatar
    else
      gravatar_icon(nil, size, scale)
    end
  end

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

  def default_avatar
    ActionController::Base.helpers.image_path('no_avatar.png')
  end

50 51 52 53 54
  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,
55
      css_class: 'd-none d-sm-inline'
56 57 58
    }))
  end

59 60 61 62 63 64 65 66 67 68
  def user_avatar_url_for(options = {})
    if options[:url]
      options[:url]
    elsif options[:user]
      avatar_icon_for_user(options[:user], options[:size])
    else
      avatar_icon_for_email(options[:user_email], options[:size])
    end
  end

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

73
    avatar_url = user_avatar_url_for(options.merge(size: avatar_size))
74

75
    has_tooltip = options[:has_tooltip].nil? ? true : options[:has_tooltip]
76
    data_attributes = options[:data] || {}
77 78 79 80
    css_class = %W[avatar s#{avatar_size}].push(*options[:css_class])

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

84 85 86 87 88 89 90 91 92 93
    if options[:lazy]
      css_class << 'lazy'
      data_attributes[:src] = avatar_url
      avatar_url = LazyImageTagHelper.placeholder_image
    end

    image_options = {
      alt:   "#{user_name}'s avatar",
      src:   avatar_url,
      data:  data_attributes,
94
      class: css_class,
95 96 97 98
      title: user_name
    }

    tag(:img, image_options)
99 100 101 102
  end

  def user_avatar(options = {})
    avatar = user_avatar_without_link(options)
103 104 105 106 107 108 109

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

  private

113 114
  def source_icon(source, options = {})
    avatar_url = source.try(:avatar_url)
115

116 117
    if avatar_url
      image_tag avatar_url, options
118 119 120 121 122 123 124
    else
      source_identicon(source, options)
    end
  end

  def source_identicon(source, options = {})
    bg_key = (source.id % 7) + 1
125 126 127

    options[:class] =
      [*options[:class], "identicon bg#{bg_key}"].join(' ')
128 129 130 131 132

    content_tag(:div, class: options[:class].strip) do
      source.name[0, 1].upcase
    end
  end
133
end