diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2015-10-12 16:58:09 +0200 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2015-10-15 12:05:01 +0200 |
commit | 693e63f5234032aa1abbc226c0d6337d6ea810ed (patch) | |
tree | e8ef1139615bb31156fdf44fffc43f9d08fe373d | |
parent | fb7785628a04f9facb0d05867cb5c4cafb646561 (diff) | |
download | gitlab-ce-693e63f5234032aa1abbc226c0d6337d6ea810ed.tar.gz |
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.
-rw-r--r-- | app/helpers/application_helper.rb | 10 | ||||
-rw-r--r-- | spec/helpers/application_helper_spec.rb | 9 |
2 files changed, 16 insertions, 3 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index cab2278adb7..8ecdeaf8e76 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 742420f550e..1dfae0fbd3f 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -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 |