Skip to content

Instantly share code, notes, and snippets.

@SitanHuang
Created June 9, 2018 21:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SitanHuang/d1566828a47fddb9b9291846ecd09962 to your computer and use it in GitHub Desktop.
Save SitanHuang/d1566828a47fddb9b9291846ecd09962 to your computer and use it in GitHub Desktop.
Ruby Pagination Algorithm HTML Generator
module PaginationHelper
def paginate(current, max, template, delta: 2)
left = current - delta
right = current + delta + 1
range = []
range_with_dots = []
l = nil
max.times do |_i|
i = _i + 1
if i == 1 || i == max || i >= left && i < right
range << <<-HTML
<a class="num button" href="#{template.sub('%num%', ((i-1) * ITEMS_PER_LOAD).to_s)}">#{i}</a>
HTML
end
end
range.each_with_index do |btn, i|
if l&.>0
if i - l == 2
range_with_dots << range[l]
elsif i - l != 1
range_with_dots << '<span>...</span>'
end
end
range_with_dots << btn
l = i
end
range_with_dots.join("\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment