Skip to content

Commit

Permalink
added Current-Page default header to headers extra
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Mar 15, 2019
1 parent 4086181 commit 016b377
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
16 changes: 8 additions & 8 deletions docs/extras/headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ pagy_render(Product.all)
## Headers

This extra generates the standard `Link` header defined in the
[RFC-8288](https://tools.ietf.org/html/rfc8288), and adds 3 customizable headers useful for pagination: `Page-Items`, `Total-Pages` and `Total-Count` headers.
[RFC-8288](https://tools.ietf.org/html/rfc8288), and adds 4 customizable headers useful for pagination: `Current-Page`, `Page-Items`, `Total-Pages` and `Total-Count` headers.

Example of the default HTTP headers produced:

```
Link <https://example.com:8080/foo?page=1>; rel="first", <https://example.com:8080/foo?page=2>; rel="prev", <https://example.com:8080/foo?page=4>; rel="next", <https://example.com:8080/foo?page=50>; rel="last"
Current-Page 3
Page-Items 20
Total-Pages 50
Total-Count 1000
Expand All @@ -89,25 +90,26 @@ Example of HTTP headers produced from a `Pagy::Countless` object:

```
Link <https://example.com:8080/foo?page=1>; rel="first", <https://example.com:8080/foo?page=2>; rel="prev", <https://example.com:8080/foo?page=4>; rel="next"
Current-Page 3
Page-Items 20
```

## Variables

| Variable | Description | Default |
|:-----------|:---------------------------------------------------------|:----------------------------------------------------------------------|
| `:headers` | Hash variable to customize/suppress the optional headers | `{ items: 'Page-Items', count: 'Total-Count', pages: 'Total-Pages' }` |
| Variable | Description | Default |
|:-----------|:---------------------------------------------------------|:--------------------------------------------------------------------------------------------|
| `:headers` | Hash variable to customize/suppress the optional headers | `{ page: 'Current-Page', items: 'Page-Items', count: 'Total-Count', pages: 'Total-Pages' }` |

As usual, depending on the scope of the customization, you can set the variables globally or for a single pagy instance.

For example, the following will change the header names and will suppres the `:pages` ('Total-Pages') header:

```ruby
# globally
Pagy::VARS[:headers] = {items: 'Per-Page', pages: false, count: 'Total'}
Pagy::VARS[:headers] = {page: 'Current-Page', items: 'Per-Page', pages: false, count: 'Total'}

# or for single instance
pagy, records = pagy(collection, headers: {items: 'Per-Page', pages: false, count: 'Total'})
pagy, records = pagy(collection, headers: {page: 'Current-Page', items: 'Per-Page', pages: false, count: 'Total'})
```

## Methods
Expand All @@ -133,5 +135,3 @@ This method generates a hash structure of the headers, useful if you want to inc
```ruby
render json: records.as_json.merge!(meta: {pagination: pagy_headers_hash(pagy)})
```


2 changes: 1 addition & 1 deletion lib/config/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
# Headers extra: http response headers (and other helpers) useful for API pagination
# See http://ddnexus.github.io/pagy/extras/headers
# require 'pagy/extras/headers'
# Pagy::VARS[:headers] = { items: 'Page-Items', count: 'Total-Count', pages: 'Total-Pages' } # default
# Pagy::VARS[:headers] = { page: 'Current-Page', items: 'Page-Items', count: 'Total-Count', pages: 'Total-Pages' } # default

# Support extra: Extra support for features like: incremental, infinite, auto-scroll pagination
# See https://ddnexus.github.io/pagy/extras/support
Expand Down
3 changes: 2 additions & 1 deletion lib/pagy/extras/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Pagy
# Add specialized backend methods to add pagination response headers
module Backend ; private

VARS[:headers] = { items: 'Page-Items', count: 'Total-Count', pages: 'Total-Pages' }
VARS[:headers] = { page: 'Current-Page', items: 'Page-Items', count: 'Total-Count', pages: 'Total-Pages' }

include Helpers

Expand All @@ -26,6 +26,7 @@ def pagy_headers_hash(pagy)
url_str = pagy_url_for(Frontend::MARKER, pagy, :url)
hash = { 'Link' => Hash[rels.map{|rel, n|[rel, url_str.sub(Frontend::MARKER, n.to_s)] if n}.compact] }
headers = pagy.vars[:headers]
hash[headers[:page]] = pagy.page if headers[:page]
hash[headers[:items]] = pagy.vars[:items] if headers[:items]
unless countless
hash[headers[:pages]] = pagy.pages if headers[:pages]
Expand Down
10 changes: 5 additions & 5 deletions test/pagy/extras/headers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

it 'returns the full headers hash' do
pagy, _records = @controller.send(:pagy, @collection)
@controller.send(:pagy_headers, pagy).must_equal({"Link"=>"<https://example.com:8080/foo?page=1>; rel=\"first\", <https://example.com:8080/foo?page=2>; rel=\"prev\", <https://example.com:8080/foo?page=4>; rel=\"next\", <https://example.com:8080/foo?page=50>; rel=\"last\"", "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000})
@controller.send(:pagy_headers, pagy).must_equal({"Link"=>"<https://example.com:8080/foo?page=1>; rel=\"first\", <https://example.com:8080/foo?page=2>; rel=\"prev\", <https://example.com:8080/foo?page=4>; rel=\"next\", <https://example.com:8080/foo?page=50>; rel=\"last\"", "Current-Page"=>3, "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000})
end

it 'returns custom headers hash' do
Expand All @@ -28,17 +28,17 @@

it 'returns the countless headers hash' do
pagy, _records = @controller.send(:pagy_countless, @collection)
@controller.send(:pagy_headers, pagy).must_equal({"Link"=>"<https://example.com:8080/foo?page=1>; rel=\"first\", <https://example.com:8080/foo?page=2>; rel=\"prev\", <https://example.com:8080/foo?page=4>; rel=\"next\"", "Page-Items"=>20})
@controller.send(:pagy_headers, pagy).must_equal({"Link"=>"<https://example.com:8080/foo?page=1>; rel=\"first\", <https://example.com:8080/foo?page=2>; rel=\"prev\", <https://example.com:8080/foo?page=4>; rel=\"next\"", "Current-Page"=>3, "Page-Items"=>20})
end

it 'omit prev on first page' do
pagy, _records = @controller.send(:pagy, @collection, page: 1)
@controller.send(:pagy_headers, pagy).must_equal({"Link"=>"<https://example.com:8080/foo?page=1>; rel=\"first\", <https://example.com:8080/foo?page=2>; rel=\"next\", <https://example.com:8080/foo?page=50>; rel=\"last\"", "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000})
@controller.send(:pagy_headers, pagy).must_equal({"Link"=>"<https://example.com:8080/foo?page=1>; rel=\"first\", <https://example.com:8080/foo?page=2>; rel=\"next\", <https://example.com:8080/foo?page=50>; rel=\"last\"", "Current-Page"=>1, "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000})
end

it 'omit next on last page' do
pagy, _records = @controller.send(:pagy, @collection, page: 50)
@controller.send(:pagy_headers, pagy).must_equal({"Link"=>"<https://example.com:8080/foo?page=1>; rel=\"first\", <https://example.com:8080/foo?page=49>; rel=\"prev\", <https://example.com:8080/foo?page=50>; rel=\"last\"", "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000})
@controller.send(:pagy_headers, pagy).must_equal({"Link"=>"<https://example.com:8080/foo?page=1>; rel=\"first\", <https://example.com:8080/foo?page=49>; rel=\"prev\", <https://example.com:8080/foo?page=50>; rel=\"last\"", "Current-Page"=>50, "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000})
end

end
Expand All @@ -53,7 +53,7 @@
it 'returns the full headers hash' do
pagy, _records = @controller.send(:pagy, @collection)
@controller.send(:pagy_headers_merge, pagy)
@controller.send(:response).headers.must_equal({"Link"=>"<https://example.com:8080/foo?page=1>; rel=\"first\", <https://example.com:8080/foo?page=2>; rel=\"prev\", <https://example.com:8080/foo?page=4>; rel=\"next\", <https://example.com:8080/foo?page=50>; rel=\"last\"", "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000})
@controller.send(:response).headers.must_equal({"Link"=>"<https://example.com:8080/foo?page=1>; rel=\"first\", <https://example.com:8080/foo?page=2>; rel=\"prev\", <https://example.com:8080/foo?page=4>; rel=\"next\", <https://example.com:8080/foo?page=50>; rel=\"last\"", "Current-Page"=>3, "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000})
end

end
Expand Down

0 comments on commit 016b377

Please sign in to comment.