Module: Transit::PaginationHelper

Included in:
TransitHelper
Defined in:
app/helpers/transit/pagination_helper.rb

Instance Method Summary collapse

Instance Method Details

#paginate(collection, html_attrs = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/helpers/transit/pagination_helper.rb', line 5

def paginate(collection, html_attrs = {})    
  return "" if collection.total_count <= 0

  total_pages  = collection.total_pages
  current_page = collection.current_page
  html_classes = (html_attrs.delete(:class) || "").split(" ")
  window_min   = [(current_page - 5), 1].max
  window_max   = [(current_page + 5), total_pages].min
  page_links   = []

  html_attrs.merge!('class' => html_classes.push('pagination_link').uniq.join(" "))

  (window_min..window_max).to_a.each do |page|
    unless page == current_page        
      page_links << pagination_link(page, html_attrs)
    else
      page_links << (:span, page, class: 'current').to_s
    end
  end

  unless current_page == 1
    page_links.unshift pagination_link_previous(current_page - 1, html_attrs).html_safe
  end
  unless current_page == total_pages
    page_links << pagination_link_next(current_page + 1, total_pages, html_attrs).html_safe
  end

  (:p, class: 'pagination') do
    (:span, "Viewing page #{current_page} of #{total_pages}", class: 'pagination_detail') <<
    (:span, page_links.join(" ").html_safe, class: 'pagination_links')
  end.html_safe

end