Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CTSKF-486] Replace Kaminari with Pagy #7915

Merged
merged 7 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,11 @@ Metrics/AbcSize:
- 'spec/support/seed_helpers.rb'
- 'spec/support/validation_helpers.rb'

# Offense count: 19
# Offense count: 20
# Configuration parameters: CountComments, Max, CountAsOne.
Metrics/ClassLength:
Exclude:
- 'app/controllers/case_workers/claims_controller.rb'
- 'app/controllers/external_users/claims_controller.rb'
- 'app/interfaces/api/entities/search_result.rb'
- 'app/interfaces/api/v1/dropdown_data.rb'
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ gem 'jquery-rails', '~> 4.6.0'
gem 'json-schema', '~> 5.1.1'
gem 'jsbundling-rails'
gem 'nokogiri', '~> 1.18'
gem 'kaminari', '>= 1.2.1'
gem 'libreconv', '~> 0.9.5'
gem 'logstasher', '2.1.5'
gem 'logstuff', '0.0.2'
gem 'net-imap'
gem 'net-pop'
gem 'net-smtp'
gem 'pagy'
gem 'paper_trail', '~> 16.0.0'
gem 'pg', '~> 1.5.9'
gem 'rails', '~> 7.0.8'
Expand Down
15 changes: 2 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,6 @@ GEM
jsonapi-renderer (0.2.2)
jwt (2.10.1)
base64
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
kaminari-activerecord (= 1.2.2)
kaminari-core (= 1.2.2)
kaminari-actionview (1.2.2)
actionview
kaminari-core (= 1.2.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
laa-fee-calculator-client (2.0.0)
faraday (~> 2.9)
faraday-http-cache (~> 2.2)
Expand Down Expand Up @@ -435,6 +423,7 @@ GEM
version_gem (~> 1.1)
orm_adapter (0.5.0)
ostruct (0.6.1)
pagy (9.3.3)
paper_trail (16.0.0)
activerecord (>= 6.1)
request_store (~> 1.4)
Expand Down Expand Up @@ -739,7 +728,6 @@ DEPENDENCIES
jsbundling-rails
json-schema (~> 5.1.1)
json_spec
kaminari (>= 1.2.1)
laa-cda!
laa-fee-calculator-client (~> 2.0)
launchy (~> 3.0.1)
Expand All @@ -754,6 +742,7 @@ DEPENDENCIES
net-smtp
net-ssh (~> 7.3)
nokogiri (~> 1.18)
pagy
paper_trail (~> 16.0.0)
parallel_tests
pg (~> 1.5.9)
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class ApplicationController < ActionController::Base
skip_forgery_protection if ENV.fetch('DISABLE_CSRF', nil) == '1'

include CookieConcern
include Pagy::Backend

before_action :set_default_cookie_usage

helper_method :current_user_messages_count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class AllocationsController < CaseWorkers::Admin::ApplicationController
before_action :set_claims, only: %i[new create]
before_action :set_summary_values, only: [:new], if: :summary_from_previous_request?
before_action :process_claim_ids, only: [:create], if: :quantity_allocation?
before_action :sort_and_paginate

def new
@allocation = Allocation.new
Expand Down Expand Up @@ -88,7 +89,13 @@ def search_claims(states = nil)
end

def sort_and_paginate
@claims = @claims.sort_using(sort_column, sort_direction).page(current_page).per(page_size)
@claims = @claims.sort_using(sort_column, sort_direction) unless @claims.remote?
@pagy = Pagy.new(
count: @claims.total_count,
limit: @claims.limit_value,
page: @claims.current_page,
pages: @claims.total_pages
)
end

def allocation_params
Expand Down
9 changes: 7 additions & 2 deletions app/controllers/case_workers/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,13 @@ def search_terms
end

def sort_and_paginate
# GOTCHA: must paginate in same call that sorts/orders
@claims = @claims.sort_using(sort_column, sort_direction).page(current_page).per(page_size) unless @claims.remote?
@claims = @claims.sort_using(sort_column, sort_direction) unless @claims.remote?
@pagy = Pagy.new(
count: @claims.total_count,
limit: @claims.limit_value,
page: @claims.current_page,
pages: @claims.total_pages
)
end

def sort_claims
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/pagination_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def page_size
end

def current_page
params[:page].to_i
[params[:page].to_i, 1].max
end
end
3 changes: 2 additions & 1 deletion app/controllers/external_users/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ def sort_defaults(defaults = {})

def sort_and_paginate(options = {})
sort_defaults(options)
@claims = @claims.sort_using(sort_column, sort_direction).page(current_page).per(@sort_defaults[:pagination])
@pagy, @claims = pagy(@claims.sort_using(sort_column, sort_direction), page: current_page,
limit: @sort_defaults[:pagination])
end

def scheme
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UsersController < ApplicationController
include PaginationHelpers

def index
@users = User.order(created_at: :desc).page(current_page).per(page_size)
@pagy, @users = pagy(User.order(created_at: :desc), page: current_page, limit: page_size)
end

def update_settings
Expand Down
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module ApplicationHelper
include GOVUKDesignSystemFormBuilder::BuilderHelper
include Pagy::Frontend

def current_user_is_caseworker?
current_user.persona.is_a?(CaseWorker)
Expand Down
12 changes: 8 additions & 4 deletions app/interfaces/api/entities/paginated_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ def items
object[:items]
end

def pagination
options[:pagy]
end

def current_page
items.current_page
pagination.page
end

def total_pages
items.total_pages
pagination.pages
end

def total_count
items.total_count
pagination.count
end

def limit_value
items.limit_value
pagination.limit
end
end
end
Expand Down
18 changes: 14 additions & 4 deletions app/interfaces/api/v2/case_workers/claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ def value_band_id
params[:value_band_id]
end

def page
params[:page]
end

def limit
params[:limit]
end

def current_claims
current_user.claims.where(id: ::Claim::BaseClaim.search(
search_terms, Claims::StateMachine::CASEWORKER_DASHBOARD_UNDER_ASSESSMENT_STATES, *search_options
Expand Down Expand Up @@ -82,19 +90,21 @@ def claims_scope
end

def claims
claims_scope
@pagy, @claims = pagy(claims_scope
.includes(:external_user, :case_type, :injection_attempts,
:case_workers, :court, :messages,
defendants: %i[representation_orders])
.sort_using(params[:sorting], params[:direction])
.page(params[:page]).per(params[:limit])
.sort_using(params[:sorting], params[:direction]),
page:, limit:)

@claims
end
end

resource :claims do
desc 'Retrieve list of allocated, unallocated or archived claims'
get do
present claims, with: API::Entities::PaginatedCollection, user: current_user
present claims, with: API::Entities::PaginatedCollection, user: current_user, pagy: @pagy
end
end
end
Expand Down
1 change: 1 addition & 0 deletions app/interfaces/api/v2/criteria_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module API::V2
module CriteriaHelper
extend Grape::API::Helpers
include Pagy::Backend

params :pagination do
optional :page, type: Integer, desc: 'OPTIONAL: Current page.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
.govuk-grid-column-one-half
= t('.re_allocation')
.govuk-grid-column-one-half.claim-count{ class: 'govuk-!-text-align-right' }
= t('.number_of_claims', claim_count: @claims.total_count)
= pagy_info(@pagy).html_safe

= govuk_table_thead do
= govuk_table_row do
Expand Down
2 changes: 1 addition & 1 deletion app/views/case_workers/admin/allocations/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
.js-re-allocation-page
= render partial: 're_allocation'

= paginate @claims
= render partial: 'shared/pagination'
2 changes: 1 addition & 1 deletion app/views/case_workers/claims/_claims_list.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.govuk-grid-row{class: 'govuk-!-margin-top-9'}
.govuk-grid-column-full
%p.govuk-body{class: 'claim-count govuk-!-text-align-right'}
= t('.number_of_claims', claim_count: claims.total_count)
= pagy_info(@pagy).html_safe

.govuk-grid-column-full
= govuk_table do
Expand Down
3 changes: 2 additions & 1 deletion app/views/case_workers/claims/archived.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
= render partial: 'layouts/header', locals: { page_heading: t('.page_heading')}
= render partial: 'search_form', locals: { archived: true }
= render partial: 'claims_list', locals: { claims: @claims }
= paginate @claims

= render partial: 'shared/pagination'
3 changes: 2 additions & 1 deletion app/views/case_workers/claims/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
= render partial: 'layouts/header', locals: { page_heading: t('.page_heading') }
= render partial: 'search_form', locals: { archived: false }
= render partial: 'claims_list', locals: { claims: @claims }
= paginate @claims

= render partial: 'shared/pagination'
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- if @claims.none?
= t('shared.no_claims_found')
- else
= "Showing #{pluralize(@claims.unscope(:order).size, 'claim')} of #{@claims.total_count}"
= pagy_info(@pagy).html_safe
- if params[:search].present?
= "matching #{params[:search]}"

Expand Down
2 changes: 1 addition & 1 deletion app/views/external_users/claims/archived.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

#details
= render partial: 'main_claims_list', locals: { claims: @claims }
= paginate @claims
= render partial: 'shared/pagination'
4 changes: 2 additions & 2 deletions app/views/external_users/claims/authorised.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

%p.govuk-heading-l
%span.govuk-caption-l
= t('.with_total_assessed_value_of_html', size: @claims.unscope(:order).size, total: @claims.total_count)
= t('.with_total_assessed_value_of_html', size: @claims.unscope(:order).size, total: @pagy.count)
= number_to_currency(@total_value)

#details#listanchor
= render partial: 'main_claims_list', locals: { claims: @claims }
= paginate @claims
= render partial: 'shared/pagination'
3 changes: 2 additions & 1 deletion app/views/external_users/claims/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
.importer-results{'aria-live': 'polite'}

= render partial: 'main_claims_list', locals: { claims: @claims }
= paginate @claims

= render partial: 'shared/pagination'
4 changes: 2 additions & 2 deletions app/views/external_users/claims/outstanding.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

%p.govuk-heading-l
%span.govuk-caption-l
= t('.claims_value_html', size: @claims.unscope(:order).size, total: @claims.total_count)
= t('.claims_value_html', size: @claims.unscope(:order).size, total: @pagy.count)
= number_to_currency(@total_value)

#details#listanchor
= render partial: 'main_claims_list', locals: { claims: @claims }
= paginate @claims
= render partial: 'shared/pagination'
9 changes: 0 additions & 9 deletions app/views/kaminari/_first_page.html.haml

This file was deleted.

8 changes: 0 additions & 8 deletions app/views/kaminari/_gap.html.haml

This file was deleted.

9 changes: 0 additions & 9 deletions app/views/kaminari/_last_page.html.haml

This file was deleted.

9 changes: 0 additions & 9 deletions app/views/kaminari/_next_page.html.haml

This file was deleted.

10 changes: 0 additions & 10 deletions app/views/kaminari/_page.html.haml

This file was deleted.

23 changes: 0 additions & 23 deletions app/views/kaminari/_paginator.html.haml

This file was deleted.

9 changes: 0 additions & 9 deletions app/views/kaminari/_prev_page.html.haml

This file was deleted.

Loading