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 << (page, html_attrs)
else
page_links << content_tag(:span, page, class: 'current').to_s
end
end
unless current_page == 1
page_links.unshift (current_page - 1, html_attrs).html_safe
end
unless current_page == total_pages
page_links << (current_page + 1, total_pages, html_attrs).html_safe
end
content_tag(:p, class: 'pagination') do
content_tag(:span, "Viewing page #{current_page} of #{total_pages}", class: 'pagination_detail') <<
content_tag(:span, page_links.join(" ").html_safe, class: 'pagination_links')
end.html_safe
end
|