Extract repeated logic into #avatar_icon_for.
This essentially allows to pass both user and email, so that we can either prefer the user to retrieve the avatar or (if user is not present) fall back to the email lookup.
This commit is contained in:
parent
6670074c06
commit
dd1d13b859
4 changed files with 32 additions and 16 deletions
|
@ -68,6 +68,18 @@ module ApplicationHelper
|
|||
end
|
||||
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)
|
||||
user = User.find_by_any_email(email.try(:downcase))
|
||||
if user
|
||||
|
|
|
@ -60,10 +60,7 @@
|
|||
%tbody
|
||||
%tr
|
||||
%td{ style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:15px;line-height:1.4;vertical-align:middle;padding-right:5px;" }
|
||||
- if commit.author
|
||||
%img.avatar{ height: "24", src: avatar_icon_for_user(commit.author, 24, only_path: false), style: "display:block;border-radius:12px;margin:-2px 0;", width: "24", alt: "" }/
|
||||
- else
|
||||
%img.avatar{ height: "24", src: avatar_icon_for_email(commit.author_email, 24, only_path: false), style: "display:block;border-radius:12px;margin:-2px 0;", width: "24", alt: "" }/
|
||||
%img.avatar{ height: "24", src: avatar_icon_for(commit.author, commit.author_email, 24, only_path: false), style: "display:block;border-radius:12px;margin:-2px 0;", width: "24", alt: "" }/
|
||||
%td{ style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:15px;line-height:1.4;vertical-align:middle;" }
|
||||
- if commit.author
|
||||
%a.muted{ href: user_url(commit.author), style: "color:#333333;text-decoration:none;" }
|
||||
|
@ -79,10 +76,7 @@
|
|||
%tbody
|
||||
%tr
|
||||
%td{ style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:15px;line-height:1.4;vertical-align:middle;padding-right:5px;" }
|
||||
- if commit.commiter
|
||||
%img.avatar{ height: "24", src: avatar_icon_for_user(commit.committer, 24, only_path: false), style: "display:block;border-radius:12px;margin:-2px 0;", width: "24", alt: "" }/
|
||||
- else
|
||||
%img.avatar{ height: "24", src: avatar_icon_for_email(commit.committer_email, 24, only_path: false), style: "display:block;border-radius:12px;margin:-2px 0;", width: "24", alt: "" }/
|
||||
%img.avatar{ height: "24", src: avatar_icon_for(commit.committer, commit.committer_email, 24, only_path: false), style: "display:block;border-radius:12px;margin:-2px 0;", width: "24", alt: "" }/
|
||||
%td{ style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:15px;line-height:1.4;vertical-align:middle;" }
|
||||
- if commit.committer
|
||||
%a.muted{ href: user_url(commit.committer), style: "color:#333333;text-decoration:none;" }
|
||||
|
|
|
@ -60,10 +60,7 @@
|
|||
%tbody
|
||||
%tr
|
||||
%td{ style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:15px;line-height:1.4;vertical-align:middle;padding-right:5px;" }
|
||||
- if commit.author
|
||||
%img.avatar{ height: "24", src: avatar_icon_for_user(commit.author, 24, only_path: false), style: "display:block;border-radius:12px;margin:-2px 0;", width: "24", alt: "" }/
|
||||
- else
|
||||
%img.avatar{ height: "24", src: avatar_icon_for_email(commit.author_email, 24, only_path: false), style: "display:block;border-radius:12px;margin:-2px 0;", width: "24", alt: "" }/
|
||||
%img.avatar{ height: "24", src: avatar_icon_for(commit.author, commit.author_email, 24, only_path: false), style: "display:block;border-radius:12px;margin:-2px 0;", width: "24", alt: "" }/
|
||||
%td{ style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:15px;line-height:1.4;vertical-align:middle;" }
|
||||
- if commit.author
|
||||
%a.muted{ href: user_url(commit.author), style: "color:#333333;text-decoration:none;" }
|
||||
|
@ -79,10 +76,7 @@
|
|||
%tbody
|
||||
%tr
|
||||
%td{ style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:15px;line-height:1.4;vertical-align:middle;padding-right:5px;" }
|
||||
- if commit.committer
|
||||
%img.avatar{ height: "24", src: avatar_icon_for_user(commit.committer, 24, only_path: false), style: "display:block;border-radius:12px;margin:-2px 0;", width: "24", alt: "" }/
|
||||
- else
|
||||
%img.avatar{ height: "24", src: avatar_icon_for_email(commit.committer_email, 24, only_path: false), style: "display:block;border-radius:12px;margin:-2px 0;", width: "24", alt: "" }/
|
||||
%img.avatar{ height: "24", src: avatar_icon_for(commit.committer, commit.committer_email, 24, only_path: false), style: "display:block;border-radius:12px;margin:-2px 0;", width: "24", alt: "" }/
|
||||
%td{ style: "font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:15px;line-height:1.4;vertical-align:middle;" }
|
||||
- if commit.committer
|
||||
%a.muted{ href: user_url(commit.committer), style: "color:#333333;text-decoration:none;" }
|
||||
|
|
|
@ -63,6 +63,22 @@ describe ApplicationHelper do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'avatar_icon_for' do
|
||||
let!(:user) { create(:user, avatar: File.open(uploaded_image_temp_path), email: 'bar@example.com') }
|
||||
let(:email) { 'foo@example.com' }
|
||||
let!(:another_user) { create(:user, avatar: File.open(uploaded_image_temp_path), email: email) }
|
||||
|
||||
it 'prefers the user to retrieve the avatar_url' do
|
||||
expect(helper.avatar_icon_for(user, email).to_s)
|
||||
.to eq(user.avatar.url)
|
||||
end
|
||||
|
||||
it 'falls back to email lookup if no user given' do
|
||||
expect(helper.avatar_icon_for(nil, email).to_s)
|
||||
.to eq(another_user.avatar.url)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'avatar_icon_for_email' do
|
||||
let(:user) { create(:user, avatar: File.open(uploaded_image_temp_path)) }
|
||||
|
||||
|
|
Loading…
Reference in a new issue