%# @count_gap refers to the minimum number of sibling pages to show %>
<% @count_gap = 2; @i = 1; @total_pages = total_pages; %>
<% if @current_page > 1 %>
Previous
<% else %>
Previous
<% end %>
<% while @i <= @total_pages do %>
<% @gap = false %>
<%# first half ensures we're not caught in an infinite loop at @total_pages - 1 %>
<%# second half, with the .abs, checks for gap siblings ahead *and* behind the current page %>
<% if @i < (@total_pages - 1) && (@i - @current_page).abs > @count_gap %>
<% @gap = true %>
<%# we skip multiple iterations to insert the gap (`...`), and continue with the regular links %>
<% @i = (@i < @current_page ? @current_page - @count_gap : @total_pages - 1) - 1 %>
<% end %>
<% if @gap %>
…
<% else %>
<%= @i %>
<% end %>
<% @i += 1 %>
<% end %>
<% if @current_page < @total_pages %>
Next
<% else %>
Next
<% end %>