Skip to content

Commit

Permalink
Merge pull request #98 from mooreniemi/96-OptimizingQueries
Browse files Browse the repository at this point in the history
96 - first pass of fixing a few more n+1s
  • Loading branch information
mooreniemi authored Feb 24, 2021
2 parents 4f680f1 + 582bb78 commit 678aba3
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/controllers/procedures_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class ProceduresController < ApplicationController
def index
@procedures = Procedure.all.order(:name)
@pins_per_procedure = Procedure.joins(:pins).group("pins.procedure_id").count
@comments_per_procedure = Procedure.joins(:comment_threads).group("comments.commentable_id").count
end

def show
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/surgeons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def index

def show
@surgeon = Surgeon.friendly.find(params[:id])
# produces {[surgeon_id, procedure_id] => count }
@pins_by_surgeon_procedure = Pin.group(:surgeon_id, :procedure_id).count
end

def new
Expand Down
2 changes: 1 addition & 1 deletion app/models/surgeon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def overall_satisfaction
end

def satisfaction_by_procedure
Pin.where(surgeon_id: self.id).where.not(satisfaction: 0).
Pin.where(surgeon_id: id).where.not(satisfaction: 0).
group([:procedure_id]).average(:satisfaction)
end

Expand Down
4 changes: 2 additions & 2 deletions app/views/procedures/_procedure.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<tr itemscope itemtype="https://schema.org/MedicalProcedure">
<td><%= link_to procedure.name, procedure_path(procedure) %></td>
<% if user_signed_in? %>
<td><%= link_to Pin.where(procedure_id: procedure.id).count, pins_path(procedure: procedure.id) %></td>
<td><%= link_to @pins_per_procedure[procedure.id] || 0, pins_path(procedure: procedure.id) %></td>
<% else %>
<td><%= link_to "Register to see more", register_path %></td>
<% end %>
<td><%= procedure.avg_satisfaction %></td>
<td><%= procedure.avg_sensation %></td>
<td><%= procedure.comment_threads.count %></td>
<td><%= @comments_per_procedure[procedure.id] || 0 %></td>
</tr>
6 changes: 4 additions & 2 deletions app/views/procedures/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<% title @procedure.to_s %>
<% cache(@procedure) do %>
<div class="panel panel-default" itemscope itemtype="https://schema.org/MedicalProcedure">
<div class="panel-heading">
<%= @procedure.name %>
<%= link_to Pin.where(procedure_id: @procedure.id).count, pins_path(procedure: @procedure.id), { style: 'border-radius: 100%; background: #ffffcc; display:inline-block; padding:5px;' } %>
<h2><%= @procedure.name %></h2>
<%= link_to "See all submissions", pins_path(procedure: @procedure.id) %>
</div>
<% if @procedure.body_type %>
<div class="panel-body" itemprop="bodyLocation">
Expand All @@ -24,6 +25,7 @@
</li>
</ul>
</div>
<% end %>
<% if current_user %>
<h4><%= fa_icon("comments-o") %> discussion</h4>
Expand Down
2 changes: 1 addition & 1 deletion app/views/surgeons/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><%= Procedure.find(procedure_id).name.to_s %>
<%= link_to Pin.where(surgeon_id: @surgeon.id).where(procedure_id: procedure_id).count, pins_path(procedure: procedure_id, surgeon: @surgeon.id), { style: 'border-radius: 100%; background: #ffffcc; display:inline-block; padding:5px;' } %>
<%= link_to @pins_by_surgeon_procedure[[@surgeon.id, procedure_id]], pins_path(procedure: procedure_id, surgeon: @surgeon.id), { style: 'border-radius: 100%; background: #ffffcc; display:inline-block; padding:5px;' } %>
</h3>
</div>
<div class="panel-body">
Expand Down
3 changes: 3 additions & 0 deletions spec/views/surgeons/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
shared_examples "surgeon" do
it "displays the surgeon" do
assign(:surgeon, surgeon)
assign(:pins_by_surgeon_procedure, {})

render

Expand All @@ -24,10 +25,12 @@

it "shows the relevant procedures" do
assign(:surgeon, surgeon)
assign(:pins_by_surgeon_procedure, { [surgeon.id, pin.procedure_id] => 1})

render

expect(rendered).to match(Regexp.new(pin.procedure.name))
expect(rendered).to match(Regexp.new("Average patient satisfaction"))
end
end
end

0 comments on commit 678aba3

Please sign in to comment.