From 3558376db25612d1851a2f3eb249df81656e0061 Mon Sep 17 00:00:00 2001 From: Finn Lawrence Date: Wed, 4 Nov 2020 19:57:21 +1300 Subject: [PATCH 1/2] Add total records to pagination --- lib/jsonapi/pagination.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/jsonapi/pagination.rb b/lib/jsonapi/pagination.rb index 4d267cf..178dd8c 100644 --- a/lib/jsonapi/pagination.rb +++ b/lib/jsonapi/pagination.rb @@ -82,6 +82,10 @@ def jsonapi_pagination_meta(resources) numbers[:last] = last_page end + if total.present? + numbers[:records] = total + end + numbers end From 81a0862aef53e927cc2d7122ccd20b8aa16cf621 Mon Sep 17 00:00:00 2001 From: Finn Lawrence Date: Wed, 4 Nov 2020 20:11:29 +1300 Subject: [PATCH 2/2] Spec for total records --- spec/pagination_spec.rb | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/spec/pagination_spec.rb b/spec/pagination_spec.rb index 879c883..ec1c3c4 100644 --- a/spec/pagination_spec.rb +++ b/spec/pagination_spec.rb @@ -17,7 +17,13 @@ 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, + 'records' => 0 + } + ) end context 'with users' do @@ -68,7 +74,8 @@ 'first' => 1, 'prev' => 1, 'next' => 3, - 'last' => 3 + 'last' => 3, + 'records' => 3 ) end end @@ -83,7 +90,8 @@ 'first' => 1, 'prev' => 1, 'next' => 3, - 'last' => 3 + 'last' => 3, + 'records' => 3 ) expect(response_json).to have_link(:self) @@ -122,7 +130,8 @@ expect(response_json['meta']['pagination']).to eq( 'current' => 3, 'first' => 1, - 'prev' => 2 + 'prev' => 2, + 'records' => 3 ) expect(response_json).to have_link(:self) @@ -161,7 +170,8 @@ expect(response_json['meta']['pagination']).to eq( 'current' => 5, 'first' => 1, - 'prev' => 4 + 'prev' => 4, + 'records' => 3 ) end end @@ -173,7 +183,8 @@ expect(response_json['meta']['pagination']).to eq( 'current' => 5, 'first' => 1, - 'prev' => 4 + 'prev' => 4, + 'records' => 3 ) expect(response_json).to have_link(:self) @@ -209,7 +220,8 @@ expect(response_json['meta']['pagination']).to eq( 'current' => 1, 'next' => 2, - 'last' => 3 + 'last' => 3, + 'records' => 3 ) expect(response_json).not_to have_link(:prev)