Permalink
Browse files

Fix coercion error when offset_value is nil

This commit does two things:

* Adds `.offset(0)` when limit is set to `nil` for consistency
* Force-returns `1` for `#total_pages` and `#current_page` when
using `per(nil)`. This avoid "Cannot coerce nil into Fixnum"
errors, and will also prevent "ZeroDivisionError" if
`limit_value` is ever typecast using `to_i`.
  • Loading branch information...
1 parent 150a57d commit 873dcf9a90527307e8d9d1b6b90b442682ebe292 @bricker bricker committed Dec 11, 2012
Showing with 14 additions and 1 deletion.
  1. +4 −1 lib/kaminari/models/page_scope_methods.rb
  2. +10 −0 spec/models/active_record/scopes_spec.rb
@@ -4,7 +4,7 @@ module PageScopeMethods
# Model.page(3).per(10)
def per(num)
if num.nil?
- limit(nil)
+ limit(nil).offset(0)
elsif (n = num.to_i) <= 0
self
elsif max_per_page && max_per_page < n
@@ -20,6 +20,8 @@ def padding(num)
# Total number of pages
def total_pages
+ return 1 if limit_value.nil?
+
total_pages_count = (total_count.to_f / limit_value).ceil
if max_pages.present? && max_pages < total_pages_count
max_pages
@@ -32,6 +34,7 @@ def total_pages
# Current page number
def current_page
+ return 1 if limit_value.nil?
(offset_value / limit_value) + 1
end
@@ -123,6 +123,11 @@
subject { model_class.page }
its(:total_pages) { should == 4 }
end
+
+ context "with per(nil)" do
+ subject { model_class.page.per(nil) }
+ its(:total_pages) { should == 1 }
+ end
end
@@ -136,6 +141,11 @@
subject { model_class.page(2).per 3 }
its(:current_page) { should == 2 }
end
+
+ context "with per(nil)" do
+ subject { model_class.page.per(nil) }
+ its(:current_page) { should == 1 }
+ end
end
describe '#first_page?' do

0 comments on commit 873dcf9

Please sign in to comment.