Skip to content

Commit e801a07

Browse files
committed
Use per_page based on model by default
1 parent c5f6e79 commit e801a07

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

lib/api-pagination.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,42 @@ def paginate_with_kaminari(collection, options, paginate_array_options = {})
4545
if Kaminari.config.max_per_page && options[:per_page] > Kaminari.config.max_per_page
4646
options[:per_page] = Kaminari.config.max_per_page
4747
elsif options[:per_page] <= 0
48-
options[:per_page] = Kaminari.config.default_per_page
48+
options[:per_page] = get_default_per_page_for_kaminari(collection)
4949
end
5050

5151
collection = Kaminari.paginate_array(collection, paginate_array_options) if collection.is_a?(Array)
5252
collection.page(options[:page]).per(options[:per_page])
5353
end
5454

5555
def paginate_with_will_paginate(collection, options)
56-
options[:per_page] = WillPaginate.per_page if options[:per_page] <= 0
56+
if options[:per_page] <= 0
57+
options[:per_page] = default_per_page_for_will_paginate(collection)
58+
end
5759

5860
if defined?(Sequel::Dataset) && collection.kind_of?(Sequel::Dataset)
5961
collection.paginate(options[:page], options[:per_page])
6062
else
6163
collection.paginate(:page => options[:page], :per_page => options[:per_page])
6264
end
6365
end
66+
67+
def get_default_per_page_for_kaminari(collection)
68+
default = Kaminari.config.default_per_page
69+
detect_model(collection).default_per_page || default
70+
rescue
71+
default
72+
end
73+
74+
def default_per_page_for_will_paginate(collection)
75+
default = WillPaginate.per_page
76+
detect_model(collection).per_page || default
77+
rescue
78+
default
79+
end
80+
81+
def detect_model(collection)
82+
collection.first.class
83+
end
6484
end
6585
end
6686

spec/rails_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,47 @@
200200
end
201201
end
202202
end
203+
204+
context 'default per page in model' do
205+
before do
206+
class Fixnum
207+
@default_per_page = 6
208+
@per_page = 6
209+
210+
class << self
211+
attr_accessor :default_per_page, :per_page
212+
end
213+
end
214+
end
215+
216+
after do
217+
class Fixnum
218+
@default_per_page = 25
219+
@per_page = 25
220+
end
221+
end
222+
223+
it 'should use default per page from model' do
224+
get :index_with_no_per_page, count: 100
225+
226+
expect(response.header['Per-Page']).to eq('6')
227+
end
228+
229+
it 'should not fail if model does not respond to per page' do
230+
class Fixnum
231+
@default_per_page = nil
232+
@per_page = nil
233+
end
234+
235+
get :index_with_no_per_page, count: 100
236+
237+
expect(response.header['Per-Page']).to eq(
238+
case ApiPagination.config.paginator
239+
when :kaminari then Kaminari.config.default_per_page.to_s
240+
when :will_paginate then WillPaginate.per_page.to_s
241+
end
242+
)
243+
end
244+
end
203245
end
204246
end

0 commit comments

Comments
 (0)