Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

puts in a bisection link #264

Closed
wants to merge 3 commits into from

3 participants

@daveheitzman

created a patch for will_paginate that adds bisection links into the list of page numbers:

instead of
1 2 3 4 5 .. 1000, you'd get 1 2 3 4 5 .. 500 .. 1000

A person can use it by putting a key in the options, like
will_paginate @the_objects, :bisect=>true

If you'd like to pull it into the main repo let me know, otherwise I can just issue it as a separate gem that plugs into will_paginate.

git@github.com:daveheitzman/will_paginate.git

Thanks,
David Heitzman

@travisbot

This pull request fails (merged bf46f95 into 71f793e).

@travisbot

This pull request passes (merged 33b707c into 71f793e).

@travisbot

This pull request fails (merged e38b0e9 into 71f793e).

@mislav
Owner

This is an interesting feature but I feel it's too advanced. For those who really need it, they should subclass the LinkRenderer to create their own behavior. This way you have control over it and can fine-tune it.

@mislav mislav closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
1  lib/will_paginate/active_record.rb
@@ -4,6 +4,7 @@
require 'active_record'
module WillPaginate
+
# = Paginating finders for ActiveRecord models
#
# WillPaginate adds +paginate+, +per_page+ and other methods to
View
29 lib/will_paginate/view_helpers/link_renderer_base.rb
@@ -60,9 +60,36 @@ def windowed_page_numbers
right = (middle.last + 1)..total_pages
end
- left.to_a + middle.to_a + right.to_a
+ out = left.to_a + middle.to_a + right.to_a
+ return @options[:bisect] ? windowed_page_numbers_bisected( out ) : out
end
+
+ def windowed_page_numbers_bisected(out)
+ bisect_right = ((total_pages + current_page)/2).to_i
+ bisect_left = (current_page/2).to_i
+ inner_window, outer_window = @options[:inner_window].to_i, @options[:outer_window].to_i
+
+ start_at = out.index(current_page)
+ return out unless start_at
+ start_at -= 1 while !out[start_at].is_a?(Symbol) && start_at > 0
+ window_from = start_at
+
+ start_at = out.index(current_page)
+ start_at += 1 while !out[start_at].is_a?(Symbol) && start_at < out.size
+ window_to = start_at
+
+ if !out.include?(bisect_right) && bisect_right < total_pages && bisect_right > 1
+ out[window_to-2] = :gap
+ out[window_to-1] =bisect_right
+ end
+ if !out.include?(bisect_left) && bisect_left > 1
+ out[ window_from + 1 ] = bisect_left
+ out[ window_from + 2 ] = :gap
+ end
+ return out
+ end
+
private
def current_page
Something went wrong with that request. Please try again.