Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix wrong pagination when used with padding #359

Merged
merged 1 commit into from

4 participants

Vladimir Gordeev Yuki Nishijima Akira Matsuda Michael Borohovski
Vladimir Gordeev

Pagination just breaks when used with padding, because total_pages and current_page doesn't checks is padding used.

For example:

source = [
  # padding
  1, 2, 3, 4,

  # first page
  5, 6, 7,

  # second
  8, 9,
]
arr = Kaminari.paginate_array(source).page(2).per(3).padding(4)

puts arr.inspect
# => [8, 9] which is correct

puts arr.total_pages # => 3
# whoops, that's wrong, 2 was expected

puts arr.current_page # => 3
# also wrong, 2 was expected

Array used just for example, I encounter same problem when work with active record.

I fixed it, but probably it can be written in more sound style to the rest of code.

Yuki Nishijima
Collaborator

@vladimir-vg Thank you for the PR! Could you provide some failng tests for the PR?

Akira Matsuda
Owner

@vladimir-vg Confirmed the bug. Going to merge your PR. Thanks!

Akira Matsuda amatsuda merged commit ee5954f into from
Michael Borohovski

I think this is still not working, actually.

[26] pry(main)> a = [1,2,3,4,5,6,7,8,9]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
[27] pry(main)> b = Kaminari.paginate_array(a).page(2).per(3).padding(4)
=> [8, 9]
[28] pry(main)> b
=> [8, 9]
[29] pry(main)> b.total_pages
=> 3
[30] pry(main)> b.current_page
=> 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 21, 2013
  1. Vladimir Gordeev
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 2 deletions.
  1. +12 −2 lib/kaminari/models/page_scope_methods.rb
14 lib/kaminari/models/page_scope_methods.rb
View
@@ -15,6 +15,7 @@ def per(num)
end
def padding(num)
+ @_padding = num
offset(offset_value + num.to_i)
end
@@ -22,7 +23,11 @@ def padding(num)
def total_pages
return 1 if limit_value.nil?
- total_pages_count = (total_count.to_f / limit_value).ceil
+ count_without_padding = total_count
+ count_without_padding -= @_padding if @_padding
+ count_without_padding = 0 if count_without_padding < 0
+
+ total_pages_count = (count_without_padding.to_f / limit_value).ceil
if max_pages.present? && max_pages < total_pages_count
max_pages
else
@@ -35,7 +40,12 @@ def total_pages
# Current page number
def current_page
return 1 if limit_value.nil?
- (offset_value / limit_value) + 1
+
+ offset_without_padding = offset_value
+ offset_without_padding -= @_padding if @_padding
+ offset_without_padding = 0 if offset_without_padding < 0
+
+ (offset_without_padding / limit_value) + 1
end
# First page of the collection ?
Something went wrong with that request. Please try again.