From 672103158d098a71e483ddf1377d52e0b3798a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Mugnolo?= Date: Thu, 28 May 2020 15:03:18 -0400 Subject: [PATCH] Allow to exclude the total from one call It adds an option to leave out the total on specific requests. --- lib/rails/pagination.rb | 7 +++++-- spec/rails_spec.rb | 14 +++++++++++--- spec/support/numbers_controller.rb | 5 +++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/rails/pagination.rb b/lib/rails/pagination.rb index 5d0b5c0..6b2b69a 100644 --- a/lib/rails/pagination.rb +++ b/lib/rails/pagination.rb @@ -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) @@ -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 diff --git a/spec/rails_spec.rb b/spec/rails_spec.rb index cde7420..a2e60d9 100644 --- a/spec/rails_spec.rb +++ b/spec/rails_spec.rb @@ -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 @@ -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 @@ -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} @@ -309,4 +317,4 @@ class Fixnum end end end -end \ No newline at end of file +end diff --git a/spec/support/numbers_controller.rb b/spec/support/numbers_controller.rb index 0f2a504..c5a4bf7 100644 --- a/spec/support/numbers_controller.rb +++ b/spec/support/numbers_controller.rb @@ -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 @@ -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)