grape-listing 1.2.2 → 1.3.2
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: 91c1e4c2304b5a8918099f9d66fe17adb0867d70dc3e31c4e1d8ddd4f4763bb2
|
4
|
+
data.tar.gz: 43c297e7791da7cc815f893f1ed8e920047799f35633f4facf174911874c6c88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 38e71d0a6b5b79d13923c4e1950fd6fe9d51732eb57fcf7efe87546a473b93be40994f131beeadd7db0d24ed5b77612997676e472e0e993ccac4d0056f7891a3
|
7
|
+
data.tar.gz: b24ea490f428720e199ffb8029bcfc96abd0f13d471dbc68c8125fe6455b14f19be3d17a7040f7b2cf488199040961586f55ffc258b6a0d57b7469c9b03a643c
|
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'] }[0]
|
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.2
|
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-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|