Skip to content

Commit 9d9e637

Browse files
committed
feat(index): add pagination controls to the bottom of the page
1 parent b1dac2b commit 9d9e637

File tree

15 files changed

+1286
-48
lines changed

15 files changed

+1286
-48
lines changed

lib/db.rb

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def self.connect db_credentials
2929
# logger = Logger.new($stdout)
3030
con = Sequel.connect(db_credentials.merge(:logger => logger, :pool_class => Sequel::ThreadedConnectionPool, :encoding => 'utf8'))
3131
con.extension(:connection_validator)
32+
con.extension(:pagination)
3233
con.pool.connection_validation_timeout = -1 #Check the connection on every request
3334
con.timezone = :utc
3435
con.run("SET sql_mode='STRICT_TRANS_TABLES';") if db_credentials[:adapter].to_s =~ /mysql/

lib/pact_broker/app.rb

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def configure_database_connection
110110
PactBroker::DB.connection.timezone = :utc
111111
PactBroker::DB.validate_connection_config if configuration.validate_database_connection_config
112112
PactBroker::DB.set_mysql_strict_mode_if_mysql
113+
PactBroker::DB.connection.extension(:pagination)
113114
Sequel.datetime_class = DateTime
114115
Sequel.database_timezone = :utc # Store all dates in UTC, assume any date without a TZ is UTC
115116
Sequel.application_timezone = :local # Convert dates to localtime when retrieving from database

lib/pact_broker/index/page.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module PactBroker
2+
module Index
3+
class Page < Array
4+
attr_reader :pagination_record_count
5+
6+
def initialize(array, pagination_record_count)
7+
super(array)
8+
@pagination_record_count = pagination_record_count
9+
end
10+
end
11+
end
12+
end

lib/pact_broker/index/service.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'pact_broker/matrix/head_row'
55
require 'pact_broker/matrix/aggregated_row'
66
require 'pact_broker/repositories/helpers'
7+
require 'pact_broker/index/page'
78

89
module PactBroker
910
module Index
@@ -20,6 +21,8 @@ class Service
2021
Sequel.desc(:consumer_version_order),
2122
Sequel.asc(Sequel.function(:lower, :provider_name))
2223
].freeze
24+
DEFAULT_PAGE_SIZE = 30
25+
DEFAULT_PAGE_NUMBER = 1
2326

2427
# This method provides data for both the OSS server side rendered index (with and without tags)
2528
# and the Pactflow UI. It really needs to be broken into to separate methods, as it's getting too messy
@@ -77,6 +80,7 @@ def self.find_index_items_optimised options = {}
7780
webhooks = PactBroker::Webhooks::Webhook.select(:consumer_id, :provider_id).distinct.all
7881

7982
pact_publication_ids = head_pact_publication_ids(options)
83+
pagination_record_count = pact_publication_ids.pagination_record_count
8084

8185
pact_publications = PactBroker::Pacts::PactPublication
8286
.where(id: pact_publication_ids)
@@ -89,7 +93,7 @@ def self.find_index_items_optimised options = {}
8993
.eager(latest_verification: { provider_version: :tags_with_latest_flag })
9094
.eager(:head_pact_tags)
9195

92-
pact_publications.all.collect do | pact_publication |
96+
index_items = pact_publications.all.collect do | pact_publication |
9397
is_overall_latest_for_integration = latest_pact_publication_ids.include?(pact_publication.id)
9498
latest_verification = latest_verification_for_pseudo_branch(pact_publication, is_overall_latest_for_integration, latest_verifications_for_cv_tags, options[:tags])
9599
webhook = webhooks.find{ |webhook| webhook.is_for?(pact_publication.integration) }
@@ -106,6 +110,8 @@ def self.find_index_items_optimised options = {}
106110
options[:tags] && latest_verification ? latest_verification.provider_version.tags_with_latest_flag.select(&:latest?) : []
107111
)
108112
end.sort
113+
114+
Page.new(index_items, pagination_record_count)
109115
end
110116

111117
# Worst. Code. Ever.
@@ -193,8 +199,7 @@ def self.head_pact_publication_ids(options = {})
193199
end
194200

195201
query.order(*HEAD_PP_ORDER_COLUMNS)
196-
.limit(options[:limit] || 50)
197-
.offset(options[:offset] || 0)
202+
.paginate(options[:page_number] || DEFAULT_PAGE_NUMBER, options[:page_size] || DEFAULT_PAGE_SIZE)
198203
.select(:id)
199204
end
200205

lib/pact_broker/ui/controllers/index.rb

