Created
January 25, 2010 18:18
-
-
Save ilpoldo/286079 to your computer and use it in GitHub Desktop.
Creates an array of pages not bigger than 9 elements, with ellipsis to signal gaps.
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
def page_order(page, total_pages, links=9) | |
bck = ((page > links ? page-links : 1)..page).entries.reverse | |
fwd = (page..((total_pages - page > links ? page+links : total_pages))).entries | |
order = fwd.size > page ? fwd.zip(bck) : bck.zip(fwd) | |
order = order.flatten.compact.uniq[0,links].sort | |
if order[0] !=1 | |
order[1]='...' | |
order[0]=1 | |
end | |
if order[-1] !=total_pages | |
order[-2]='...' | |
order[-1]=total_pages | |
end | |
order | |
end | |
# Example: | |
example = [[1,2],[2,2],[1,6],[3,6],[6,6],[1,13],[3,13],[7,13],[12,13], [3,200], [50,1000]] | |
example.each do |t| | |
p page_order(*t) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment