From 6085c8ce9d2eb144f12d5a712913ee3604b17dec Mon Sep 17 00:00:00 2001 From: Travis Miller Date: Mon, 9 Mar 2020 10:47:49 -0500 Subject: [PATCH] Add resource total count to pagination meta --- lib/jsonapi/pagination.rb | 2 ++ spec/pagination_spec.rb | 24 +++++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/jsonapi/pagination.rb b/lib/jsonapi/pagination.rb index c551066..b04eb66 100644 --- a/lib/jsonapi/pagination.rb +++ b/lib/jsonapi/pagination.rb @@ -82,6 +82,8 @@ def jsonapi_pagination_meta(resources) numbers[:last] = last_page end + numbers[:total] = total + numbers end diff --git a/spec/pagination_spec.rb b/spec/pagination_spec.rb index 879c883..e7c43e6 100644 --- a/spec/pagination_spec.rb +++ b/spec/pagination_spec.rb @@ -17,7 +17,7 @@ it do expect(response_json['data'].size).to eq(0) expect(response_json['meta']) - .to eq('many' => true, 'pagination' => { 'current' => 1 }) + .to eq('many' => true, 'pagination' => { 'current' => 1, 'total' => 0 }) end context 'with users' do @@ -68,7 +68,8 @@ 'first' => 1, 'prev' => 1, 'next' => 3, - 'last' => 3 + 'last' => 3, + 'total' => 3 ) end end @@ -83,8 +84,9 @@ 'first' => 1, 'prev' => 1, 'next' => 3, - 'last' => 3 - ) + 'last' => 3, + 'total' => 3 + ) expect(response_json).to have_link(:self) expect(response_json).to have_link(:prev) @@ -122,8 +124,9 @@ expect(response_json['meta']['pagination']).to eq( 'current' => 3, 'first' => 1, - 'prev' => 2 - ) + 'prev' => 2, + 'total' => 3 + ) expect(response_json).to have_link(:self) expect(response_json).to have_link(:prev) @@ -161,7 +164,8 @@ expect(response_json['meta']['pagination']).to eq( 'current' => 5, 'first' => 1, - 'prev' => 4 + 'prev' => 4, + 'total' => 3 ) end end @@ -173,7 +177,8 @@ expect(response_json['meta']['pagination']).to eq( 'current' => 5, 'first' => 1, - 'prev' => 4 + 'prev' => 4, + 'total' => 3 ) expect(response_json).to have_link(:self) @@ -209,7 +214,8 @@ expect(response_json['meta']['pagination']).to eq( 'current' => 1, 'next' => 2, - 'last' => 3 + 'last' => 3, + 'total' => 3 ) expect(response_json).not_to have_link(:prev)