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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/sequel/paginate/views_helper.rb', line 7
def paginate(models, *args, &block)
args = [{}] if args.nil? || args.empty?
page_no = (params[:page] || 1).to_i
left = args[0][:left] || 3
right = args[0][:right] || 3
middle = args[0][:middle] || 3
remote = args[0][:remote] || false
html_class = args[0][:class] || "pagination"
class_name = models.first.class
path = request.path
page_count = (1..(class_name.count.to_f / class_name.paginate_per).ceil).to_a
return nil if page_count.length <= 1
page_to_show = []
if page_count.count > left + right + middle
page_count.each do |p|
if p > left && p < page_count[-right]
if (p < page_no + middle && p > page_no - middle)
page_to_show << p
else
page_to_show << -1 unless page_to_show.last == -1
end
else
page_to_show << p
end
end
else
page_to_show = page_count
end
html = "<div class='#{html_class}'><ul>"
if page_no <= 1
html += "<li><a>Prev</a></li>"
else
html += "<li class='previous'><a href='#{path}?page=#{page_no-1}' #{remote_tag(remote)}>Prev</a></li>"
end
page_to_show.each do |page|
if page == -1
html += "<li><a>...</a></li>"
else
if page == page_no
html += "<li class='active'><a>#{page}</a></li>"
else
html += "<li><a href='#{path}?page=#{page}' #{remote_tag(remote)}>#{page}</a></li>"
end
end
end
if page_no >= page_count.count
html += "<li><a>Next</a></li>"
else
html += "<li class='next'><a href='#{path}?page=#{page_no+1}' #{remote_tag(remote)}>Next</a></li>"
end
html += "</ul></div>"
html.html_safe
end
|