fetcheable_on_api 0.1.9.0 → 0.2
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 +49 -0
- data/lib/fetcheable_on_api/filtreable.rb +3 -2
- data/lib/fetcheable_on_api/sortable.rb +17 -12
- data/lib/fetcheable_on_api/version.rb +1 -1
- 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: c9c8bca94ebf7b39b0dfee1fc94b7322afbbe19959712f67c0b1785c07944ff9
|
4
|
+
data.tar.gz: b0b9b05f0f4071c051c469e7092d1d17f7d216373c81ad13d29d73026cdd261c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d9ecd3bad835fca0821ed277b808c400b3f3c8b6b93661d43c031e4d11063a3d67f34c9194ec0c4a88121cf1f8f502f5c104770b703d409147b50e7f2ece24b
|
7
|
+
data.tar.gz: d61db97cdf938aefcbfe31d6ca491a15bdd68ed50b5bcfcdb995abc198239f0d5b0ca0a437d64bf534269a767977b315a30693b6f612da8b6099e8f2b7401a31
|
data/README.md
CHANGED
@@ -259,6 +259,55 @@ $ curl -X GET \
|
|
259
259
|
]
|
260
260
|
```
|
261
261
|
|
262
|
+
You can also sort through an association like this:
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
class QuestionsController < ActionController::Base
|
266
|
+
#
|
267
|
+
# FetcheableOnApi
|
268
|
+
#
|
269
|
+
sort_by :position, :id
|
270
|
+
sort_by :answer,
|
271
|
+
class_name: Answer,
|
272
|
+
as: 'content'
|
273
|
+
|
274
|
+
# GET /questions
|
275
|
+
def index
|
276
|
+
questions = apply_fetcheable(Question.joins(:answer).includes(:answer).all)
|
277
|
+
render json: questions
|
278
|
+
end
|
279
|
+
end
|
280
|
+
```
|
281
|
+
|
282
|
+
```bash
|
283
|
+
$ curl -X GET \
|
284
|
+
'http://localhost:3000/questions?sort=answer'
|
285
|
+
|
286
|
+
[
|
287
|
+
{
|
288
|
+
"id": 3,
|
289
|
+
"position": 1,
|
290
|
+
"category_id": 1,
|
291
|
+
"content": "How to simply sort a collection with this gem ?",
|
292
|
+
"answer": "Just add sort_by in your controller and call the apply_fetcheable method"
|
293
|
+
},
|
294
|
+
{
|
295
|
+
"id": 4,
|
296
|
+
"position": 2,
|
297
|
+
"category_id": 2,
|
298
|
+
"content": "Is it so simple?",
|
299
|
+
"answer": "Yes"
|
300
|
+
},
|
301
|
+
{
|
302
|
+
"id": 5,
|
303
|
+
"position": 3,
|
304
|
+
"category_id": 2,
|
305
|
+
"content": "Is this real life?",
|
306
|
+
"answer": "Yes this is real life"
|
307
|
+
}
|
308
|
+
]
|
309
|
+
```
|
310
|
+
|
262
311
|
### Pagination
|
263
312
|
|
264
313
|
Pagination is automatically set on the controller and allows the use of a new parameter `page`.
|
@@ -82,9 +82,10 @@ module FetcheableOnApi
|
|
82
82
|
def valid_keys
|
83
83
|
keys = filters_configuration.keys
|
84
84
|
keys.each_with_index do |key, index|
|
85
|
-
predicate = filters_configuration[key.to_sym].fetch(:with, :ilike)
|
85
|
+
predicate = filters_configuration[key.to_sym].fetch(:with, :ilike)
|
86
|
+
next if predicate.respond_to?(:call) || PREDICATES_WITH_ARRAY.exclude?(predicate.to_sym)
|
86
87
|
|
87
|
-
keys[index] = { key => [] }
|
88
|
+
keys[index] = { key => [] }
|
88
89
|
end
|
89
90
|
|
90
91
|
keys
|
@@ -51,23 +51,28 @@ module FetcheableOnApi
|
|
51
51
|
return collection if params[:sort].blank?
|
52
52
|
foa_valid_parameters!(:sort, foa_permitted_types: [String])
|
53
53
|
|
54
|
-
ordering
|
55
|
-
sorted_params = params[:sort].split(',')
|
54
|
+
ordering = []
|
56
55
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
if klass.attribute_names.include?(attr)
|
62
|
-
ordering[attr] = SORT_ORDER[sort_sign]
|
63
|
-
end
|
64
|
-
end
|
56
|
+
clean_params(params[:sort]).each do |attr|
|
57
|
+
klass = sorts_configuration[attr.to_sym].fetch(:class_name, collection.klass)
|
58
|
+
field = sorts_configuration[attr.to_sym].fetch(:as, attr.to_sym)
|
59
|
+
next unless klass.attribute_names.include?(field)
|
65
60
|
|
66
|
-
|
67
|
-
|
61
|
+
sort_sign = (attr =~ /\A[+-]/) ? attr.slice!(0) : '+'
|
62
|
+
ordering << klass
|
63
|
+
.arel_table[field.to_sym]
|
64
|
+
.send(SORT_ORDER[sort_sign])
|
68
65
|
end
|
69
66
|
|
70
67
|
collection.order(ordering)
|
71
68
|
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def clean_params(params)
|
73
|
+
params
|
74
|
+
.split(',')
|
75
|
+
.select { |e| sorts_configuration.keys.map(&:to_s).include?(e) }
|
76
|
+
end
|
72
77
|
end
|
73
78
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fetcheable_on_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fabien
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|