Skip to content

Commit b130020

Browse files
authored
Merge pull request #6208 from Kerman07/improve-case-contacts-index-performance-with-pagination
Improve performance of case contacts index action using pagination
2 parents a5cfe82 + bfc1d02 commit b130020

File tree

8 files changed

+21
-5
lines changed

8 files changed

+21
-5
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ gem "net-pop" # needed for ruby upgrade to 3.1.0 https://www.ruby-lang.org/en/ne
3232
gem "net-smtp", require: false # needed for ruby upgrade to 3.1.0 for some dang reason
3333
gem "noticed" # Notifications
3434
gem "oj" # faster JSON parsing 🍊
35+
gem "pagy" # pagination
3536
gem "paranoia" # For soft-deleting database objects
3637
gem "pdf-forms" # filling in fund request PDFs with user input
3738
gem "pg" # Use postgresql as the database for Active Record

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ GEM
354354
ostruct (>= 0.2)
355355
orm_adapter (0.5.0)
356356
ostruct (0.6.1)
357+
pagy (9.3.3)
357358
parallel (1.26.3)
358359
parallel_tests (4.9.0)
359360
parallel
@@ -672,6 +673,7 @@ DEPENDENCIES
672673
net-smtp
673674
noticed
674675
oj
676+
pagy
675677
parallel_tests
676678
paranoia
677679
pdf-forms

app/controllers/application_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class ApplicationController < ActionController::Base
22
include Pundit::Authorization
3+
include Pagy::Backend
34
include Organizational
45
include Users::TimeZone
56

app/controllers/case_contacts_controller.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ def index
1919
}
2020
) || return
2121

22-
case_contacts = CaseContact.case_hash_from_cases(@filterrific.find)
22+
@pagy, @filtered_case_contacts = pagy(@filterrific.find)
23+
case_contacts = CaseContact.case_hash_from_cases(@filtered_case_contacts)
2324
case_contacts = case_contacts.select { |k, _v| k == params[:casa_case_id].to_i } if params[:casa_case_id].present?
2425

2526
@presenter = CaseContactPresenter.new(case_contacts)
@@ -103,12 +104,12 @@ def current_organization_groups
103104
end
104105

105106
def all_case_contacts
106-
policy_scope(current_organization.case_contacts).includes(
107+
policy_scope(current_organization.case_contacts).preload(
107108
:creator,
108109
:followups,
109-
:contact_types,
110-
contact_topic_answers: [:contact_topic],
111-
casa_case: [:volunteers]
110+
contact_types: :contact_type_group,
111+
contact_topic_answers: :contact_topic,
112+
casa_case: :volunteers
112113
)
113114
end
114115

app/helpers/application_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
module ApplicationHelper
2+
include Pagy::Frontend
3+
24
def body_class
35
qualified_controller_name = controller.controller_path.tr("/", "-")
46
"#{qualified_controller_name} #{qualified_controller_name}-#{controller.action_name}"

app/views/case_contacts/index.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
</div>
126126
</div>
127127
<% end %>
128+
128129
<%= turbo_frame_tag :case_contacts do %>
129130
<% @presenter.case_contacts.each do |casa_case_id, data| %>
130131
<div class="card-style-1 mb-5">
@@ -156,4 +157,5 @@
156157
</div>
157158
<% end %>
158159
<% end %>
160+
<%== pagy_bootstrap_nav(@pagy) %>
159161
<% end %>

config/initializers/pagy.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require "pagy/extras/bootstrap"
2+
require "pagy/extras/size"
3+
4+
Pagy::DEFAULT[:size] = [1, 3, 3, 1]
5+
Pagy::DEFAULT[:nav_class] = "pagy-bootstrap-nav"

spec/views/case_contacts/index.html.erb_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
RSpec.describe "case_contacts/index", type: :view do
44
let(:user) { build_stubbed(:volunteer) }
55
let(:case_contacts) { CaseContact.all }
6+
let(:pagy) { Pagy.new(count: 0) }
67

78
let(:filterrific_param_set) do
89
param_set = Filterrific::ParamSet.new(case_contacts, {})
@@ -30,6 +31,7 @@
3031
assign(:current_organization_groups, groups)
3132
assign(:filterrific, filterrific_param_set)
3233
assign(:presenter, CaseContactPresenter.new(case_contacts))
34+
assign(:pagy, pagy)
3335

3436
render template: "case_contacts/index"
3537
end

0 commit comments

Comments
 (0)