Improve performance for issue#show page

* store @participants in variable
* store result of subscribed? call into variable

In total it reduce amount of SQL queries for issue with 10 comments/participants twice.

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
Dmitriy Zaporozhets 2015-06-24 12:49:34 +02:00
parent c83bbfa79e
commit a5f23435f7
No known key found for this signature in database
GPG key ID: 161B5D6A44D3D88A
4 changed files with 13 additions and 9 deletions

View file

@ -55,6 +55,7 @@ class Projects::IssuesController < Projects::ApplicationController
end end
def show def show
@participants = @issue.participants(current_user, @project)
@note = @project.notes.new(noteable: @issue) @note = @project.notes.new(noteable: @issue)
@notes = @issue.notes.inc_author.fresh @notes = @issue.notes.inc_author.fresh
@noteable = @issue @noteable = @issue

View file

@ -14,7 +14,7 @@
# #
# participant :author, :assignee, :mentioned_users, :notes # participant :author, :assignee, :mentioned_users, :notes
# end # end
# #
# issue = Issue.last # issue = Issue.last
# users = issue.participants # users = issue.participants
# # `users` will contain the issue's author, its assignee, # # `users` will contain the issue's author, its assignee,
@ -35,11 +35,13 @@ module Participable
end end
end end
# Be aware that this method makes a lot of sql queries.
# Save result into variable if you are going to reuse it inside same request
def participants(current_user = self.author, project = self.project) def participants(current_user = self.author, project = self.project)
participants = self.class.participant_attrs.flat_map do |attr| participants = self.class.participant_attrs.flat_map do |attr|
meth = method(attr) meth = method(attr)
value = value =
if meth.arity == 1 || meth.arity == -1 if meth.arity == 1 || meth.arity == -1
meth.call(current_user) meth.call(current_user)
else else
@ -59,7 +61,7 @@ module Participable
end end
private private
def participants_for(value, current_user = nil, project = nil) def participants_for(value, current_user = nil, project = nil)
case value case value
when User when User

View file

@ -12,8 +12,8 @@
.votes-holder.pull-right .votes-holder.pull-right
#votes= render 'votes/votes_block', votable: @issue #votes= render 'votes/votes_block', votable: @issue
.participants .participants
%span= pluralize(@issue.participants(current_user).count, 'participant') %span= pluralize(@participants.count, 'participant')
- @issue.participants(current_user).each do |participant| - @participants.each do |participant|
= link_to_member(@project, participant, name: false, size: 24) = link_to_member(@project, participant, name: false, size: 24)
.voting_notes#notes= render 'projects/notes/notes_with_form' .voting_notes#notes= render 'projects/notes/notes_with_form'
%aside.col-md-3 %aside.col-md-3

View file

@ -28,18 +28,19 @@
= f.submit class: 'btn' = f.submit class: 'btn'
- if current_user - if current_user
- subscribed = @issue.subscribed?(current_user)
%div.prepend-top-20.clearfix %div.prepend-top-20.clearfix
.issuable-context-title .issuable-context-title
%label %label
Subscription: Subscription:
%button.btn.btn-block.subscribe-button{:type => 'button'} %button.btn.btn-block.subscribe-button{:type => 'button'}
%i.fa.fa-eye %i.fa.fa-eye
%span= @issue.subscribed?(current_user) ? "Unsubscribe" : "Subscribe" %span= subscribed ? "Unsubscribe" : "Subscribe"
- subscribtion_status = @issue.subscribed?(current_user) ? "subscribed" : "unsubscribed" - subscribtion_status = subscribed ? "subscribed" : "unsubscribed"
.subscription-status{"data-status" => subscribtion_status} .subscription-status{"data-status" => subscribtion_status}
.description-block.unsubscribed{class: ( "hidden" if @issue.subscribed?(current_user) )} .description-block.unsubscribed{class: ( "hidden" if subscribed )}
You're not receiving notifications from this thread. You're not receiving notifications from this thread.
.description-block.subscribed{class: ( "hidden" unless @issue.subscribed?(current_user) )} .description-block.subscribed{class: ( "hidden" unless subscribed )}
You're receiving notifications because you're subscribed to this thread. You're receiving notifications because you're subscribed to this thread.
:coffeescript :coffeescript