Merge branch 'group-icon-performance' into 'master'

Prevent database query each time we render group avatar

Fixes #2118

See merge request !1688
This commit is contained in:
Dmitriy Zaporozhets 2015-03-12 22:28:47 +00:00
commit 2fe69158f0
10 changed files with 39 additions and 33 deletions

View file

@ -86,15 +86,6 @@ module ApplicationHelper
end
end
def group_icon(group_path)
group = Group.find_by(path: group_path)
if group && group.avatar.present?
group.avatar.url
else
image_path('no_group_avatar.png')
end
end
def avatar_icon(user_email = '', size = nil)
user = User.find_by(email: user_email)

View file

@ -40,4 +40,16 @@ module GroupsHelper
false
end
end
def group_icon(group)
if group.is_a?(String)
group = Group.find_by(path: group)
end
if group && group.avatar.present?
group.avatar.url
else
image_path('no_group_avatar.png')
end
end
end

View file

@ -28,7 +28,7 @@ module NamespacesHelper
def namespace_icon(namespace, size = 40)
if namespace.kind_of?(Group)
group_icon(namespace.path)
group_icon(namespace)
else
avatar_icon(namespace.owner.email, size)
end

View file

@ -12,7 +12,7 @@
Group info:
%ul.well-list
%li
= image_tag group_icon(@group.path), class: "avatar s60"
= image_tag group_icon(@group), class: "avatar s60"
%li
%span.light Name:
%strong= @group.name

View file

@ -27,7 +27,7 @@
%i.fa.fa-sign-out
Leave
= image_tag group_icon(group.path), class: "avatar s40 avatar-tile"
= image_tag group_icon(group), class: "avatar s40 avatar-tile"
= link_to group, class: 'group-name' do
%strong= group.name

View file

@ -12,7 +12,7 @@
.form-group
.col-sm-2
.col-sm-10
= image_tag group_icon(@group.to_param), alt: '', class: 'avatar group-avatar s160'
= image_tag group_icon(@group), alt: '', class: 'avatar group-avatar s160'
%p.light
- if @group.avatar?
You can change your group avatar here

View file

@ -1,6 +1,6 @@
.dashboard
%div
= image_tag group_icon(@group.path), class: "avatar group-avatar s90"
= image_tag group_icon(@group), class: "avatar group-avatar s90"
.clearfix
%h2
= @group.name

View file

@ -1,4 +1,4 @@
.clearfix
- groups.each do |group|
= link_to group, class: 'profile-groups-avatars inline', title: group.name do
= image_tag group_icon(group.path), class: 'avatar group-avatar s40'
= image_tag group_icon(group), class: 'avatar group-avatar s40'

View file

@ -39,24 +39,6 @@ describe ApplicationHelper do
end
end
describe 'group_icon' do
avatar_file_path = File.join(Rails.root, 'public', 'gitlab_logo.png')
it 'should return an url for the avatar' do
group = create(:group)
group.avatar = File.open(avatar_file_path)
group.save!
expect(group_icon(group.path).to_s).
to match("/uploads/group/avatar/#{ group.id }/gitlab_logo.png")
end
it 'should give default avatar_icon when no avatar is present' do
group = create(:group)
group.save!
expect(group_icon(group.path)).to match('group_avatar.png')
end
end
describe 'project_icon' do
avatar_file_path = File.join(Rails.root, 'public', 'gitlab_logo.png')

View file

@ -0,0 +1,21 @@
require 'spec_helper'
describe GroupsHelper do
describe 'group_icon' do
avatar_file_path = File.join(Rails.root, 'public', 'gitlab_logo.png')
it 'should return an url for the avatar' do
group = create(:group)
group.avatar = File.open(avatar_file_path)
group.save!
expect(group_icon(group.path).to_s).
to match("/uploads/group/avatar/#{ group.id }/gitlab_logo.png")
end
it 'should give default avatar_icon when no avatar is present' do
group = create(:group)
group.save!
expect(group_icon(group.path)).to match('group_avatar.png')
end
end
end