jsonapi.rb 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -0
- data/lib/jsonapi/pagination.rb +26 -15
- data/lib/jsonapi/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f77c3754e421d90a87ea1b4e29d0a93684491c7b856358544e7d284e016f1e2
|
4
|
+
data.tar.gz: b2fc4ffb3a75a0dde01133af68e2993cc96ee4404adc5d7ffae49c97d15b0532
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36eaed17ff71ecb1dbf2d78cdf80cde3d3f310a1540c62972c675430a5800cc555fc6b2cc00bd83044b5734782e7c8dea94cd9f8dd50efe29d1abfed5cb6f6d1
|
7
|
+
data.tar.gz: 3f6cd094ffad9b139329da5628ff68e53bccef6eba51b73f720fba402dccd9f84eae8fa3a9572f78b50dd33ad1cbd49a1f2d5816450fe58855fa2cf2b509078b
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
data/lib/jsonapi/pagination.rb
CHANGED
@@ -26,19 +26,34 @@ module JSONAPI::Pagination
|
|
26
26
|
#
|
27
27
|
# @return [Array]
|
28
28
|
def jsonapi_pagination(resources)
|
29
|
-
links = {
|
30
|
-
|
31
|
-
}
|
29
|
+
links = { self: request.base_url + request.original_fullpath }
|
30
|
+
pagination = jsonapi_pagination_meta(resources)
|
32
31
|
|
33
|
-
return links
|
34
|
-
|
35
|
-
_, limit, page = jsonapi_pagination_params
|
32
|
+
return links if pagination.blank?
|
36
33
|
|
37
34
|
original_params = params.except(
|
38
35
|
*request.path_parameters.keys.map(&:to_s)).to_unsafe_h
|
39
36
|
original_params[:page] ||= {}
|
40
37
|
original_url = request.base_url + request.path + '?'
|
41
38
|
|
39
|
+
pagination.each do |page_name, number|
|
40
|
+
original_params[:page][:number] = number
|
41
|
+
links[page_name] = original_url + CGI.unescape(original_params.to_query)
|
42
|
+
end
|
43
|
+
|
44
|
+
links
|
45
|
+
end
|
46
|
+
|
47
|
+
# Generates pagination numbers
|
48
|
+
#
|
49
|
+
# @return [Hash] with the first, previous, next, current and last page number
|
50
|
+
def jsonapi_pagination_meta(resources)
|
51
|
+
return {} unless JSONAPI::Rails.is_collection?(resources)
|
52
|
+
|
53
|
+
_, limit, page = jsonapi_pagination_params
|
54
|
+
|
55
|
+
numbers = { current: page }
|
56
|
+
|
42
57
|
if resources.respond_to?(:unscope)
|
43
58
|
total = resources.unscope(:limit, :offset).count()
|
44
59
|
else
|
@@ -48,20 +63,16 @@ module JSONAPI::Pagination
|
|
48
63
|
last_page = [1, (total.to_f / limit).ceil].max
|
49
64
|
|
50
65
|
if page > 1
|
51
|
-
|
52
|
-
|
53
|
-
original_params[:page][:number] = page - 1
|
54
|
-
links[:prev] = original_url + CGI.unescape(original_params.to_query)
|
66
|
+
numbers[:first] = 1
|
67
|
+
numbers[:prev] = page - 1
|
55
68
|
end
|
56
69
|
|
57
70
|
if page < last_page
|
58
|
-
|
59
|
-
|
60
|
-
original_params[:page][:number] = last_page
|
61
|
-
links[:last] = original_url + CGI.unescape(original_params.to_query)
|
71
|
+
numbers[:next] = page + 1
|
72
|
+
numbers[:last] = last_page
|
62
73
|
end
|
63
74
|
|
64
|
-
|
75
|
+
numbers
|
65
76
|
end
|
66
77
|
|
67
78
|
# Extracts the pagination params
|
data/lib/jsonapi/version.rb
CHANGED