File tree Expand file tree Collapse file tree 2 files changed +28
-9
lines changed Expand file tree Collapse file tree 2 files changed +28
-9
lines changed Original file line number Diff line number Diff line change @@ -68,24 +68,23 @@ def paginate_with_will_paginate(collection, options)
68
68
69
69
def get_default_per_page_for_kaminari ( collection )
70
70
default = Kaminari . config . default_per_page
71
- detect_model ( collection ) . default_per_page || default
72
- rescue
73
- default
71
+ extract_per_page_from_model ( collection , :default_per_page ) || default
74
72
end
75
73
76
74
def default_per_page_for_will_paginate ( collection )
77
75
default = WillPaginate . per_page
78
- detect_model ( collection ) . per_page || default
79
- rescue
80
- default
76
+ extract_per_page_from_model ( collection , :per_page ) || default
81
77
end
82
78
83
- def detect_model ( collection )
84
- if collection . respond_to? ( :klass )
79
+ def extract_per_page_from_model ( collection , accessor )
80
+ klass = if collection . respond_to? ( :klass )
85
81
collection . klass
86
82
else
87
83
collection . first . class
88
84
end
85
+
86
+ return unless klass . respond_to? ( accessor )
87
+ klass . send ( accessor )
89
88
end
90
89
end
91
90
end
Original file line number Diff line number Diff line change @@ -287,7 +287,7 @@ class Fixnum
287
287
expect ( response . header [ 'Per-Page' ] ) . to eq ( '6' )
288
288
end
289
289
290
- it 'should not fail if model does not respond to per page' do
290
+ it 'should not fail if the model yields nil for per page' do
291
291
class Fixnum
292
292
@default_per_page = nil
293
293
@per_page = nil
@@ -302,6 +302,26 @@ class Fixnum
302
302
end
303
303
)
304
304
end
305
+
306
+ # This spec has to be last because if we undefine these methods
307
+ # at runtime and then invoke another test that uses them, they will
308
+ # raise a NoMethodError
309
+ it 'should not fail if model does not respond to per page' do
310
+ class Fixnum
311
+ class << self
312
+ undef_method :default_per_page , :per_page
313
+ end
314
+ end
315
+
316
+ get :index_with_no_per_page , params : { count : 100 }
317
+
318
+ expect ( response . header [ 'Per-Page' ] ) . to eq (
319
+ case ApiPagination . config . paginator
320
+ when :kaminari then Kaminari . config . default_per_page . to_s
321
+ when :will_paginate then WillPaginate . per_page . to_s
322
+ end
323
+ )
324
+ end
305
325
end
306
326
end
307
327
end
You can’t perform that action at this time.
0 commit comments