diff --git a/docs/extras/headers.md b/docs/extras/headers.md index 98a844756..2f192b37b 100644 --- a/docs/extras/headers.md +++ b/docs/extras/headers.md @@ -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 ; rel="first", ; rel="prev", ; rel="next", ; rel="last" +Current-Page 3 Page-Items 20 Total-Pages 50 Total-Count 1000 @@ -89,14 +90,15 @@ Example of HTTP headers produced from a `Pagy::Countless` object: ``` Link ; rel="first", ; rel="prev", ; 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. @@ -104,10 +106,10 @@ For example, the following will change the header names and will suppres the `:p ```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 @@ -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)}) ``` - - diff --git a/lib/config/pagy.rb b/lib/config/pagy.rb index 52ba66660..b0313ce8d 100644 --- a/lib/config/pagy.rb +++ b/lib/config/pagy.rb @@ -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 diff --git a/lib/pagy/extras/headers.rb b/lib/pagy/extras/headers.rb index 1e0bd1954..afa4ac658 100644 --- a/lib/pagy/extras/headers.rb +++ b/lib/pagy/extras/headers.rb @@ -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 @@ -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] diff --git a/test/pagy/extras/headers_test.rb b/test/pagy/extras/headers_test.rb index 930ee9eb7..6773b057f 100644 --- a/test/pagy/extras/headers_test.rb +++ b/test/pagy/extras/headers_test.rb @@ -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"=>"; rel=\"first\", ; rel=\"prev\", ; rel=\"next\", ; rel=\"last\"", "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000}) + @controller.send(:pagy_headers, pagy).must_equal({"Link"=>"; rel=\"first\", ; rel=\"prev\", ; rel=\"next\", ; rel=\"last\"", "Current-Page"=>3, "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000}) end it 'returns custom headers hash' do @@ -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"=>"; rel=\"first\", ; rel=\"prev\", ; rel=\"next\"", "Page-Items"=>20}) + @controller.send(:pagy_headers, pagy).must_equal({"Link"=>"; rel=\"first\", ; rel=\"prev\", ; 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"=>"; rel=\"first\", ; rel=\"next\", ; rel=\"last\"", "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000}) + @controller.send(:pagy_headers, pagy).must_equal({"Link"=>"; rel=\"first\", ; rel=\"next\", ; 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"=>"; rel=\"first\", ; rel=\"prev\", ; rel=\"last\"", "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000}) + @controller.send(:pagy_headers, pagy).must_equal({"Link"=>"; rel=\"first\", ; rel=\"prev\", ; rel=\"last\"", "Current-Page"=>50, "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000}) end end @@ -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"=>"; rel=\"first\", ; rel=\"prev\", ; rel=\"next\", ; rel=\"last\"", "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000}) + @controller.send(:response).headers.must_equal({"Link"=>"; rel=\"first\", ; rel=\"prev\", ; rel=\"next\", ; rel=\"last\"", "Current-Page"=>3, "Page-Items"=>20, "Total-Pages"=>50, "Total-Count"=>1000}) end end