croods 0.3.0 → 0.3.1

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: abd8dafdfb10c2b936a894dcb6da1057c6bfe3646f6e6d5af0f58d59d0be7bab
4
- data.tar.gz: 8da5a9b8c82bacefae7d736610d7ba4e0e3305ecf832a602dd61cff0edc1b53c
3
+ metadata.gz: b9fa939ee66088adb051cc63ee6f9cfc1206da7674dd6f2c48c524ea0ca8b931
4
+ data.tar.gz: 9e9722e961a8b24760106965a385a5aad1a89d9b85369243d3497c2c3fc33784
5
5
  SHA512:
6
- metadata.gz: deac1ff6c8763d738b8ccd20149632df9be2425690a1550d57172e3a922a1b685f484d848f39fa5f3a8a29b3bd596c4c8c27e6f7356dbd4d56d40ff0abf879a2
7
- data.tar.gz: 7a7342dba26327dc7e1422b26adf56b08747a20a3062d1c1090b941c6d3cf4030f2953975fceb770fb1127cc15097e7398ee35ec0ca017c1a4a6b18ba3a78a4a
6
+ metadata.gz: 65565603371e3e5d46d3c8f412af157bd4e50ca2c1f6afc57a3577f4dd77f294bd63624c7f02585973214ff978d7312367d520916f7497c4f29f49f582b28f1b
7
+ data.tar.gz: 3d0f2872653a07a3edb49cdb3cc368e34eb153826a5066099ed250400000613ea128bbbf5dac4cc3563fffe87f67b94ac82d2854a8a30004010d4f6d200b9be5
@@ -13,11 +13,17 @@ module Croods
13
13
  paginate(list.send(resource.page_method_name, page_param))
14
14
  end
15
15
 
16
- def collection
17
- list = resource
18
- .apply_filters(policy_scope(model), params)
19
- .order(resource.sort_by)
16
+ def sort(list)
17
+ if list.respond_to? resource.sort_by
18
+ list.public_send(resource.sort_by, params[:order_by], params[:order])
19
+ else
20
+ list.order(resource.sort_by)
21
+ end
22
+ end
20
23
 
24
+ def collection
25
+ list = resource.apply_filters(policy_scope(model), params)
26
+ list = sort(list)
21
27
  list = paginate_collection(list) if page_param.present?
22
28
 
23
29
  list
@@ -36,7 +36,9 @@ module Croods
36
36
  def properties(resource)
37
37
  properties = {}
38
38
 
39
- resource.pagination_params.each do |attribute|
39
+ params = resource.pagination_params + resource.order_params
40
+
41
+ params.each do |attribute|
40
42
  properties[attribute.name] = definition(attribute)
41
43
  end
42
44
 
@@ -3,10 +3,26 @@
3
3
  module Croods
4
4
  module Resource
5
5
  module Sorting
6
+ def order_by_attribute
7
+ @order_by_attribute ||= Croods::Attribute.new(
8
+ 'order_by', :string, null: true
9
+ )
10
+ end
11
+
12
+ def order_attribute
13
+ @order_attribute ||= Croods::Attribute.new(
14
+ 'order', :string, null: true
15
+ )
16
+ end
17
+
18
+ def order_params
19
+ @order_params ||= [order_by_attribute, order_attribute]
20
+ end
21
+
6
22
  def sort_by(attribute = nil)
7
23
  return @sort_by ||= :created_at unless attribute
8
24
 
9
- @sort_by = attribute
25
+ @sort_by ||= attribute
10
26
  end
11
27
  end
12
28
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Croods
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: croods
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Weinmann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-13 00:00:00.000000000 Z
11
+ date: 2020-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: api-pagination