grape-listing 1.2.2 → 1.3.3

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: 93c908d060e847beb9d2919d16e2d631168c97181c0522e008ac6cf2ea9b9957
4
- data.tar.gz: c06c939b8ae46d8b900b3e652488b281c5858ea8abc6b7b6265e2c40000ce0a8
3
+ metadata.gz: 753ca42cbbc590322ae50cc4deb2319abbf1ed42971f3465d47fd437ae59426b
4
+ data.tar.gz: 8340c60d43bfaf42cd28507f78797802a2a02d59c5520ff538b20f5f2573a8d7
5
5
  SHA512:
6
- metadata.gz: f43ec4d90177802e01106f55b0e589a663c2f1013c176154f17a9bc5ef57fb57566b915ffc63c2c65aa324c154a8cad8d21c0da740d29811a32cd76a72d76e14
7
- data.tar.gz: 82c8fd75f4eb8405a1706e03c3ce28cb8ae4456787b910aad3b423d4a705002c74165212adb11edf2d83344be92a7c6cbda37e1346f081e9db607931ba80b045
6
+ metadata.gz: 61b65a31634ecd111fa68f0769c80fc2668e9d80b8f61e262fc4f989e499232af1bdeacfe1c6abc711b81bf92f43c9c2630242b8c3229395c68c7a8b40e3c7f1
7
+ data.tar.gz: e102f06d75c44ddc18b657cefe265f0d9fc6c9cff86640352be57079c1af8032a14bed7c3481e3418fe1e439af48b0ee549bd57bfa4ab43e2dee2bea67532e3f
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 = { model:, entity:, scopes:, search:, params:, current_user:, caching:, request_method:, request_uri: }
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
- @sort_by = @params['sort_by'] || :id
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
- query = Arel.sql("#{@sort_by} #{@sort_order} NULLS LAST")
8
- proc { order(query) }
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'] }&.first
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.2.2
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Павел Бабин
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-07 00:00:00.000000000 Z
11
+ date: 2024-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack