fetcheable_on_api 0.2.4 → 0.2.5
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 +55 -0
- data/lib/fetcheable_on_api/sortable.rb +11 -8
- 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: c6219549e8a0f5504952d5ef35cefff9cc5e4fecc5a0999d63e9d27f5af9f0e6
|
4
|
+
data.tar.gz: 9ccff3a7b58b450aa033659bd5ad5378541c204340d57aa09433edcca08c1b12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec44cfa6a0ed5e2d0993b7e4527113c65a79386ed3e7f5047990a7d4e4c01cd3c8205451319fe3398056c8b566222973e52f0d4f15dc9b1d6f54c7c918131720
|
7
|
+
data.tar.gz: 56439e23285b03eb1218cb5538f798efda9e4317a324c052ce7e9d82a56b9bc5968b26d4a7111222a75e985fb89773fa1f4cc3443226a40cdad2973877bbda2b
|
data/README.md
CHANGED
@@ -152,6 +152,7 @@ class QuestionsController < ActionController::Base
|
|
152
152
|
end
|
153
153
|
end
|
154
154
|
```
|
155
|
+
|
155
156
|
This allows you to pass a new parameter in the query:
|
156
157
|
|
157
158
|
```bash
|
@@ -184,6 +185,7 @@ $ curl -X GET \
|
|
184
185
|
```
|
185
186
|
|
186
187
|
FetcheableOnApi support multiple sort fields by allowing comma-separated (U+002C COMMA, “,”) sort fields:
|
188
|
+
|
187
189
|
```ruby
|
188
190
|
class QuestionsController < ActionController::Base
|
189
191
|
#
|
@@ -308,6 +310,59 @@ $ curl -X GET \
|
|
308
310
|
]
|
309
311
|
```
|
310
312
|
|
313
|
+
Furthermore you can sort on `lowered` attributes using the `:lower` option:
|
314
|
+
|
315
|
+
```ruby
|
316
|
+
class QuestionsController < ActionController::Base
|
317
|
+
#
|
318
|
+
# FetcheableOnApi
|
319
|
+
#
|
320
|
+
sort_by :answer, lower: true
|
321
|
+
|
322
|
+
# GET /questions
|
323
|
+
def index
|
324
|
+
questions = apply_fetcheable(Question.joins(:answer).includes(:answer).all)
|
325
|
+
render json: questions
|
326
|
+
end
|
327
|
+
end
|
328
|
+
```
|
329
|
+
|
330
|
+
```bash
|
331
|
+
$ curl -X GET \
|
332
|
+
'http://localhost:3000/questions?sort=position'
|
333
|
+
|
334
|
+
[
|
335
|
+
{
|
336
|
+
"id": 3,
|
337
|
+
"position": 1,
|
338
|
+
"category_id": 1,
|
339
|
+
"content": "How to simply sort a collection with this gem ?",
|
340
|
+
"answer": "Just add sort_by in your controller and call the apply_fetcheable method"
|
341
|
+
},
|
342
|
+
{
|
343
|
+
"id": 6,
|
344
|
+
"position": 4,
|
345
|
+
"category_id": 1,
|
346
|
+
"content": "Why am I here?",
|
347
|
+
"answer": "just to demonstrate lowered sort",
|
348
|
+
},
|
349
|
+
{
|
350
|
+
"id": 4,
|
351
|
+
"position": 2,
|
352
|
+
"category_id": 2,
|
353
|
+
"content": "Is it so simple?",
|
354
|
+
"answer": "Yes"
|
355
|
+
},
|
356
|
+
{
|
357
|
+
"id": 5,
|
358
|
+
"position": 3,
|
359
|
+
"category_id": 2,
|
360
|
+
"content": "Is this real life?",
|
361
|
+
"answer": "Yes this is real life"
|
362
|
+
}
|
363
|
+
]
|
364
|
+
```
|
365
|
+
|
311
366
|
### Pagination
|
312
367
|
|
313
368
|
Pagination is automatically set on the controller and allows the use of a new parameter `page`.
|
@@ -7,8 +7,8 @@ module FetcheableOnApi
|
|
7
7
|
# Supports
|
8
8
|
#
|
9
9
|
SORT_ORDER = {
|
10
|
-
|
11
|
-
|
10
|
+
"+" => :asc,
|
11
|
+
"-" => :desc,
|
12
12
|
}.freeze
|
13
13
|
|
14
14
|
#
|
@@ -32,7 +32,7 @@ module FetcheableOnApi
|
|
32
32
|
|
33
33
|
attrs.each do |attr|
|
34
34
|
sorts_configuration[attr] ||= {
|
35
|
-
as: attr
|
35
|
+
as: attr,
|
36
36
|
}
|
37
37
|
|
38
38
|
sorts_configuration[attr] = sorts_configuration[attr].merge(options)
|
@@ -70,7 +70,10 @@ module FetcheableOnApi
|
|
70
70
|
field = field_for(attr_name)
|
71
71
|
return unless belong_to_attributes_for?(klass, field)
|
72
72
|
|
73
|
-
klass.arel_table[field]
|
73
|
+
attribute = klass.arel_table[field]
|
74
|
+
attribute = attribute.lower if sorts_configuration[attr_name].fetch(:lower, false)
|
75
|
+
|
76
|
+
attribute.send(sort_method)
|
74
77
|
end
|
75
78
|
|
76
79
|
def class_for(attr_name, collection)
|
@@ -92,11 +95,11 @@ module FetcheableOnApi
|
|
92
95
|
def format_params(params)
|
93
96
|
res = {}
|
94
97
|
params
|
95
|
-
.split(
|
98
|
+
.split(",")
|
96
99
|
.each do |attribute|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
+
sort_sign = attribute =~ /\A[+-]/ ? attribute.slice!(0) : "+"
|
101
|
+
res[attribute.to_sym] = SORT_ORDER[sort_sign]
|
102
|
+
end
|
100
103
|
res
|
101
104
|
end
|
102
105
|
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.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fabien
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01-
|
11
|
+
date: 2019-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|