grape-listing 2.0.0.pre.beta.7 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ca3e6198932bcf46af81e4ab587e535d998769aeb1aa484adb95d901500cca4
4
- data.tar.gz: 395bf8eb79c15863fce8d6347fec18b4f2cf1d8e9acb88cb61740818e23e6280
3
+ metadata.gz: 3362800e5ff0d7e9495b7b5b6fd6222714604e70c9fc7f9a875635d747ff4339
4
+ data.tar.gz: ff3bc2c8c00bdf59d3b3d6d6fa966f63317fdb0e8749067657720cc8cd10d6d2
5
5
  SHA512:
6
- metadata.gz: d6f3bc7b5805ac6ad44ba6c307e1dccb37fd7229b2f031e5fc372530d26ed84162745d5910e10e9b15c0b34ebeec283aac5f29877a09a702d6b004306305f504
7
- data.tar.gz: 8a00e955bdffdb46d73b77636865269152516d98a03439b6b6263dc45c77ebee7bb120cb1a0fbd3af086ee63a9387387ae07211665ca9d27115b4cc7d72a7c54
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, 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.7'.freeze
2
+ VERSION = '2.0.0-beta.9'.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,26 @@ 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
- type = @model_cols[column].type
41
- subject = "#{@model.table_name}.#{column}"
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
+
51
+ next unless type
42
52
 
43
53
  clause = case type
44
54
  when :string, :text
@@ -58,6 +68,17 @@ module GrapeListing
58
68
  end
59
69
  end
60
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
+
61
82
  def string_clause(subject, condition, operator, value)
62
83
  case operator
63
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.7
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-05 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