Skip to content

Commit 712310c

Browse files
Merge pull request #26 from infinum/feature/pagy-countless-paginator
Add Pagy Countless paginator
2 parents 9b979e7 + dc1072d commit 712310c

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Or install it yourself as:
2727

2828
```ruby
2929
class UserQuery < Jsonapi::QueryBuilder::BaseQuery
30-
## pagination
30+
## pagination
3131
paginator Jsonapi::QueryBuilder::Paginator::Pagy # default paginator
3232

3333
## sorting
@@ -62,9 +62,9 @@ pass the scoped collection to the `Jsonapi::QueryBuilder::BaseQuery` object.
6262
### Pagination
6363

6464
Pagination support is configurable using the `paginator` method to define the paginator. It defaults to the `Pagy`
65-
paginator, a lightweight and fast paginator. Other paginators currently supported are `Kaminari` and an implementation
66-
of keyset pagination. Before using these paginators we need to explicitly require the gems in our Gemfile and the
67-
paginator file in question. Additionally one can implement it's own paginator by inheriting
65+
paginator, a lightweight and fast paginator. Other paginators currently supported are `Kaminari`, `PagyCountless` and
66+
an implementation of keyset pagination. Before using these paginators we need to explicitly require the gems in our
67+
Gemfile and the paginator file in question. Additionally one can implement it's own paginator by inheriting
6868
from `Jsonapi::QueryBuilder::Paginator::BasePaginator`. The minimum required implementation is a `#paginate` method that
6969
receives page params and returns a page of the collection. It can return the pagination details as the second item of
7070
the returned array, that can be used in the serializer for pagination metadata.
@@ -85,6 +85,14 @@ require "jsonapi/query_builder/paginator/keyset"
8585
paginator Jsonapi::QueryBuilder::Paginator::Keyset
8686
```
8787

88+
#### Using the Pagy Countless Paginator
89+
90+
```ruby
91+
require "jsonapi/query_builder/paginator/pagy_countless"
92+
93+
paginator Jsonapi::QueryBuilder::Paginator::PagyCountless
94+
```
95+
8896
### Sorting
8997

9098
#### Ensuring deterministic results
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
require "pagy"
4+
require "pagy/extras/items"
5+
require "pagy/extras/countless"
6+
7+
module Jsonapi
8+
module QueryBuilder
9+
module Paginator
10+
class PagyCountless < BasePaginator
11+
include ::Pagy::Backend
12+
13+
def paginate(page_params)
14+
@params = {page: page_params}
15+
16+
pagination_details, records = pagy_countless(collection, page: page_params[:number],
17+
items: page_params[:size],
18+
outset: page_params[:offset])
19+
20+
[records, pagination_details]
21+
end
22+
23+
private
24+
25+
attr_reader :params
26+
end
27+
end
28+
end
29+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require "jsonapi/query_builder/paginator/pagy_countless"
4+
5+
RSpec.describe Jsonapi::QueryBuilder::Paginator::PagyCountless do
6+
describe "#paginate" do
7+
subject(:paginate) { pagy_paginator.paginate(number: 2, size: 20, offset: 3) }
8+
9+
let(:pagy_paginator) { described_class.new(collection) }
10+
let(:collection) { instance_double "collection" }
11+
let(:paged_collection) { instance_double "paged-collection" }
12+
let(:pagination_details) { instance_double Pagy, "pagination-details" }
13+
14+
before do
15+
allow(pagy_paginator).to receive(:pagy_countless).and_return([pagination_details, paged_collection])
16+
end
17+
18+
it { is_expected.to be_an Array }
19+
20+
it "returns the records and pagination details" do
21+
expect(paginate).to eql [paged_collection, pagination_details]
22+
end
23+
24+
it "passes the collection and pagination params to pagy_countless method" do
25+
paginate
26+
27+
expect(pagy_paginator).to have_received(:pagy_countless).with(collection, page: 2, items: 20, outset: 3)
28+
end
29+
30+
it "sets the private params attribute reader used internally by pagy backend" do
31+
paginate
32+
33+
expect(pagy_paginator.send(:params)).to eql page: {number: 2, size: 20, offset: 3}
34+
end
35+
end
36+
end

0 commit comments

Comments
 (0)