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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1aecce440021adb14b7cae95dbf624f862d91d0e97c0ffe36553e21ac46615a4
4
- data.tar.gz: 2b4dc32c99bea47eb78c0a51ec2c8c57b4eafb4c5b0af93a04510d5ddb8339dd
3
+ metadata.gz: c8c99132363e73e0952c34a8da86c22115d3006ffc1e2116fb533984df91fb7c
4
+ data.tar.gz: 8489a9c06caf7462ff7e1485763b7b05f848003d0d4f6d3f3c90448c9ace66b2
5
5
  SHA512:
6
- metadata.gz: 6f1f9ba541c2086ec7c411d7761e75f7d70e4c7fbc01940b9fb208f5f75bc769099411a45e9f0893f6b7a9e3ce8e7d2c51eb4d323bf441da818f5d049480202f
7
- data.tar.gz: d5084ba6e1b009ca633dcbb8478441e9ce0df6248dd5cf02cb01a7d6d8455e3f8fc68122d51b8084e0852fc60ec9f67b995a4b6907f2a32de2c79f45910acd94
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, custom_search: nil, custom_sort: nil, paginate: true, cache: nil, preload: 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, custom_search, custom_sort, cache, request_method, request_uri, preload)
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, custom_search, custom_sort, cache, request_method, request_uri, preload)
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
- custom_search:,
34
+ search_joins:,
35
35
  custom_sort:,
36
36
  params:,
37
37
  current_user:,
@@ -1,3 +1,3 @@
1
1
  module GrapeListing
2
- VERSION = '2.0.0-beta.8'.freeze
2
+ VERSION = '2.0.0-beta.10'.freeze
3
3
  end
@@ -22,10 +22,8 @@ module GrapeListing
22
22
  # поля, по которым возможен поиск
23
23
  @search_params = @params.except('offset', 'limit', 'sort_by', 'sort_order')
24
24
 
25
- # настройки кастомного поиска
26
- if args[:custom_search]
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
- custom_scope = @custom_search[column] if @custom_search
34
- if custom_scope
35
- @objects = list.send(custom_scope, value)
36
- next
37
- end
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
- subject = "#{@model.table_name}.#{column}"
41
- type = @model_cols[column]&.type
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.8
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-06 00:00:00.000000000 Z
11
+ date: 2025-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack