Skip to content

Allow to exclude the total from one call #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/rails/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def _discover_format(options)
def _paginate_collection(collection, options={})
options[:page] = ApiPagination.config.page_param(params)
options[:per_page] ||= ApiPagination.config.per_page_param(params)
exclude_total = options.delete :exclude_total

collection, pagy = ApiPagination.paginate(collection, options)

Expand All @@ -48,12 +49,14 @@ def _paginate_collection(collection, options={})
total_header = ApiPagination.config.total_header
per_page_header = ApiPagination.config.per_page_header
page_header = ApiPagination.config.page_header
include_total = ApiPagination.config.include_total
include_total = ApiPagination.config.include_total && !exclude_total

headers['Link'] = links.join(', ') unless links.empty?
headers[per_page_header] = options[:per_page].to_s
headers[page_header] = options[:page].to_s unless page_header.nil?
headers[total_header] = total_count(pagy || collection, options).to_s if include_total
if include_total
headers[total_header] = total_count(pagy || collection, options).to_s
end

return collection
end
Expand Down
14 changes: 11 additions & 3 deletions spec/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@
after { ApiPagination.config.include_total = true }
end

context 'without a total' do
it 'should not include a Total header' do
get :index_without_total

expect(response.header['Total']).to be_nil
end
end

context 'custom page param' do
context 'page_param as a symbol' do
before do
Expand Down Expand Up @@ -262,7 +270,7 @@ class Fixnum
end
end

after :all do
after :all do
class Fixnum
class << self
undef_method :default_per_page, :per_page
Expand Down Expand Up @@ -295,7 +303,7 @@ class Fixnum
end
end

context 'default per page in objects without paginator defaults' do
context 'default per page in objects without paginator defaults' do
it 'should not fail if model does not respond to per page' do
get :index_with_no_per_page, params: {count: 100}

Expand All @@ -309,4 +317,4 @@ class Fixnum
end
end
end
end
end
5 changes: 5 additions & 0 deletions spec/support/numbers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def teardown(*methods)
get :index_with_custom_render
get :index_with_no_per_page
get :index_with_paginate_array_options
get :index_without_total
end
end
end
Expand Down Expand Up @@ -98,6 +99,10 @@ def index_with_paginate_array_options

render json: NumbersSerializer.new(numbers)
end

def index_without_total
paginate json: (1..100).to_a, exclude_total: true
end
end

ApiPagination::Railtie.initializers.each(&:run)