Skip to content

Commit

Permalink
Merge pull request #6208 from Kerman07/improve-case-contacts-index-pe…
Browse files Browse the repository at this point in the history
…rformance-with-pagination

Improve performance of case contacts index action using pagination
  • Loading branch information
compwron authored Feb 4, 2025
2 parents a5cfe82 + bfc1d02 commit b130020
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ gem "net-pop" # needed for ruby upgrade to 3.1.0 https://www.ruby-lang.org/en/ne
gem "net-smtp", require: false # needed for ruby upgrade to 3.1.0 for some dang reason
gem "noticed" # Notifications
gem "oj" # faster JSON parsing 🍊
gem "pagy" # pagination
gem "paranoia" # For soft-deleting database objects
gem "pdf-forms" # filling in fund request PDFs with user input
gem "pg" # Use postgresql as the database for Active Record
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ GEM
ostruct (>= 0.2)
orm_adapter (0.5.0)
ostruct (0.6.1)
pagy (9.3.3)
parallel (1.26.3)
parallel_tests (4.9.0)
parallel
Expand Down Expand Up @@ -672,6 +673,7 @@ DEPENDENCIES
net-smtp
noticed
oj
pagy
parallel_tests
paranoia
pdf-forms
Expand Down
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ApplicationController < ActionController::Base
include Pundit::Authorization
include Pagy::Backend
include Organizational
include Users::TimeZone

Expand Down
11 changes: 6 additions & 5 deletions app/controllers/case_contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def index
}
) || return

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

@presenter = CaseContactPresenter.new(case_contacts)
Expand Down Expand Up @@ -103,12 +104,12 @@ def current_organization_groups
end

def all_case_contacts
policy_scope(current_organization.case_contacts).includes(
policy_scope(current_organization.case_contacts).preload(
:creator,
:followups,
:contact_types,
contact_topic_answers: [:contact_topic],
casa_case: [:volunteers]
contact_types: :contact_type_group,
contact_topic_answers: :contact_topic,
casa_case: :volunteers
)
end

Expand Down
2 changes: 2 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module ApplicationHelper
include Pagy::Frontend

def body_class
qualified_controller_name = controller.controller_path.tr("/", "-")
"#{qualified_controller_name} #{qualified_controller_name}-#{controller.action_name}"
Expand Down
2 changes: 2 additions & 0 deletions app/views/case_contacts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
</div>
</div>
<% end %>

<%= turbo_frame_tag :case_contacts do %>
<% @presenter.case_contacts.each do |casa_case_id, data| %>
<div class="card-style-1 mb-5">
Expand Down Expand Up @@ -156,4 +157,5 @@
</div>
<% end %>
<% end %>
<%== pagy_bootstrap_nav(@pagy) %>
<% end %>
5 changes: 5 additions & 0 deletions config/initializers/pagy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "pagy/extras/bootstrap"
require "pagy/extras/size"

Pagy::DEFAULT[:size] = [1, 3, 3, 1]
Pagy::DEFAULT[:nav_class] = "pagy-bootstrap-nav"
2 changes: 2 additions & 0 deletions spec/views/case_contacts/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
RSpec.describe "case_contacts/index", type: :view do
let(:user) { build_stubbed(:volunteer) }
let(:case_contacts) { CaseContact.all }
let(:pagy) { Pagy.new(count: 0) }

let(:filterrific_param_set) do
param_set = Filterrific::ParamSet.new(case_contacts, {})
Expand Down Expand Up @@ -30,6 +31,7 @@
assign(:current_organization_groups, groups)
assign(:filterrific, filterrific_param_set)
assign(:presenter, CaseContactPresenter.new(case_contacts))
assign(:pagy, pagy)

render template: "case_contacts/index"
end
Expand Down

0 comments on commit b130020

Please sign in to comment.