grape-listing 2.0.0.pre.beta.8 → 2.0.0.pre.beta.9
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 +29 -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: 3362800e5ff0d7e9495b7b5b6fd6222714604e70c9fc7f9a875635d747ff4339
|
4
|
+
data.tar.gz: ff3bc2c8c00bdf59d3b3d6d6fa966f63317fdb0e8749067657720cc8cd10d6d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65ac74a633107849305c0b7a2885ed2958160c28fea93fef7c6163281edbed821a340852ab16ddb415e0d68b814109674fde653547f59b82e84aeb683575b7ef
|
7
|
+
data.tar.gz: c656e4470f98ded4e75820c67ec85e4cf160e88634a2b48bbb1cf8a573fd7792514c77b05a47e8e42a84384d95787f8d8b096ac21dbbe68b24b7f95440dc4690
|
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
|
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.9
|
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
|