Skip to content

Commit 39e4511

Browse files
committed
Take total_count option when kaminari used
1 parent 52207af commit 39e4511

File tree

3 files changed

+74
-12
lines changed

3 files changed

+74
-12
lines changed

lib/rails/pagination.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@ def paginate_with(collection)
2424
private
2525

2626
def _paginate_collection(collection, options={})
27-
options = {
28-
:page => ApiPagination.config.page_param(params),
29-
:per_page => (
30-
options.delete(:per_page) ||
31-
ApiPagination.config.per_page_param(params)
32-
)
33-
}
27+
options[:page] = ApiPagination.config.page_param(params)
28+
options[:per_page] ||= ApiPagination.config.per_page_param(params)
29+
3430
collection = ApiPagination.paginate(collection, options)
3531

3632
links = (headers['Link'] || "").split(',').map(&:strip)
@@ -47,12 +43,20 @@ def _paginate_collection(collection, options={})
4743
page_header = ApiPagination.config.page_header
4844
include_total = ApiPagination.config.include_total
4945

50-
headers['Link'] = links.join(', ') unless links.empty?
51-
headers[total_header] = ApiPagination.total_from(collection) if include_total
46+
headers['Link'] = links.join(', ') unless links.empty?
5247
headers[per_page_header] = options[:per_page].to_s
53-
headers[page_header] = options[:page].to_s unless page_header.nil?
48+
headers[page_header] = options[:page].to_s unless page_header.nil?
49+
headers[total_header] = total_count(collection, options) if include_total
5450

5551
return collection
5652
end
53+
54+
def total_count(collection, options)
55+
total_count = if ApiPagination.config.paginator == :kaminari
56+
paginate_array_options = options[:paginate_array_options]
57+
paginate_array_options[:total_count] if paginate_array_options
58+
end
59+
total_count || ApiPagination.total_from(collection)
60+
end
5761
end
5862
end

spec/rails_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,52 @@
201201
end
202202
end
203203

204+
context 'paginate array options' do
205+
shared_examples 'properly set Total header' do
206+
let(:params) do
207+
{
208+
paginate_array_total_count: paginate_array_total_count,
209+
count: count,
210+
}
211+
end
212+
213+
specify do
214+
get :index_with_paginate_array_options, params
215+
expect(response.header['Total'].to_i).to eq total_header
216+
end
217+
end
218+
219+
context 'kaminari' do
220+
around do |example|
221+
paginator = ApiPagination.config.paginator
222+
ApiPagination.config.paginator = :kaminari
223+
example.run
224+
ApiPagination.config.paginator = paginator
225+
end
226+
227+
it_should_behave_like 'properly set Total header' do
228+
let(:paginate_array_total_count) { 300 }
229+
let(:total_header) { 300 }
230+
let(:count) { 50 }
231+
end
232+
end
233+
234+
context 'will_paginate' do
235+
around do |example|
236+
paginator = ApiPagination.config.paginator
237+
ApiPagination.config.paginator = :will_paginate
238+
example.run
239+
ApiPagination.config.paginator = paginator
240+
end
241+
242+
it_should_behave_like 'properly set Total header' do
243+
let(:paginate_array_total_count) { 300 }
244+
let(:total_header) { 50 }
245+
let(:count) { 50 }
246+
end
247+
end
248+
end
249+
204250
context 'default per page in model' do
205251
before do
206252
class Fixnum

spec/support/numbers_controller.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ def teardown(*methods)
4040

4141
Rails.application.routes.draw do
4242
resources :numbers, :only => [:index] do
43-
get :index_with_custom_render, on: :collection
44-
get :index_with_no_per_page, on: :collection
43+
collection do
44+
get :index_with_custom_render
45+
get :index_with_no_per_page
46+
get :index_with_paginate_array_options
47+
end
4548
end
4649
end
4750

@@ -85,4 +88,13 @@ def index_with_no_per_page
8588

8689
render json: NumbersSerializer.new(numbers)
8790
end
91+
92+
def index_with_paginate_array_options
93+
count = params.fetch(:count).to_i
94+
total_count = params.fetch(:paginate_array_total_count).to_i
95+
numbers = (1..count).to_a
96+
numbers = paginate numbers, paginate_array_options: {total_count: total_count}
97+
98+
render json: NumbersSerializer.new(numbers)
99+
end
88100
end

0 commit comments

Comments
 (0)