Created
February 18, 2010 18:24
-
-
Save ohlol/307912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('« Previous', url_for(route, :page, :page => collection.previous_page)) : '« Previous' | |
end | |
def next_page(collection, route) | |
collection.next_page ? link_to('« Next', url_for(route, :page, :page => collection.next_page)) : '« Next' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment