Skip to content

Commit

Permalink
headers must be strings (closes ddnexus#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jan 9, 2020
1 parent 666700a commit 6a93b97
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/pagy/extras/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def pagy_headers_hash(pagy)
url_str = pagy_url_for(PAGE_PLACEHOLDER, pagy, :url)
hash = { 'Link' => Hash[rels.map{|rel, n|[rel, url_str.sub(PAGE_PLACEHOLDER, 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]
hash[headers[:page]] = pagy.page.to_s if headers[:page]
hash[headers[:items]] = pagy.vars[:items].to_s if headers[:items]
unless countless
hash[headers[:pages]] = pagy.pages if headers[:pages]
hash[headers[:count]] = pagy.count if headers[:count]
hash[headers[:pages]] = pagy.pages.to_s if headers[:pages]
hash[headers[:count]] = pagy.count.to_s if headers[:count]
end
hash
end
Expand Down
12 changes: 6 additions & 6 deletions test/pagy/extras/headers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@

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\"", "Current-Page"=>3, "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
pagy, _records = @controller.send(:pagy, @collection, headers:{items:'Per-Page', count: 'Total', pages:false})
_(@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\"", "Per-Page"=>20, "Total"=>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\"", "Per-Page"=>"20", "Total"=>"1000"})
end

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\"", "Current-Page"=>3, "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\"", "Current-Page"=>1, "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\"", "Current-Page"=>50, "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 @@ -51,7 +51,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\"", "Current-Page"=>3, "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 6a93b97

Please sign in to comment.