Skip to content

Instantly share code, notes, and snippets.

@mislav
Created October 11, 2016 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mislav/1bb12cab7a8a6c1b34789769702d99ad to your computer and use it in GitHub Desktop.
Save mislav/1bb12cab7a8a6c1b34789769702d99ad to your computer and use it in GitHub Desktop.
Example will_paginate renderer that avoids rendering high page numbers
require 'action_view'
require 'will_paginate/view_helpers/action_view'
require 'will_paginate/collection'
current_page = 10
total_pages = 31
class NoLastPages < WillPaginate::ActionView::LinkRenderer
# Make sure no page links are rendered after the `:gap` item that follows
# the current page number.
def pagination
seen_current = seen_gap = false
super.reject do |item|
if item == current_page then seen_current = true
elsif seen_current && item == :gap then seen_gap = true
end
seen_gap && item.is_a?(Fixnum)
end
end
end
collection = WillPaginate::Collection.new(current_page, 1, total_pages)
template = Object.new
def template.will_paginate_translate(*) yield end
def template.url_for(params) "/page/#{params[:page]}" end
options = {
param_name: :page,
page_links: true,
link_separator: "\n",
inner_window: 4,
outer_window: 1,
}
renderer = NoLastPages.new
renderer.prepare(collection, options, template)
puts renderer.to_html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment