Commit f338ff43 authored by Andreas Brandl's avatar Andreas Brandl

Refactor and split ApplicationHelper#avatar_icon.

When we don't use the original `ApplicationHelper#avatar_icon` anymore, we
can just remove it (and its specs).

Closes #42800.
parent 16908f97
...@@ -69,17 +69,27 @@ module ApplicationHelper ...@@ -69,17 +69,27 @@ module ApplicationHelper
end end
def avatar_icon(user_or_email = nil, size = nil, scale = 2, only_path: true) def avatar_icon(user_or_email = nil, size = nil, scale = 2, only_path: true)
user = if user_or_email.is_a?(User)
if user_or_email.is_a?(User) avatar_icon_for_user(user_or_email, size, scale, only_path: only_path)
user_or_email else
else avatar_icon_for_email(user_or_email, size, scale, only_path: only_path)
User.find_by_any_email(user_or_email.try(:downcase)) end
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
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 if user
user.avatar_url(size: size, only_path: only_path) || default_avatar user.avatar_url(size: size, only_path: only_path) || default_avatar
else else
gravatar_icon(user_or_email, size, scale) gravatar_icon(nil, size, scale)
end end
end end
......
...@@ -91,6 +91,54 @@ describe ApplicationHelper do ...@@ -91,6 +91,54 @@ describe ApplicationHelper do
end end
end end
describe 'avatar_icon_for_email' do
let(:user) { create(:user, avatar: File.open(uploaded_image_temp_path)) }
context 'using an email' do
context 'when there is a matching user' do
it 'returns a relative URL for the avatar' do
expect(helper.avatar_icon_for_email(user.email).to_s)
.to eq(user.avatar.url)
end
end
context 'when no user exists for the email' do
it 'calls gravatar_icon' do
expect(helper).to receive(:gravatar_icon).with('foo@example.com', 20, 2)
helper.avatar_icon_for_email('foo@example.com', 20, 2)
end
end
context 'without an email passed' do
it 'calls gravatar_icon' do
expect(helper).to receive(:gravatar_icon).with(nil, 20, 2)
helper.avatar_icon_for_email(nil, 20, 2)
end
end
end
end
describe 'avatar_icon_for_user' do
let(:user) { create(:user, avatar: File.open(uploaded_image_temp_path)) }
context 'with a user object passed' do
it 'returns a relative URL for the avatar' do
expect(helper.avatar_icon_for_user(user).to_s)
.to eq(user.avatar.url)
end
end
context 'without a user object passed' do
it 'calls gravatar_icon' do
expect(helper).to receive(:gravatar_icon).with(nil, 20, 2)
helper.avatar_icon_for_user(nil, 20, 2)
end
end
end
describe 'gravatar_icon' do describe 'gravatar_icon' do
let(:user_email) { 'user@email.com' } let(:user_email) { 'user@email.com' }
......
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