grape-listing 2.0.0.pre.beta.8 → 2.0.0.pre.beta.10
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/README.md +0 -2
- data/lib/grape/dsl.rb +4 -4
- data/lib/grape_listing/version.rb +1 -1
- data/lib/listing_service/args_handling.rb +2 -4
- data/lib/listing_service/search.rb +40 -9
- 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: c8c99132363e73e0952c34a8da86c22115d3006ffc1e2116fb533984df91fb7c
|
4
|
+
data.tar.gz: 8489a9c06caf7462ff7e1485763b7b05f848003d0d4f6d3f3c90448c9ace66b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 76787d2cc277f638184b433549a1e56583e1c1d43ec764d10ebc02f8facd0e8fec7fb358fd883666c90f402760331b94b0788a3ea2dc18ed492ba1e65412962e
|
7
|
+
data.tar.gz: 90aa15979be17180abc11fb4519ab9fbaa6de7c89e90c188ead5a5cf5cdcd4cff6f16391131c1c9160f311706067bc5d8c11d0f41e5088a70d6ec949840693b7
|
data/README.md
CHANGED
@@ -43,8 +43,6 @@ end
|
|
43
43
|
|
44
44
|
`fields: %i[...]` - список полей, которые должны присутствовать в списке записей (передается вместо `entity`)
|
45
45
|
|
46
|
-
`custom_search: %w[...]` - список параметров, по которым должна осуществляться специальная фильтрация с применением индивидуальных Rails-скоупов
|
47
|
-
|
48
46
|
`custom_sort: %w[...]` - список параметров, по которым может осуществляться специальная сортировка с применением индивидуальных Rails-скоупов
|
49
47
|
|
50
48
|
`paginate: false` - отдавать результат без пагинации, сразу всей коллекцией (по умолчанию - с пагинацией)
|
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: nil, fields: nil, scopes: nil,
|
7
|
+
def listing(model:, entity: nil, fields: nil, scopes: nil, search_joins: nil, custom_sort: nil, paginate: true, cache: nil, preload: nil)
|
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, fields, scopes,
|
13
|
+
opts = listing_opts(model, entity, fields, scopes, search_joins, custom_sort, cache, request_method, request_uri, preload)
|
14
14
|
|
15
15
|
if params[:spreadsheet]
|
16
16
|
listing_spreadsheet(**opts)
|
@@ -23,7 +23,7 @@ module Grape
|
|
23
23
|
|
24
24
|
private
|
25
25
|
|
26
|
-
def listing_opts(model, entity, fields, scopes,
|
26
|
+
def listing_opts(model, entity, fields, scopes, search_joins, custom_sort, cache, request_method, request_uri, preload)
|
27
27
|
# стандартные опции
|
28
28
|
opts = {
|
29
29
|
model:,
|
@@ -31,7 +31,7 @@ module Grape
|
|
31
31
|
entity:,
|
32
32
|
fields:,
|
33
33
|
scopes:,
|
34
|
-
|
34
|
+
search_joins:,
|
35
35
|
custom_sort:,
|
36
36
|
params:,
|
37
37
|
current_user:,
|
@@ -22,10 +22,8 @@ module GrapeListing
|
|
22
22
|
# поля, по которым возможен поиск
|
23
23
|
@search_params = @params.except('offset', 'limit', 'sort_by', 'sort_order')
|
24
24
|
|
25
|
-
# настройки кастомного поиска
|
26
|
-
|
27
|
-
@custom_search = args[:custom_search].map { |i| i.split('|') }.to_h
|
28
|
-
end
|
25
|
+
# настройки джойнов для кастомного поиска
|
26
|
+
@search_joins = args[:search_joins]
|
29
27
|
|
30
28
|
# настройки кастомной сортировки
|
31
29
|
if args[:custom_sort]
|
@@ -29,16 +29,25 @@ module GrapeListing
|
|
29
29
|
column, condition, operator = param.split('.')
|
30
30
|
next unless [column, condition, operator].all?
|
31
31
|
|
32
|
-
# применение
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
# применение джойнов кастомной фильтрации (при наличии)
|
33
|
+
search_joins = @search_joins[column.to_sym] if @search_joins
|
34
|
+
|
35
|
+
if search_joins
|
36
|
+
# условие для поиска по массиву ИД
|
37
|
+
column = 'id' if column.ends_with?('_ids')
|
38
|
+
|
39
|
+
last_join_table = determine_query_join(search_joins).to_s.pluralize
|
40
|
+
last_join_model = last_join_table.to_s.classify.constantize
|
38
41
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
+
subject = "#{last_join_table}.#{column}"
|
43
|
+
type = last_join_model.columns_hash[column].type
|
44
|
+
|
45
|
+
list = list.joins(search_joins)
|
46
|
+
else
|
47
|
+
subject = "#{@model.table_name}.#{column}"
|
48
|
+
type = @model_cols[column]&.type
|
49
|
+
end
|
50
|
+
|
42
51
|
next unless type
|
43
52
|
|
44
53
|
clause = case type
|
@@ -59,6 +68,17 @@ module GrapeListing
|
|
59
68
|
end
|
60
69
|
end
|
61
70
|
|
71
|
+
def determine_query_join(joins)
|
72
|
+
return joins if joins.is_a?(Symbol)
|
73
|
+
|
74
|
+
result = joins.to_a.flatten.last
|
75
|
+
if result.is_a?(Symbol)
|
76
|
+
result
|
77
|
+
else
|
78
|
+
determine_query_join(result)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
62
82
|
def string_clause(subject, condition, operator, value)
|
63
83
|
case operator
|
64
84
|
when PRESENT
|
@@ -125,6 +145,8 @@ module GrapeListing
|
|
125
145
|
|
126
146
|
def enum_clause(subject, condition, operator, value)
|
127
147
|
case operator
|
148
|
+
when PRESENT
|
149
|
+
enum_present(subject, condition)
|
128
150
|
when EQUAL
|
129
151
|
enum_equal(subject, condition, value)
|
130
152
|
when IN
|
@@ -324,6 +346,15 @@ module GrapeListing
|
|
324
346
|
end
|
325
347
|
end
|
326
348
|
|
349
|
+
def enum_present(subject, condition)
|
350
|
+
case condition
|
351
|
+
when IS
|
352
|
+
"#{subject} IS NOT NULL"
|
353
|
+
when NOT
|
354
|
+
"#{subject} IS NULL"
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
327
358
|
def enum_equal(subject, condition, value)
|
328
359
|
case condition
|
329
360
|
when IS
|
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: 2.0.0.pre.beta.
|
4
|
+
version: 2.0.0.pre.beta.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Павел Бабин
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-06-
|
11
|
+
date: 2025-06-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|