4
4
module ApiPagination
5
5
class << self
6
6
def paginate ( collection , options = { } )
7
- options [ :page ] = options [ :page ] . to_i
8
- options [ :page ] = 1 if options [ :page ] <= 0
9
- options [ :per_page ] = options [ :per_page ] . to_i
7
+ options [ :page ] = options [ :page ] . to_i
8
+ options [ :page ] = 1 if options [ :page ] <= 0
9
+ options [ :per_page ] = options [ :per_page ] . to_i
10
+ options [ :paginator ] ||= ApiPagination . config . paginator
10
11
11
- case ApiPagination . config . paginator
12
+ case options [ : paginator]
12
13
when :pagy
13
14
paginate_with_pagy ( collection , options )
14
15
when :kaminari
15
16
paginate_with_kaminari ( collection , options , options [ :paginate_array_options ] || { } )
16
17
when :will_paginate
17
18
paginate_with_will_paginate ( collection , options )
18
19
else
19
- raise StandardError , "Unknown paginator: #{ ApiPagination . config . paginator } "
20
+ raise StandardError , "Unknown paginator: #{ options [ : paginator] } "
20
21
end
21
22
end
22
23
23
24
def pages_from ( collection , options = { } )
24
- return pagy_pages_from ( collection ) if ApiPagination . config . paginator == :pagy && collection . is_a? ( Pagy )
25
+ options [ :paginator ] ||= ApiPagination . config . paginator
26
+ return pagy_pages_from ( collection , options ) if options [ :paginator ] == :pagy && collection . is_a? ( Pagy )
25
27
26
28
{ } . tap do |pages |
27
29
unless collection . first_page?
28
30
pages [ :first ] = 1
29
31
pages [ :prev ] = collection . current_page - 1
30
32
end
31
33
32
- unless collection . last_page? || ( ApiPagination . config . paginator == :kaminari && collection . out_of_range? )
33
- pages [ :last ] = collection . total_pages if ApiPagination . config . include_total
34
+ unless collection . last_page? || ( options [ : paginator] == :kaminari && collection . out_of_range? )
35
+ pages [ :last ] = collection . total_pages if options [ : include_total]
34
36
pages [ :next ] = collection . current_page + 1
35
37
end
36
38
end
37
39
end
38
40
39
- def total_from ( collection )
40
- case ApiPagination . config . paginator
41
+ def total_from ( collection , options )
42
+ options [ :paginator ] ||= ApiPagination . config . paginator
43
+ case options [ :paginator ]
41
44
when :pagy then collection . count . to_s
42
45
when :kaminari then collection . total_count . to_s
43
46
when :will_paginate then collection . total_entries . to_s
@@ -69,19 +72,19 @@ def pagy_from(collection, options)
69
72
else
70
73
count = collection . is_a? ( Array ) ? collection . count : collection . count ( :all )
71
74
end
72
-
75
+
73
76
Pagy . new ( count : count , items : options [ :per_page ] , page : options [ :page ] )
74
77
end
75
78
76
- def pagy_pages_from ( pagy )
79
+ def pagy_pages_from ( pagy , options )
77
80
{ } . tap do |pages |
78
81
unless pagy . page == 1
79
82
pages [ :first ] = 1
80
83
pages [ :prev ] = pagy . prev
81
84
end
82
85
83
86
unless pagy . page == pagy . pages
84
- pages [ :last ] = pagy . pages if ApiPagination . config . include_total
87
+ pages [ :last ] = pagy . pages if options [ : include_total]
85
88
pages [ :next ] = pagy . next
86
89
end
87
90
end
@@ -96,7 +99,7 @@ def paginate_with_kaminari(collection, options, paginate_array_options = {})
96
99
97
100
collection = Kaminari . paginate_array ( collection , paginate_array_options ) if collection . is_a? ( Array )
98
101
collection = collection . page ( options [ :page ] ) . per ( options [ :per_page ] )
99
- collection . without_count if !collection . is_a? ( Array ) && !ApiPagination . config . include_total
102
+ collection . without_count if !collection . is_a? ( Array ) && !options [ : include_total]
100
103
[ collection , nil ]
101
104
end
102
105
0 commit comments