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
direct_page_link(page)
click to toggle source
# File set/abstract/paging/paging_links.rb, line 64 def direct_page_link page return unless page >= 0 && page <= @total paging_item page + 1, page end
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
links(window)
click to toggle source
# File set/abstract/paging/paging_links.rb, line 25 def links window @window_start = [@current - window, 0].max @window_end = [@current + window, @total].min left_part + window_part + right_part 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
next_page_link()
click to toggle source
# File set/abstract/paging/paging_links.rb, line 59 def next_page_link paging_item '<span aria-hidden="true">»</span>', next_page, "aria-label" => "Next", status: :next 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
previous_page_link()
click to toggle source
# File set/abstract/paging/paging_links.rb, line 54 def previous_page_link paging_item '<span aria-hidden="true">«</span>', previous_page, "aria-label" => "Previous", status: :previous 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