grape-listing 1.2.2 → 1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/grape/dsl.rb +15 -4
- data/lib/listing_service/args_handling.rb +1 -2
- data/lib/listing_service/sorting.rb +19 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 753ca42cbbc590322ae50cc4deb2319abbf1ed42971f3465d47fd437ae59426b
|
4
|
+
data.tar.gz: 8340c60d43bfaf42cd28507f78797802a2a02d59c5520ff538b20f5f2573a8d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61b65a31634ecd111fa68f0769c80fc2668e9d80b8f61e262fc4f989e499232af1bdeacfe1c6abc711b81bf92f43c9c2630242b8c3229395c68c7a8b40e3c7f1
|
7
|
+
data.tar.gz: e102f06d75c44ddc18b657cefe265f0d9fc6c9cff86640352be57079c1af8032a14bed7c3481e3418fe1e439af48b0ee549bd57bfa4ab43e2dee2bea67532e3f
|
data/README.md
CHANGED
@@ -43,6 +43,8 @@ end
|
|
43
43
|
|
44
44
|
`search: %w[...]` - список полей, по которым должна осуществляться фильтрация (поиск).
|
45
45
|
|
46
|
+
`sortable: %w[...]` - список полей, по которым может осуществляться сортировка.
|
47
|
+
|
46
48
|
`paginate: false` - отдавать результат без пагинации, сразу всей коллекцией (по умолчанию - с пагинацией)
|
47
49
|
|
48
50
|
## Параметры HTTP запроса
|
data/lib/grape/dsl.rb
CHANGED
@@ -4,13 +4,13 @@ module Grape
|
|
4
4
|
module DSL
|
5
5
|
module InsideRoute
|
6
6
|
|
7
|
-
def listing(model:, entity:, scopes: nil, search: nil, paginate: true, caching: false)
|
7
|
+
def listing(model:, entity:, scopes: nil, search: nil, sortable: nil, paginate: true, caching: false)
|
8
8
|
# параметры запроса API
|
9
9
|
request_method = request.env['REQUEST_METHOD']
|
10
10
|
request_uri = request.env['REQUEST_URI']
|
11
11
|
|
12
12
|
# опции для сервиса
|
13
|
-
opts = listing_opts(model, entity, scopes, search, caching, request_method, request_uri)
|
13
|
+
opts = listing_opts(model, entity, scopes, search, sortable, caching, request_method, request_uri)
|
14
14
|
|
15
15
|
if params[:spreadsheet]
|
16
16
|
listing_spreadsheet(**opts)
|
@@ -23,9 +23,20 @@ module Grape
|
|
23
23
|
|
24
24
|
private
|
25
25
|
|
26
|
-
def listing_opts(model, entity, scopes, search, caching, request_method, request_uri)
|
26
|
+
def listing_opts(model, entity, scopes, search, sortable, caching, request_method, request_uri)
|
27
27
|
# стандартные опции
|
28
|
-
opts = {
|
28
|
+
opts = {
|
29
|
+
model:,
|
30
|
+
entity:,
|
31
|
+
scopes:,
|
32
|
+
search:,
|
33
|
+
sortable:,
|
34
|
+
params:,
|
35
|
+
current_user:,
|
36
|
+
caching:,
|
37
|
+
request_method:,
|
38
|
+
request_uri:
|
39
|
+
}
|
29
40
|
|
30
41
|
# требуемый список полей (+стандартные) для фильтрации в Grape Entity
|
31
42
|
if params[:columns]
|
@@ -38,8 +38,7 @@ module GrapeListing
|
|
38
38
|
@only_columns = args[:only_columns]
|
39
39
|
|
40
40
|
# сортировка
|
41
|
-
@
|
42
|
-
@sort_order = @params['sort_order'] || :desc
|
41
|
+
@sortable = args[:sortable]
|
43
42
|
|
44
43
|
# подсчет кол-ва записей
|
45
44
|
@objects_count = records_count
|
@@ -4,8 +4,25 @@ module GrapeListing
|
|
4
4
|
private
|
5
5
|
|
6
6
|
def sort_proc
|
7
|
-
|
8
|
-
|
7
|
+
# проверка необходимости нестандартной сортировки
|
8
|
+
unless custom_sorting_field
|
9
|
+
return proc { order('id DESC NULLS LAST') }
|
10
|
+
end
|
11
|
+
|
12
|
+
sort_order = @params['sort_order'] || 'DESC'
|
13
|
+
sorting_scope = custom_sorting_field.split('|')[1]
|
14
|
+
|
15
|
+
if sorting_scope
|
16
|
+
proc { send(sorting_scope, sort_order) }
|
17
|
+
else
|
18
|
+
query = Arel.sql("#{custom_sorting_field} #{sort_order} NULLS LAST")
|
19
|
+
proc { order(query) }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# поиск поля для индивидуальной сортировки среди допустимых
|
24
|
+
def custom_sorting_field
|
25
|
+
@custom_sorting_field ||= @sortable&.select { |i| i.split('|')[0] == @params['sort_by'] }&.first
|
9
26
|
end
|
10
27
|
|
11
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-listing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Павел Бабин
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-10-
|
11
|
+
date: 2024-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|