Skip to content

Commit 8dd6354

Browse files
committed
Allow users to skip the Total header
If a table is very large, calculating the number of rows in it may be very costly. We should allow users to skip that query and forego the Total header altogether. Fixes #35 Signed-off-by: David Celis <[email protected]>
1 parent 4f859d0 commit 8dd6354

File tree

6 files changed

+33
-4
lines changed

6 files changed

+33
-4
lines changed

lib/api-pagination.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ def paginator
4545
end
4646

4747
def per_page_header
48-
warn "[DEPRECATION] ApiPagination.paginator is deprecated. Please use ApiPagination.config.per_page_header"
48+
warn "[DEPRECATION] ApiPagination.per_page_header is deprecated. Please use ApiPagination.config.per_page_header"
4949
config.per_page_header
5050
end
5151

5252
def total_header
53-
warn "[DEPRECATION] ApiPagination.paginator is deprecated. Please use ApiPagination.config.total_header"
53+
warn "[DEPRECATION] ApiPagination.total_header is deprecated. Please use ApiPagination.config.total_header"
5454
config.total_header
5555
end
5656

lib/api-pagination/configuration.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Configuration
66

77
attr_accessor :page_header
88

9+
attr_accessor :include_total
10+
911
def configure(&block)
1012
yield self
1113
end
@@ -14,6 +16,7 @@ def initialize
1416
@total_header = 'Total'
1517
@per_page_header = 'Per-Page'
1618
@page_header = nil
19+
@include_total = true
1720
end
1821

1922
def paginator

lib/grape/pagination.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ def paginate(collection)
2323
total_header = ApiPagination.config.total_header
2424
per_page_header = ApiPagination.config.per_page_header
2525
page_header = ApiPagination.config.page_header
26+
include_total = ApiPagination.config.include_total
2627

2728
header 'Link', links.join(', ') unless links.empty?
28-
header total_header, ApiPagination.total_from(collection)
29+
header total_header, ApiPagination.total_from(collection) if include_total
2930
header per_page_header, options[:per_page].to_s
3031
header page_header, options[:page].to_s unless page_header.nil?
3132

lib/rails/pagination.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ def _paginate_collection(collection, options={})
4242
total_header = ApiPagination.config.total_header
4343
per_page_header = ApiPagination.config.per_page_header
4444
page_header = ApiPagination.config.page_header
45+
include_total = ApiPagination.config.include_total
4546

4647
headers['Link'] = links.join(', ') unless links.empty?
47-
headers[total_header] = ApiPagination.total_from(collection)
48+
headers[total_header] = ApiPagination.total_from(collection) if include_total
4849
headers[per_page_header] = options[:per_page].to_s
4950
headers[page_header] = options[:page].to_s unless page_header.nil?
5051

spec/grape_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@
110110
end
111111
end
112112

113+
context 'configured not to include the total' do
114+
before { ApiPagination.config.include_total = false }
115+
116+
it 'should not include a Total header' do
117+
get '/numbers', count: 10
118+
119+
expect(last_response.header['Total']).to be_nil
120+
end
121+
122+
after { ApiPagination.config.include_total = true }
123+
end
124+
113125
context 'with query string including array parameter' do
114126
before do
115127
get '/numbers', { count: 100, parity: ['odd', 'even']}

spec/rails_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,17 @@
111111
expect(page).to eq(1)
112112
end
113113
end
114+
115+
context 'configured not to include the total' do
116+
before { ApiPagination.config.include_total = false }
117+
118+
it 'should not include a Total header' do
119+
get :index, count: 10
120+
121+
expect(response.header['Total']).to be_nil
122+
end
123+
124+
after { ApiPagination.config.include_total = true }
125+
end
114126
end
115127
end

0 commit comments

Comments
 (0)