Skip to content

Instantly share code, notes, and snippets.

@mudasobwa
Last active December 29, 2015 19:39
Show Gist options
  • Save mudasobwa/7718359 to your computer and use it in GitHub Desktop.
Save mudasobwa/7718359 to your computer and use it in GitHub Desktop.
Paginator helper for ruhoh, collapsing page links when there are a lot of posts.
module Ruhoh::Views::Helpers
module Paginator
# current_page is set via a compiler or previewer
# in which it can discern what current_page to serve
def paginator
per_page = config["paginator"]["per_page"] rescue 5
current_page = master.page_data['current_page'].to_i
current_page = current_page.zero? ? 1 : current_page
offset = (current_page-1)*per_page
page_batch = all[offset, per_page]
raise "Page does not exist" unless page_batch
page_batch
end
# [1][2] ... [n-1][n][n+1] ... [last-1][last]
def paginator_navigation
paginator_config = {"borders"=>5, "per_page"=>10}.merge(config["paginator"] || {})
page_count = all.length
total_pages = (page_count.to_f/paginator_config["per_page"]).ceil
current_page = master.page_data['current_page'].to_i
current_page = current_page.zero? ? 1 : current_page
left_dots = ((current_page+1)/2).ceil
right_dots = ((total_pages+current_page)/2).ceil
borders = paginator_config["borders"]
pages = total_pages.times.select { |i|
i+1 <= borders || i+1 > total_pages-borders ||
(i+1 >= current_page-(borders/2).ceil && i+1 <= current_page+(borders/2).ceil) ||
i+1 == left_dots || i+1 == right_dots
}.map { |i|
url = i.zero? && paginator_config["root_page"] ?
paginator_config["root_page"] : "#{paginator_config["url"]}/#{i+1}"
name = (i+1 > borders) && (i+1 < total_pages-borders) &&
((i+1 < current_page-(borders/2).ceil) || (i+1 > current_page+(borders/2).ceil)) &&
(i+1 == left_dots || i+1 == right_dots) ? '…' : "#{i+1}"
{
"url" => ruhoh.to_url(url),
"name" => name,
"is_active_page" => (i+1 == current_page),
"title" => "#{i+1}"
}
}
pages
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment