class PagingLinks

render paging links

Public Class Methods

new(total_pages, current_page) click to toggle source
# File set/abstract/paging/paging_links.rb, line 5
def initialize total_pages, current_page
  @total = total_pages
  @current = current_page
end

Public Instance Methods

build(window=2, &block) click to toggle source

@param window [integer] number of page links shown left and right

of the current page

@example: current page = 5, window = 2

|<<|1|...|3|4|[5]|6|7|...|10|>>|

@yield [text, page, status, options] block to build single paging link @yieldparam status [Symbol] :active (for current page) or :disabled @yieldparam page [Integer] page number, first page is 0 @return [Array<String>]

# File set/abstract/paging/paging_links.rb, line 18
def build window=2, &block
  @render_item = block
  links window
end

Private Instance Methods

ellipse() click to toggle source
# File set/abstract/paging/paging_links.rb, line 70
def ellipse
  paging_item "<span>...</span>", nil, status: :ellipses
end
left_part() click to toggle source
# File set/abstract/paging/paging_links.rb, line 38
def left_part
  [
    previous_page_link,
    (direct_page_link 0 if @window_start.positive?),
    (ellipse if @window_start > 1)
  ].compact
end
next_page() click to toggle source
# File set/abstract/paging/paging_links.rb, line 88
def next_page
  @current < @total ? @current + 1 : false
end
paging_item(text, page, options={}) click to toggle source
# File set/abstract/paging/paging_links.rb, line 74
def paging_item text, page, options={}
  status =
    if page == @current
      :current
    else
      options.delete :status
    end
  @render_item.call text, page, status, options
end
previous_page() click to toggle source
# File set/abstract/paging/paging_links.rb, line 84
def previous_page
  @current.positive? ? @current - 1 : false
end
right_part() click to toggle source
# File set/abstract/paging/paging_links.rb, line 46
def right_part
  [
    (ellipse if @total > @window_end + 1),
    (direct_page_link @total if @total > @window_end),
    next_page_link
  ].compact
end
window_part() click to toggle source

the links around the current page

# File set/abstract/paging/paging_links.rb, line 32
def window_part
  (@window_start..@window_end).map do |page|
    direct_page_link page
  end.compact
end