Skip to content

Commit 01db1e9

Browse files
authored
Add Count of Records to Pagination (stas#29)
* Add total records to pagination * Spec for total records
1 parent 45ccba6 commit 01db1e9

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

lib/jsonapi/pagination.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ def jsonapi_pagination_meta(resources)
8282
numbers[:last] = last_page
8383
end
8484

85+
if total.present?
86+
numbers[:records] = total
87+
end
88+
8589
numbers
8690
end
8791

spec/pagination_spec.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
it do
1818
expect(response_json['data'].size).to eq(0)
1919
expect(response_json['meta'])
20-
.to eq('many' => true, 'pagination' => { 'current' => 1 })
20+
.to eq(
21+
'many' => true,
22+
'pagination' => {
23+
'current' => 1,
24+
'records' => 0
25+
}
26+
)
2127
end
2228

2329
context 'with users' do
@@ -68,7 +74,8 @@
6874
'first' => 1,
6975
'prev' => 1,
7076
'next' => 3,
71-
'last' => 3
77+
'last' => 3,
78+
'records' => 3
7279
)
7380
end
7481
end
@@ -83,7 +90,8 @@
8390
'first' => 1,
8491
'prev' => 1,
8592
'next' => 3,
86-
'last' => 3
93+
'last' => 3,
94+
'records' => 3
8795
)
8896

8997
expect(response_json).to have_link(:self)
@@ -122,7 +130,8 @@
122130
expect(response_json['meta']['pagination']).to eq(
123131
'current' => 3,
124132
'first' => 1,
125-
'prev' => 2
133+
'prev' => 2,
134+
'records' => 3
126135
)
127136

128137
expect(response_json).to have_link(:self)
@@ -161,7 +170,8 @@
161170
expect(response_json['meta']['pagination']).to eq(
162171
'current' => 5,
163172
'first' => 1,
164-
'prev' => 4
173+
'prev' => 4,
174+
'records' => 3
165175
)
166176
end
167177
end
@@ -173,7 +183,8 @@
173183
expect(response_json['meta']['pagination']).to eq(
174184
'current' => 5,
175185
'first' => 1,
176-
'prev' => 4
186+
'prev' => 4,
187+
'records' => 3
177188
)
178189

179190
expect(response_json).to have_link(:self)
@@ -209,7 +220,8 @@
209220
expect(response_json['meta']['pagination']).to eq(
210221
'current' => 1,
211222
'next' => 2,
212-
'last' => 3
223+
'last' => 3,
224+
'records' => 3
213225
)
214226

215227
expect(response_json).not_to have_link(:prev)

0 commit comments

Comments
 (0)