forked from toptal/chewy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kaminari.rb
45 lines (37 loc) · 1.24 KB
/
kaminari.rb
1
2
3
4
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
38
39
40
41
42
43
44
45
module Chewy
module Search
module Pagination
# This module provides `Kaminari` support for {Chewy::Search::Request}
# It is included automatically if `Kaminari` is available.
#
# @example
# PlacesIndex.all.page(3).per(10).order(:name)
# # => <PlacesIndex::Query {..., :body=>{:size=>10, :from=>20, :sort=>["name"]}}>
module Kaminari
extend ActiveSupport::Concern
included do
include ::Kaminari::PageScopeMethods
delegate :default_per_page, :max_per_page, :max_pages, to: :_kaminari_config
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{::Kaminari.config.page_method_name}(num = 1)
limit(limit_value).offset(limit_value * ([num.to_i, 1].max - 1))
end
METHOD
end
def limit_value
(raw_limit_value || default_per_page).to_i
end
def offset_value
raw_offset_value.to_i
end
private
def _kaminari_config
::Kaminari.config
end
def paginated_collection(collection)
::Kaminari.paginate_array(collection, limit: limit_value, offset: offset_value, total_count: total_count)
end
end
end
end
end