jsonapi-query_builder 0.1.9 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +75 -32
- data/README.md +26 -0
- data/jsonapi-query_builder.gemspec +4 -0
- data/lib/jsonapi/query_builder/base_query.rb +2 -0
- data/lib/jsonapi/query_builder/errors/unpermitted_sort_parameters.rb +17 -0
- data/lib/jsonapi/query_builder/mixins/paginate.rb +15 -4
- data/lib/jsonapi/query_builder/mixins/sort.rb +3 -8
- data/lib/jsonapi/query_builder/paginator/base_paginator.rb +22 -0
- data/lib/jsonapi/query_builder/paginator/kaminari.rb +34 -0
- data/lib/jsonapi/query_builder/paginator/keyset.rb +64 -0
- data/lib/jsonapi/query_builder/paginator/pagy.rb +27 -0
- data/lib/jsonapi/query_builder/paginator.rb +4 -0
- data/lib/jsonapi/query_builder/version.rb +1 -1
- data/lib/jsonapi/query_builder.rb +0 -2
- metadata +64 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 51c9f4a8cc7fb70d6ca5a535cdc3386e97231960b915b02af356126b57d80609
|
|
4
|
+
data.tar.gz: 9a1684797aa2b39a67c66226da08c0adf326133e4d51190a78f34817e4fc288b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 70121ffb0d3419bfddff71778b6d4214e4de00c9f61f0bbd272ff6ab5f0a9e848f04c37124cc911371909326fa1f1a41cf1e401f2cd4dcc56e309cc323e2b9fd
|
|
7
|
+
data.tar.gz: ed296c71cf3bfdc1b46ee995fad2c2de965b38bafad1cad00f8571e55e09a83f18627b36360e231f68e9015b211fcefde2686ba8ef41bdce8c4060dc6fc32506
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Change log
|
|
2
2
|
|
|
3
|
+
## 0.2.0 (2021-09-29)
|
|
4
|
+
Added support for Kaminari and Keyset pagination strategies in addition to Pagy.
|
|
5
|
+
|
|
6
|
+
- [#21](https://github.com/infinum/jsonapi-query_builder/pull/21): Extract paginators.
|
|
7
|
+
|
|
3
8
|
## 0.1.9 (2021-05-07)
|
|
4
9
|
|
|
5
10
|
- [#18](https://github.com/infinum/jsonapi-query_builder/pull/18): Remove Ruby `to` version.
|
data/Gemfile.lock
CHANGED
|
@@ -1,38 +1,77 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
jsonapi-query_builder (0.
|
|
4
|
+
jsonapi-query_builder (0.2.0)
|
|
5
5
|
activerecord (>= 5)
|
|
6
6
|
pagy (~> 3.5)
|
|
7
7
|
|
|
8
8
|
GEM
|
|
9
9
|
remote: https://rubygems.org/
|
|
10
10
|
specs:
|
|
11
|
-
|
|
12
|
-
activesupport (= 6.1.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
actionview (6.1.4.1)
|
|
12
|
+
activesupport (= 6.1.4.1)
|
|
13
|
+
builder (~> 3.1)
|
|
14
|
+
erubi (~> 1.4)
|
|
15
|
+
rails-dom-testing (~> 2.0)
|
|
16
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
|
17
|
+
activemodel (6.1.4.1)
|
|
18
|
+
activesupport (= 6.1.4.1)
|
|
19
|
+
activerecord (6.1.4.1)
|
|
20
|
+
activemodel (= 6.1.4.1)
|
|
21
|
+
activesupport (= 6.1.4.1)
|
|
22
|
+
activesupport (6.1.4.1)
|
|
17
23
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
18
24
|
i18n (>= 1.6, < 2)
|
|
19
25
|
minitest (>= 5.1)
|
|
20
26
|
tzinfo (~> 2.0)
|
|
21
27
|
zeitwerk (~> 2.3)
|
|
22
|
-
ast (2.4.
|
|
23
|
-
|
|
28
|
+
ast (2.4.2)
|
|
29
|
+
builder (3.2.4)
|
|
30
|
+
coderay (1.1.3)
|
|
31
|
+
concurrent-ruby (1.1.9)
|
|
32
|
+
crass (1.0.6)
|
|
24
33
|
diff-lcs (1.4.4)
|
|
34
|
+
erubi (1.10.0)
|
|
25
35
|
i18n (1.8.10)
|
|
26
36
|
concurrent-ruby (~> 1.0)
|
|
27
|
-
|
|
37
|
+
kaminari (1.2.1)
|
|
38
|
+
activesupport (>= 4.1.0)
|
|
39
|
+
kaminari-actionview (= 1.2.1)
|
|
40
|
+
kaminari-activerecord (= 1.2.1)
|
|
41
|
+
kaminari-core (= 1.2.1)
|
|
42
|
+
kaminari-actionview (1.2.1)
|
|
43
|
+
actionview
|
|
44
|
+
kaminari-core (= 1.2.1)
|
|
45
|
+
kaminari-activerecord (1.2.1)
|
|
46
|
+
activerecord
|
|
47
|
+
kaminari-core (= 1.2.1)
|
|
48
|
+
kaminari-core (1.2.1)
|
|
49
|
+
lefthook (0.7.6)
|
|
50
|
+
loofah (2.12.0)
|
|
51
|
+
crass (~> 1.0.2)
|
|
52
|
+
nokogiri (>= 1.5.9)
|
|
53
|
+
method_source (1.0.0)
|
|
54
|
+
mini_portile2 (2.6.1)
|
|
28
55
|
minitest (5.14.4)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
56
|
+
nokogiri (1.12.5)
|
|
57
|
+
mini_portile2 (~> 2.6.1)
|
|
58
|
+
racc (~> 1.4)
|
|
59
|
+
pagy (3.14.0)
|
|
60
|
+
parallel (1.21.0)
|
|
61
|
+
parser (3.0.2.0)
|
|
32
62
|
ast (~> 2.4.1)
|
|
63
|
+
pry (0.14.1)
|
|
64
|
+
coderay (~> 1.1)
|
|
65
|
+
method_source (~> 1.0)
|
|
66
|
+
racc (1.5.2)
|
|
67
|
+
rails-dom-testing (2.0.3)
|
|
68
|
+
activesupport (>= 4.2.0)
|
|
69
|
+
nokogiri (>= 1.6)
|
|
70
|
+
rails-html-sanitizer (1.4.2)
|
|
71
|
+
loofah (~> 2.3)
|
|
33
72
|
rainbow (3.0.0)
|
|
34
|
-
rake (13.0.
|
|
35
|
-
regexp_parser (2.
|
|
73
|
+
rake (13.0.6)
|
|
74
|
+
regexp_parser (2.1.1)
|
|
36
75
|
rexml (3.2.5)
|
|
37
76
|
rspec (3.10.0)
|
|
38
77
|
rspec-core (~> 3.10.0)
|
|
@@ -43,48 +82,52 @@ GEM
|
|
|
43
82
|
rspec-expectations (3.10.1)
|
|
44
83
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
45
84
|
rspec-support (~> 3.10.0)
|
|
46
|
-
rspec-mocks (3.10.
|
|
85
|
+
rspec-mocks (3.10.2)
|
|
47
86
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
48
87
|
rspec-support (~> 3.10.0)
|
|
49
|
-
rspec-support (3.10.
|
|
50
|
-
rubocop (1.
|
|
88
|
+
rspec-support (3.10.2)
|
|
89
|
+
rubocop (1.20.0)
|
|
51
90
|
parallel (~> 1.10)
|
|
52
|
-
parser (>=
|
|
91
|
+
parser (>= 3.0.0.0)
|
|
53
92
|
rainbow (>= 2.2.2, < 4.0)
|
|
54
93
|
regexp_parser (>= 1.8, < 3.0)
|
|
55
94
|
rexml
|
|
56
|
-
rubocop-ast (>= 1.
|
|
95
|
+
rubocop-ast (>= 1.9.1, < 2.0)
|
|
57
96
|
ruby-progressbar (~> 1.7)
|
|
58
|
-
unicode-display_width (>= 1.4.0, <
|
|
59
|
-
rubocop-ast (1.
|
|
60
|
-
parser (>=
|
|
61
|
-
rubocop-performance (1.
|
|
62
|
-
rubocop (>=
|
|
97
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
|
98
|
+
rubocop-ast (1.12.0)
|
|
99
|
+
parser (>= 3.0.1.1)
|
|
100
|
+
rubocop-performance (1.11.5)
|
|
101
|
+
rubocop (>= 1.7.0, < 2.0)
|
|
63
102
|
rubocop-ast (>= 0.4.0)
|
|
64
|
-
rubocop-rspec (2.
|
|
65
|
-
rubocop (~> 1.
|
|
66
|
-
rubocop-ast (>= 1.1.0)
|
|
103
|
+
rubocop-rspec (2.5.0)
|
|
104
|
+
rubocop (~> 1.19)
|
|
67
105
|
ruby-progressbar (1.11.0)
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
rubocop
|
|
106
|
+
sqlite3 (1.4.2)
|
|
107
|
+
standard (1.3.0)
|
|
108
|
+
rubocop (= 1.20.0)
|
|
109
|
+
rubocop-performance (= 1.11.5)
|
|
71
110
|
standardrb (1.0.0)
|
|
72
111
|
standard
|
|
73
112
|
tzinfo (2.0.4)
|
|
74
113
|
concurrent-ruby (~> 1.0)
|
|
75
|
-
unicode-display_width (1.
|
|
114
|
+
unicode-display_width (2.1.0)
|
|
76
115
|
zeitwerk (2.4.2)
|
|
77
116
|
|
|
78
117
|
PLATFORMS
|
|
79
118
|
ruby
|
|
80
119
|
|
|
81
120
|
DEPENDENCIES
|
|
121
|
+
activerecord
|
|
82
122
|
bundler (~> 2.0)
|
|
83
123
|
jsonapi-query_builder!
|
|
124
|
+
kaminari (~> 1.2)
|
|
84
125
|
lefthook
|
|
126
|
+
pry
|
|
85
127
|
rake (~> 13.0)
|
|
86
128
|
rspec (~> 3.0)
|
|
87
129
|
rubocop-rspec
|
|
130
|
+
sqlite3
|
|
88
131
|
standard
|
|
89
132
|
standardrb
|
|
90
133
|
|
data/README.md
CHANGED
|
@@ -27,6 +27,9 @@ Or install it yourself as:
|
|
|
27
27
|
|
|
28
28
|
```ruby
|
|
29
29
|
class UserQuery < Jsonapi::QueryBuilder::BaseQuery
|
|
30
|
+
## pagination
|
|
31
|
+
paginator Jsonapi::QueryBuilder::Paginator::Pagy # default paginator
|
|
32
|
+
|
|
30
33
|
## sorting
|
|
31
34
|
default_sort created_at: :desc
|
|
32
35
|
sorts_by :last_name
|
|
@@ -56,6 +59,29 @@ current user permissions, or for any other type of scoping. It's only responsibi
|
|
|
56
59
|
querying. Use `pundit` or similar for policy scoping, custom query objects for other scoping, and then pass the scoped
|
|
57
60
|
collection to the `Jsonapi::QueryBuilder::BaseQuery` object.
|
|
58
61
|
|
|
62
|
+
### Pagination
|
|
63
|
+
Pagination support is configurable using the `paginator` method to define the paginator. It defaults to the `Pagy`
|
|
64
|
+
paginator, a lightweight and fast paginator. Other paginators currently supported are `Kaminari` and an implementation
|
|
65
|
+
of keyset pagination. Before using these paginators we need to explicitly require the gems in our Gemfile and the
|
|
66
|
+
paginator file in question.
|
|
67
|
+
Additionally one can implement it's own paginator by inheriting from `Jsonapi::QueryBuilder::Paginator::BasePaginator`.
|
|
68
|
+
The minimum required implementation is a `#paginate` method that receives page params and returns a page of the
|
|
69
|
+
collection. It can return the pagination details as the second item of the returned array, that can be used in the
|
|
70
|
+
serializer for pagination metadata.
|
|
71
|
+
#### Using the Kaminari Paginator
|
|
72
|
+
```ruby
|
|
73
|
+
require "jsonapi/query_builder/paginator/kaminari"
|
|
74
|
+
|
|
75
|
+
paginator Jsonapi::QueryBuilder::Paginator::Kaminari
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
#### Using the Keyset Paginator
|
|
79
|
+
```ruby
|
|
80
|
+
require "jsonapi/query_builder/paginator/keyset"
|
|
81
|
+
|
|
82
|
+
paginator Jsonapi::QueryBuilder::Paginator::Keyset
|
|
83
|
+
```
|
|
84
|
+
|
|
59
85
|
### Sorting
|
|
60
86
|
#### Ensuring deterministic results
|
|
61
87
|
Sorting has a fallback to an unique attribute which defaults to the `id` attribute. This ensures deterministic paginated
|
|
@@ -48,4 +48,8 @@ Gem::Specification.new do |spec|
|
|
|
48
48
|
spec.add_development_dependency "standard"
|
|
49
49
|
spec.add_development_dependency "rubocop-rspec"
|
|
50
50
|
spec.add_development_dependency "lefthook"
|
|
51
|
+
spec.add_development_dependency "kaminari", "~> 1.2"
|
|
52
|
+
spec.add_development_dependency "activerecord"
|
|
53
|
+
spec.add_development_dependency "sqlite3"
|
|
54
|
+
spec.add_development_dependency "pry"
|
|
51
55
|
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Jsonapi
|
|
4
|
+
module QueryBuilder
|
|
5
|
+
module Errors
|
|
6
|
+
class UnpermittedSortParameters < ArgumentError
|
|
7
|
+
def initialize(unpermitted_parameters)
|
|
8
|
+
super [
|
|
9
|
+
unpermitted_parameters.to_sentence,
|
|
10
|
+
unpermitted_parameters.count == 1 ? "is not a" : "are not",
|
|
11
|
+
"permitted sort attribute".pluralize(unpermitted_parameters.count)
|
|
12
|
+
].join(" ")
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -4,7 +4,20 @@ module Jsonapi
|
|
|
4
4
|
module QueryBuilder
|
|
5
5
|
module Mixins
|
|
6
6
|
module Paginate
|
|
7
|
-
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
class_methods do
|
|
10
|
+
# Sets the paginator used to page the results. Defaults to Pagy
|
|
11
|
+
#
|
|
12
|
+
# @param [Jsonapi::QueryBuilder::Paginator::BasePaginator] paginator A subclass of BasePaginator
|
|
13
|
+
def paginator(paginator)
|
|
14
|
+
@paginator = paginator
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def _paginator
|
|
18
|
+
@paginator || Paginator::Pagy
|
|
19
|
+
end
|
|
20
|
+
end
|
|
8
21
|
|
|
9
22
|
attr_reader :pagination_details
|
|
10
23
|
|
|
@@ -14,9 +27,7 @@ module Jsonapi
|
|
|
14
27
|
# @param [Object] page_params Optional explicit pagination params
|
|
15
28
|
# @return [ActiveRecord::Relation] Paged collection
|
|
16
29
|
def paginate(collection, page_params = send(:page_params))
|
|
17
|
-
@pagination_details
|
|
18
|
-
items: page_params[:size],
|
|
19
|
-
outset: page_params[:offset]
|
|
30
|
+
records, @pagination_details = self.class._paginator.new(collection).paginate(page_params)
|
|
20
31
|
|
|
21
32
|
records
|
|
22
33
|
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "jsonapi/query_builder/mixins/sort/param"
|
|
4
|
+
require "jsonapi/query_builder/errors/unpermitted_sort_parameters"
|
|
4
5
|
|
|
5
6
|
module Jsonapi
|
|
6
7
|
module QueryBuilder
|
|
@@ -8,8 +9,6 @@ module Jsonapi
|
|
|
8
9
|
module Sort
|
|
9
10
|
extend ActiveSupport::Concern
|
|
10
11
|
|
|
11
|
-
UnpermittedSortParameters = Class.new ArgumentError
|
|
12
|
-
|
|
13
12
|
class_methods do
|
|
14
13
|
attr_reader :_default_sort
|
|
15
14
|
|
|
@@ -59,7 +58,7 @@ module Jsonapi
|
|
|
59
58
|
# @param [ActiveRecord::Relation] collection
|
|
60
59
|
# @param [Object] sort_params Optional explicit sort params
|
|
61
60
|
# @return [ActiveRecord::Relation] Sorted relation
|
|
62
|
-
# @raise [Jsonapi::QueryBuilder::
|
|
61
|
+
# @raise [Jsonapi::QueryBuilder::Errors::UnpermittedSortParameters] if not all sort parameters are
|
|
63
62
|
# permitted
|
|
64
63
|
def sort(collection, sort_params = send(:sort_params))
|
|
65
64
|
sort_params = Param.deserialize_params(sort_params)
|
|
@@ -80,11 +79,7 @@ module Jsonapi
|
|
|
80
79
|
unpermitted_parameters = sort_params.map(&:attribute).map(&:to_sym) - self.class.supported_sorts.keys
|
|
81
80
|
return if unpermitted_parameters.size.zero?
|
|
82
81
|
|
|
83
|
-
raise UnpermittedSortParameters,
|
|
84
|
-
unpermitted_parameters.to_sentence,
|
|
85
|
-
unpermitted_parameters.count == 1 ? "is not a" : "are not",
|
|
86
|
-
"permitted sort attribute".pluralize(unpermitted_parameters.count)
|
|
87
|
-
].join(" ")
|
|
82
|
+
raise Errors::UnpermittedSortParameters, unpermitted_parameters
|
|
88
83
|
end
|
|
89
84
|
|
|
90
85
|
def add_order_attributes(collection, sort_params)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Jsonapi
|
|
4
|
+
module QueryBuilder
|
|
5
|
+
module Paginator
|
|
6
|
+
class BasePaginator
|
|
7
|
+
attr_reader :collection
|
|
8
|
+
|
|
9
|
+
# @param [ActiveRecord::Relation] collection
|
|
10
|
+
def initialize(collection)
|
|
11
|
+
@collection = collection
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# @param [Hash] page_params
|
|
15
|
+
# @return [[ActiveRecord::Relation, Hash]] Records and pagination details
|
|
16
|
+
def paginate(page_params)
|
|
17
|
+
raise NotImplementedError, "#{self.class} should implement ##{__method__}"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "kaminari"
|
|
4
|
+
|
|
5
|
+
module Jsonapi
|
|
6
|
+
module QueryBuilder
|
|
7
|
+
module Paginator
|
|
8
|
+
class Kaminari < BasePaginator
|
|
9
|
+
def paginate(page_params)
|
|
10
|
+
paged_collection = collection
|
|
11
|
+
.page(page_params[:number])
|
|
12
|
+
.per(page_params[:size])
|
|
13
|
+
.padding(page_params[:offset])
|
|
14
|
+
|
|
15
|
+
[paged_collection, pagination_details(paged_collection, page_params)]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def pagination_details(collection, page_params)
|
|
21
|
+
{
|
|
22
|
+
number: collection.current_page,
|
|
23
|
+
size: collection.limit_value,
|
|
24
|
+
offset: page_params[:offset],
|
|
25
|
+
total: collection.total_count,
|
|
26
|
+
total_pages: collection.total_pages,
|
|
27
|
+
next_page: collection.next_page,
|
|
28
|
+
prev_page: collection.prev_page
|
|
29
|
+
}
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_record"
|
|
4
|
+
|
|
5
|
+
module Jsonapi
|
|
6
|
+
module QueryBuilder
|
|
7
|
+
module Paginator
|
|
8
|
+
class Keyset < BasePaginator
|
|
9
|
+
DEFAULT_DIRECTION = :after
|
|
10
|
+
DEFAULT_LIMIT = 25
|
|
11
|
+
|
|
12
|
+
def paginate(page_params)
|
|
13
|
+
page_params = extract_pagination_params(page_params)
|
|
14
|
+
records = apply_pagination(collection, page_params)
|
|
15
|
+
|
|
16
|
+
[records, page_params]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def extract_pagination_params(params)
|
|
22
|
+
{
|
|
23
|
+
column: params.fetch(:column, nil),
|
|
24
|
+
position: params.fetch(:position, nil),
|
|
25
|
+
direction: params.fetch(:direction, DEFAULT_DIRECTION),
|
|
26
|
+
limit: params.fetch(:limit, DEFAULT_LIMIT)
|
|
27
|
+
}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def apply_pagination(collection, pagination_params)
|
|
31
|
+
column = pagination_params[:column]
|
|
32
|
+
position = pagination_params[:position]
|
|
33
|
+
direction = pagination_params[:direction]
|
|
34
|
+
limit = pagination_params[:limit]
|
|
35
|
+
|
|
36
|
+
return collection unless column
|
|
37
|
+
|
|
38
|
+
collection = apply_order(collection, column, direction)
|
|
39
|
+
collection = collection.limit(limit.to_i)
|
|
40
|
+
|
|
41
|
+
return collection unless position
|
|
42
|
+
|
|
43
|
+
apply_filter(collection, column, position, direction)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def apply_order(collection, column, direction)
|
|
47
|
+
if direction.to_sym == DEFAULT_DIRECTION
|
|
48
|
+
collection.reorder(collection.arel_table[column].asc)
|
|
49
|
+
else
|
|
50
|
+
collection.reorder(collection.arel_table[column].desc)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def apply_filter(collection, column, position, direction)
|
|
55
|
+
if direction.to_sym == DEFAULT_DIRECTION
|
|
56
|
+
collection.where(collection.arel_table[column].gt(position))
|
|
57
|
+
else
|
|
58
|
+
collection.where(collection.arel_table[column].lt(position))
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pagy"
|
|
4
|
+
require "pagy/extras/items"
|
|
5
|
+
|
|
6
|
+
module Jsonapi
|
|
7
|
+
module QueryBuilder
|
|
8
|
+
module Paginator
|
|
9
|
+
class Pagy < BasePaginator
|
|
10
|
+
include ::Pagy::Backend
|
|
11
|
+
|
|
12
|
+
def paginate(page_params)
|
|
13
|
+
@params = {page: page_params}
|
|
14
|
+
|
|
15
|
+
pagination_details, records = pagy collection, page: page_params[:number],
|
|
16
|
+
items: page_params[:size],
|
|
17
|
+
outset: page_params[:offset]
|
|
18
|
+
[records, pagination_details]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
attr_reader :params
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -4,8 +4,6 @@ require "active_support/concern"
|
|
|
4
4
|
require "active_support/core_ext/array/conversions"
|
|
5
5
|
require "active_support/core_ext/hash/keys"
|
|
6
6
|
require "active_support/core_ext/string/inflections"
|
|
7
|
-
require "pagy"
|
|
8
|
-
require "pagy/extras/items"
|
|
9
7
|
|
|
10
8
|
require "jsonapi/query_builder/version"
|
|
11
9
|
require "jsonapi/query_builder/base_query"
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jsonapi-query_builder
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jure Cindro
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2021-
|
|
11
|
+
date: 2021-09-29 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
@@ -136,6 +136,62 @@ dependencies:
|
|
|
136
136
|
- - ">="
|
|
137
137
|
- !ruby/object:Gem::Version
|
|
138
138
|
version: '0'
|
|
139
|
+
- !ruby/object:Gem::Dependency
|
|
140
|
+
name: kaminari
|
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
|
142
|
+
requirements:
|
|
143
|
+
- - "~>"
|
|
144
|
+
- !ruby/object:Gem::Version
|
|
145
|
+
version: '1.2'
|
|
146
|
+
type: :development
|
|
147
|
+
prerelease: false
|
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
149
|
+
requirements:
|
|
150
|
+
- - "~>"
|
|
151
|
+
- !ruby/object:Gem::Version
|
|
152
|
+
version: '1.2'
|
|
153
|
+
- !ruby/object:Gem::Dependency
|
|
154
|
+
name: activerecord
|
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
|
156
|
+
requirements:
|
|
157
|
+
- - ">="
|
|
158
|
+
- !ruby/object:Gem::Version
|
|
159
|
+
version: '0'
|
|
160
|
+
type: :development
|
|
161
|
+
prerelease: false
|
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
163
|
+
requirements:
|
|
164
|
+
- - ">="
|
|
165
|
+
- !ruby/object:Gem::Version
|
|
166
|
+
version: '0'
|
|
167
|
+
- !ruby/object:Gem::Dependency
|
|
168
|
+
name: sqlite3
|
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
|
170
|
+
requirements:
|
|
171
|
+
- - ">="
|
|
172
|
+
- !ruby/object:Gem::Version
|
|
173
|
+
version: '0'
|
|
174
|
+
type: :development
|
|
175
|
+
prerelease: false
|
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
177
|
+
requirements:
|
|
178
|
+
- - ">="
|
|
179
|
+
- !ruby/object:Gem::Version
|
|
180
|
+
version: '0'
|
|
181
|
+
- !ruby/object:Gem::Dependency
|
|
182
|
+
name: pry
|
|
183
|
+
requirement: !ruby/object:Gem::Requirement
|
|
184
|
+
requirements:
|
|
185
|
+
- - ">="
|
|
186
|
+
- !ruby/object:Gem::Version
|
|
187
|
+
version: '0'
|
|
188
|
+
type: :development
|
|
189
|
+
prerelease: false
|
|
190
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
191
|
+
requirements:
|
|
192
|
+
- - ">="
|
|
193
|
+
- !ruby/object:Gem::Version
|
|
194
|
+
version: '0'
|
|
139
195
|
description: |
|
|
140
196
|
`Jsonapi::QueryBuilder` serves the purpose of adding the json api query related SQL conditions to the already scoped collection, usually used in controller index actions.
|
|
141
197
|
|
|
@@ -166,11 +222,17 @@ files:
|
|
|
166
222
|
- lib/jsonapi/query_builder/base_filter.rb
|
|
167
223
|
- lib/jsonapi/query_builder/base_query.rb
|
|
168
224
|
- lib/jsonapi/query_builder/base_sort.rb
|
|
225
|
+
- lib/jsonapi/query_builder/errors/unpermitted_sort_parameters.rb
|
|
169
226
|
- lib/jsonapi/query_builder/mixins/filter.rb
|
|
170
227
|
- lib/jsonapi/query_builder/mixins/include.rb
|
|
171
228
|
- lib/jsonapi/query_builder/mixins/paginate.rb
|
|
172
229
|
- lib/jsonapi/query_builder/mixins/sort.rb
|
|
173
230
|
- lib/jsonapi/query_builder/mixins/sort/param.rb
|
|
231
|
+
- lib/jsonapi/query_builder/paginator.rb
|
|
232
|
+
- lib/jsonapi/query_builder/paginator/base_paginator.rb
|
|
233
|
+
- lib/jsonapi/query_builder/paginator/kaminari.rb
|
|
234
|
+
- lib/jsonapi/query_builder/paginator/keyset.rb
|
|
235
|
+
- lib/jsonapi/query_builder/paginator/pagy.rb
|
|
174
236
|
- lib/jsonapi/query_builder/version.rb
|
|
175
237
|
homepage: https://github.com/infinum/jsonapi-query_builder
|
|
176
238
|
licenses:
|