Skip to content

Commit

Permalink
with_counts scope on Case actually saves the count of the queries and…
Browse files Browse the repository at this point in the history
… is faster!
  • Loading branch information
epugh committed Nov 28, 2023
1 parent e8b4aa6 commit 097c3d7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
7 changes: 3 additions & 4 deletions app/controllers/api/v1/cases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ def index
current_user.cases_involved_with.not_archived.includes(:metadata).references(:metadata)
.order(Arel.sql('`case_metadata`.`last_viewed_at` DESC, `cases`.`id`')).limit(3)
elsif sort_by
current_user.cases_involved_with.preload( :tries).not_archived.order(sort_by)
current_user.cases_involved_with.not_archived.with_counts.preload( :tries).order(sort_by)
else
current_user.cases_involved_with.preload(:tries, :teams,
:cases_teams)
.not_archived
current_user.cases_involved_with.not_archived.with_counts.preload(:tries, :teams,
:cases_teams)
.left_outer_joins(:metadata)
.order(Arel.sql('`case_metadata`.`last_viewed_at` DESC, `cases`.`updated_at` DESC'))
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
class HomeController < ApplicationController
def show
# @cases = @current_user.cases.not_archived.includes([ :scores ])
@cases = @current_user.cases_involved_with.not_archived
@cases = @current_user.cases_involved_with.not_archived.with_counts

@most_recent_cases = @current_user.cases_involved_with.not_archived.recent.limit(4).sort_by(&:case_name)
@most_recent_cases = @current_user.cases_involved_with.not_archived.recent.limit(4).with_counts.sort_by(&:case_name)

@most_recent_books = []
@lookup_for_books = {}
Expand Down
11 changes: 11 additions & 0 deletions app/models/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ class Case < ApplicationRecord
.order(Arel.sql('`case_metadata`.`last_viewed_at` DESC, `cases`.`updated_at` DESC'))
}

# load up the queries count for the case, alternative to counter_cache
scope :with_counts, -> {
select <<~SQL.squish
cases.*,
(
SELECT COUNT(queries.id) FROM queries
WHERE case_id = cases.id
) AS queries_count
SQL
}

# Not proud of this method, but it's the only way I can get the dependent
# objects of a Case to actually delete!
def really_destroy
Expand Down
13 changes: 7 additions & 6 deletions app/views/api/v1/cases/_case.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ json.case_id acase.id
json.scorer_id acase.scorer_id
json.book_id acase.book_id
json.owned acase.owner_id == current_user.id if current_user.present?
json.queries_count acase.queries.count
json.queries_count acase.respond_to?(:queries_count) ? acase.queries_count : acase.queries.count
unless shallow
json.owner_name acase.owner.name if acase.owner.present?
json.owner_id acase.owner.id if acase.owner.present?
Expand Down Expand Up @@ -44,12 +44,13 @@ unless shallow
end
end

# rubocop:disable Style/MultilineIfModifier
json.last_score do
json.partial! 'api/v1/case_scores/score', score: acase.last_score, shallow: shallow
end if acase.last_score.present?
# rubocop:enable Style/MultilineIfModifier

unless shallow
# rubocop:disable Style/MultilineIfModifier
json.last_score do
json.partial! 'api/v1/case_scores/score', score: acase.last_score, shallow: shallow
end if acase.last_score.present?
# rubocop:enable Style/MultilineIfModifier
json.scores acase.scores.sampled(acase.id, 10).includes(:annotation).limit(10) do |s|
json.score s.score
json.updated_at s.updated_at
Expand Down
4 changes: 1 addition & 3 deletions app/views/home/_case.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<tr>
<th scope="row"><%= kase.case_name %></th>
<td><%= kase.queries.count %></td>
<% if true == false %>
<td><%= kase.queries_count %></td>
<td>
<% unless kase.last_score.blank? %>
<%= kase.last_score.score %>
Expand All @@ -17,7 +16,6 @@
<%= kase.last_score.user.name%>
<% end %>
</td>
<% end %>

<td><%= link_to 'View', case_core_path(kase, kase.last_try_number), class: 'btn btn-sm btn-primary', role: 'button' %></td>
</tr>
20 changes: 20 additions & 0 deletions test/models/case_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,24 @@ class CaseTest < ActiveSupport::TestCase
end
end
end

describe 'with_counts scope' do
let(:the_case) { cases(:random_case) }

it 'handles query counts accurately' do
case_id = the_case.id
the_case = Case.with_counts.where(id: case_id).first
assert_equal 3, the_case.queries_count

assert_difference 'Query.count', -1 do
assert_equal 3, the_case.queries.size
the_case.queries.first.destroy
assert_equal 2, the_case.queries.size
end

# instead of a reload
the_case = Case.with_counts.where(id: case_id).first
assert_equal 2, the_case.queries_count
end
end
end

0 comments on commit 097c3d7

Please sign in to comment.