-
Notifications
You must be signed in to change notification settings - Fork 394
/
backend.rb
44 lines (38 loc) 路 1.68 KB
/
backend.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
# See Pagy::Backend API documentation: https://ddnexus.github.io/pagy/docs/api/backend
# frozen_string_literal: true
class Pagy
# Define a few generic methods to paginate a collection out of the box,
# or any collection by overriding pagy_get_items and/or pagy_get_vars in your controller
# See also the extras if you need specialized methods to paginate Arrays or other collections
module Backend
private
# Return Pagy object and paginated items/results
def pagy(collection, vars = {})
pagy = Pagy.new(pagy_get_vars(collection, vars))
[pagy, pagy_get_items(collection, pagy)]
end
# Sub-method called only by #pagy: here for easy customization of variables by overriding
# You may need to override the count call for non AR collections
def pagy_get_vars(collection, vars)
pagy_set_items_from_params(vars) if defined?(ItemsExtra)
vars[:count] ||= pagy_get_count(collection, vars)
vars[:page] ||= pagy_get_page(vars)
vars
end
# Get the count from the collection
def pagy_get_count(collection, vars)
count_args = vars[:count_args] || DEFAULT[:count_args]
(count = collection.count(*count_args)).is_a?(Hash) ? count.size : count
end
# Get the page integer from the params
# Overridable by the jsonapi extra
def pagy_get_page(vars)
(params[vars[:page_param] || DEFAULT[:page_param]] || 1).to_i
end
# Sub-method called only by #pagy: here for easy customization of record-extraction by overriding
# You may need to override this method for collections without offset|limit
def pagy_get_items(collection, pagy)
collection.offset(pagy.offset).limit(pagy.items)
end
end
end