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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/dugway/liquid/tags/paginate.rb', line 26
def render(context)
@context = context
@limit = context[@limit].present? ? context[@limit] : (@limit.present? ? @limit.to_i : nil)
@per_page = context[@per_page].present? ? context[@per_page] : (@per_page.present? ? @per_page.to_i : nil)
@order = context[@attributes['order']].present? ? context[@attributes['order']] : @attributes['order']
context.stack do
context['internal'] = {
'per_page' => @per_page,
'order' => @order,
'page' => @context.registers[:params][:page] && @context.registers[:params][:page].to_i,
'limit' => @limit
}
collection = context[@collection_name]
context[@variable_name] = collection
current_page = collection.current_page
= {
'page_size' => collection.per_page,
'current_page' => current_page,
'current_offset' => collection.offset
}
context['paginate'] =
collection_size = collection.total_entries
raise ArgumentError.new("Cannot paginate array '#{ @collection_name }'. Not found.") if collection_size.nil?
page_count = collection.total_pages
['items'] = collection_size
['pages'] = page_count
['previous'] = collection.previous_page.blank? ? no_link('« Previous') : link('« Previous', collection.previous_page)
['next'] = collection.next_page.blank? ? no_link('Next »') : link('Next »', collection.next_page)
['parts'] = []
if page_count > 1
inner_window = @attributes['inner_window'].to_i
outer_window = @attributes['outer_window'].to_i
min = current_page - inner_window
max = current_page + inner_window
if max > page_count
min -= max - page_count
elsif min < 1
max += 1 - min
end
current = min..max
beginning = 1..(1 + outer_window)
tail = (page_count - outer_window)..page_count
visible = [current, beginning, tail].map(&:to_a).flatten
visible &= (1..page_count).to_a
hellip_break = false
1.upto(page_count) { |page|
if page == current_page
['parts'] << no_link(page)
elsif visible.include?(page)
['parts'] << link(page, page)
elsif page == beginning.last + 1 || page == tail.first - 1
next if hellip_break
['parts'] << no_link('…')
hellip_break = true
next
end
hellip_break = false
}
end
render_all @nodelist, context
end
end
|