Allow avatar_icon to operate on a User
If the User object is already known before calling this method being able to re-use said object can save us an extra SQL query.
This commit is contained in:
parent
fb7785628a
commit
693e63f523
|
@ -68,13 +68,17 @@ module ApplicationHelper
|
|||
end
|
||||
end
|
||||
|
||||
def avatar_icon(user_email = '', size = nil)
|
||||
user = User.find_by(email: user_email)
|
||||
def avatar_icon(user_or_email = nil, size = nil)
|
||||
if user_or_email.is_a?(User)
|
||||
user = user_or_email
|
||||
else
|
||||
user = User.find_by(email: user_or_email)
|
||||
end
|
||||
|
||||
if user
|
||||
user.avatar_url(size) || default_avatar
|
||||
else
|
||||
gravatar_icon(user_email, size)
|
||||
gravatar_icon(user_or_email, size)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -99,6 +99,15 @@ describe ApplicationHelper do
|
|||
|
||||
helper.avatar_icon('foo@example.com', 20)
|
||||
end
|
||||
|
||||
describe 'using a User' do
|
||||
it 'should return an URL for the avatar' do
|
||||
user = create(:user, avatar: File.open(avatar_file_path))
|
||||
|
||||
expect(helper.avatar_icon(user).to_s).
|
||||
to match("/uploads/user/avatar/#{user.id}/banana_sample.gif")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'gravatar_icon' do
|
||||
|
|
Loading…
Reference in New Issue