Commit 762901c9 authored by Marius Bobin's avatar Marius Bobin Committed by Nick Thomas

Add generic avatar method for users and groups

parent 280776b4
...@@ -98,6 +98,14 @@ module AvatarsHelper ...@@ -98,6 +98,14 @@ module AvatarsHelper
end end
end end
def avatar_without_link(resource, options = {})
if resource.is_a?(User)
user_avatar_without_link(options.merge(user: resource))
elsif resource.is_a?(Group)
group_icon(resource, options.merge(class: 'avatar'))
end
end
private private
def avatar_icon_by_user_email_or_gravatar(email, size, scale, only_path:) def avatar_icon_by_user_email_or_gravatar(email, size, scale, only_path:)
...@@ -136,9 +144,10 @@ module AvatarsHelper ...@@ -136,9 +144,10 @@ module AvatarsHelper
def source_identicon(source, options = {}) def source_identicon(source, options = {})
bg_key = (source.id % 7) + 1 bg_key = (source.id % 7) + 1
size_class = "s#{options[:size]}" if options[:size]
options[:class] = options[:class] =
[*options[:class], "identicon bg#{bg_key}"].join(' ') [*options[:class], "identicon bg#{bg_key}", size_class].compact.join(' ')
content_tag(:div, class: options[:class].strip) do content_tag(:div, class: options[:class].strip) do
source.name[0, 1].upcase source.name[0, 1].upcase
......
---
title: Add generic avatar method for users and groups
merge_request: 59758
author:
type: fixed
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
%thead %thead
%tr %tr
%th= _("Project") %th= _("Project")
%th= _("Author") %th= _("Owner")
%th %th
%tbody %tbody
- @project.upstream_project_subscriptions.each do |subscription| - @project.upstream_project_subscriptions.each do |subscription|
......
...@@ -3,8 +3,9 @@ ...@@ -3,8 +3,9 @@
%td %td
= project.name = project.name
%td %td
= user_avatar_without_link(user: project.owner, size: 32) .gl-display-flex.gl-align-items-center
= project.owner.name = avatar_without_link(project.owner, size: 32)
= project.owner.name
%td.gl-text-right %td.gl-text-right
= link_to project_subscription_path(@project, subscription.id), method: :delete, data: { toggle: 'tooltip', title: tooltip, container: 'body', testid: 'delete-subscription' }, class: "gl-button btn btn-danger" do = link_to project_subscription_path(@project, subscription.id), method: :delete, data: { toggle: 'tooltip', title: tooltip, container: 'body', testid: 'delete-subscription' }, class: "gl-button btn btn-danger" do
= sprite_icon('close', size: 16, css_class: 'gl-icon') = sprite_icon('close', size: 16, css_class: 'gl-icon')
...@@ -409,4 +409,33 @@ RSpec.describe AvatarsHelper do ...@@ -409,4 +409,33 @@ RSpec.describe AvatarsHelper do
end end
end end
end end
describe '#avatar_without_link' do
let(:options) { { size: 32 } }
subject { helper.avatar_without_link(resource, options) }
context 'with users' do
let(:resource) { user }
it 'displays user avatar' do
is_expected.to eq tag(
:img,
alt: "#{user.name}'s avatar",
src: avatar_icon_for_user(user, 32),
data: { container: 'body' },
class: 'avatar s32 has-tooltip',
title: user.name
)
end
end
context 'with groups' do
let(:resource) { build_stubbed(:group, name: 'foo') }
it 'displays group avatar' do
is_expected.to match(%r{<div class="avatar identicon bg\d+ s32">F</div>})
end
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment