Skip to content

Instantly share code, notes, and snippets.

@ohlol
Created February 18, 2010 18:24
Show Gist options
  • Save ohlol/307912 to your computer and use it in GitHub Desktop.
Save ohlol/307912 to your computer and use it in GitHub Desktop.
module ViewHelpers
def paginate(collection, route)
cur = collection.current_page
tot = collection.total_pages
ret = ''
ret << prev_page(collection, route) + ' '
if (1..8) === cur
1.upto(9) do |i|
if i == cur
ret << "#{i} "
else
ret << link_to_page(route, i) + ' '
end
end
ret << '... '
ret << link_to_page(route, tot-1) + ' '
ret << link_to_page(route, tot) + ' '
elsif (9..tot-8) === cur
1.upto(2) do |i|
ret << link_to_page(route, i) + ' '
end
ret << '... '
(cur-4).upto(cur+4) do |i|
if i == cur
ret << "#{i} "
else
ret << link_to_page(route, i) + ' '
end
end
ret << '... '
ret << link_to_page(route, tot-1) + ' '
ret << link_to_page(route, tot) + ' '
else
1.upto(2) do |i|
ret << link_to_page(route, i) + ' '
end
ret << '... '
(tot-8).upto(tot) do |i|
if i == cur
ret << "#{i} "
else
ret << link_to_page(route, i) + ' '
end
end
end
ret << next_page(collection, route) + ' '
ret
end
private
def link_to_page(route, page)
link_to(page, url_for(route, :page, :page => page))
end
def prev_page(collection, route)
collection.previous_page ? link_to('&laquo; Previous', url_for(route, :page, :page => collection.previous_page)) : '&laquo; Previous'
end
def next_page(collection, route)
collection.next_page ? link_to('&laquo; Next', url_for(route, :page, :page => collection.next_page)) : '&laquo; Next'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment