44module ApiPagination
55 class << self
66 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
1011
11- case ApiPagination . config . paginator
12+ case options [ : paginator]
1213 when :pagy
1314 paginate_with_pagy ( collection , options )
1415 when :kaminari
1516 paginate_with_kaminari ( collection , options , options [ :paginate_array_options ] || { } )
1617 when :will_paginate
1718 paginate_with_will_paginate ( collection , options )
1819 else
19- raise StandardError , "Unknown paginator: #{ ApiPagination . config . paginator } "
20+ raise StandardError , "Unknown paginator: #{ options [ : paginator] } "
2021 end
2122 end
2223
2324 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 )
2527
2628 { } . tap do |pages |
2729 unless collection . first_page?
2830 pages [ :first ] = 1
2931 pages [ :prev ] = collection . current_page - 1
3032 end
3133
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]
3436 pages [ :next ] = collection . current_page + 1
3537 end
3638 end
3739 end
3840
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 ]
4144 when :pagy then collection . count . to_s
4245 when :kaminari then collection . total_count . to_s
4346 when :will_paginate then collection . total_entries . to_s
@@ -69,19 +72,19 @@ def pagy_from(collection, options)
6972 else
7073 count = collection . is_a? ( Array ) ? collection . count : collection . count ( :all )
7174 end
72-
75+
7376 Pagy . new ( count : count , items : options [ :per_page ] , page : options [ :page ] )
7477 end
7578
76- def pagy_pages_from ( pagy )
79+ def pagy_pages_from ( pagy , options )
7780 { } . tap do |pages |
7881 unless pagy . page == 1
7982 pages [ :first ] = 1
8083 pages [ :prev ] = pagy . prev
8184 end
8285
8386 unless pagy . page == pagy . pages
84- pages [ :last ] = pagy . pages if ApiPagination . config . include_total
87+ pages [ :last ] = pagy . pages if options [ : include_total]
8588 pages [ :next ] = pagy . next
8689 end
8790 end
@@ -96,7 +99,7 @@ def paginate_with_kaminari(collection, options, paginate_array_options = {})
9699
97100 collection = Kaminari . paginate_array ( collection , paginate_array_options ) if collection . is_a? ( Array )
98101 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]
100103 [ collection , nil ]
101104 end
102105
0 commit comments