+19-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,27 @@ class Index < Base
1414
if params[:tags]
1515
tags = params[:tags] == 'true' ? true : [*params[:tags]].compact
1616
end
17-
options = { tags: tags, limit: params[:limit]&.to_i, offset: params[:offset]&.to_i }
17+
page_number = params[:page]&.to_i || 1
18+
page_size = params[:pageSize]&.to_i || 30
19+
options = {
20+
tags: tags,
21+
page_number: page_number,
22+
page_size: page_size
23+
}
24+
1825
options[:optimised] = true if params[:optimised] == 'true'
19-
view_model = ViewDomain::IndexItems.new(index_service.find_index_items(options))
26+
index_items = ViewDomain::IndexItems.new(index_service.find_index_items(options))
27+
2028
page = tags ? :'index/show-with-tags' : :'index/show'
21-
haml page, {locals: {index_items: view_model, title: "Pacts"}, layout: :'layouts/main'}
29+
locals = {
30+
index_items: index_items, title: "Pacts",
31+
page_number: page_number,
32+
page_size: page_size,
33+
pagination_record_count: index_items.pagination_record_count,
34+
current_page_size: index_items.size
35+
}
36+
37+
haml page, {locals: locals, layout: :'layouts/main'}
2238
end
2339

2440
def set_headers

lib/pact_broker/ui/view_models/index_items.rb

+10-8
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@ module UI
55
module ViewDomain
66
class IndexItems
77

8+
attr_reader :pagination_record_count
9+
810
def initialize index_items
11+
# Why are we sorting twice!?
912
@index_items = index_items.collect{ |index_item| IndexItem.new(index_item) }.sort
13+
# until the feature flag is turned on
14+
@pagination_record_count = index_items.size
15+
@pagination_record_count = index_items.pagination_record_count if index_items.respond_to?(:pagination_record_count)
1016
end
1117

1218
def each(&block)
1319
index_items.each(&block)
1420
end
1521

16-
def size_label
17-
case index_items.size
18-
when 1 then "1 pact"
19-
else
20-
"#{index_items.size} pacts"
21-
end
22-
end
23-
2422
def empty?
2523
index_items.empty?
2624
end
2725

26+
def size
27+
index_items.size
28+
end
29+
2830
private
2931

3032
attr_reader :index_items
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
%script{type: 'text/javascript', src:'/javascripts/pagination.js'}
2+
3+
:javascript
4+
const PAGE_NUMBER = #{page_number};
5+
const PAGE_SIZE = #{page_size};
6+
const TOTAL_NUMBER = #{pagination_record_count}
7+
const CURRENT_PAGE_SIZE = #{current_page_size}
8+
9+
$(document).ready(function(){
10+
function createPageLink(pageNumber, pageSize) {
11+
const url = new URL(window.location)
12+
url.searchParams.set('page', pageNumber)
13+
url.searchParams.set('pageSize', pageSize)
14+
return url.toString()
15+
}
16+
17+
function createFooter(currentPage, totalPage, totalNumber) {
18+
return `<div class='nav-footer'>${CURRENT_PAGE_SIZE} of ${totalNumber} pacts</div>`
19+
}
20+
21+
$('div.pagination').pagination({
22+
dataSource: [],
23+
totalNumber: TOTAL_NUMBER,
24+
pageNumber: PAGE_NUMBER,
25+
pageSize: PAGE_SIZE,
26+
pageRange: 2,
27+
pageLink: createPageLink,
28+
ulClassName: 'pagination',
29+
footer: createFooter
30+
})
31+
});

lib/pact_broker/ui/views/index/show-with-tags.haml

+5-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@
8282
%td
8383
- if index_item.show_settings?
8484
%span.integration-settings.glyphicon.glyphicon-option-horizontal{ 'aria-hidden': true }
85-
%div.relationships-size
86-
= index_items.size_label
85+
86+
%div.pagination
87+
88+
- pagination_locals = { page_number: page_number, page_size: page_size, pagination_record_count: pagination_record_count, current_page_size: current_page_size }
89+
= render :haml, :'index/_pagination', :layout => false, locals: pagination_locals
8790
8891
:javascript
8992
$(function(){

lib/pact_broker/ui/views/index/show.haml

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@
5555
%span.glyphicon.glyphicon-warning-sign{ 'aria-hidden': true }
5656
%td
5757
%span.integration-settings.glyphicon.glyphicon-option-horizontal{ 'aria-hidden': true }
58-
%div.relationships-size
59-
= index_items.size_label
58+
%div.pagination
59+
60+
- pagination_locals = { page_number: page_number, page_size: page_size, pagination_record_count: pagination_record_count, current_page_size: current_page_size }
61+
= render :haml, :'index/_pagination', :layout => false, locals: pagination_locals
62+
6063
6164
:javascript
6265
$(function(){

0 commit comments

Comments
 (0)