fetcheable_on_api 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -0
- data/lib/fetcheable_on_api/filtreable.rb +7 -5
- data/lib/fetcheable_on_api/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f28d2fc702708ed0bec47b13b9d4573f0d817c5
|
4
|
+
data.tar.gz: 4a33d75b625c595516143e00088a6b5374cb1b85
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b64b0d3dbc5cf98d933e4b4187c86ab91ed9e35fe06d13450973a364cb07ffbbffd4ec5d319713de04ffde8041ebcc0f5a7f71079b5df9174e251ca20040662
|
7
|
+
data.tar.gz: 73292fdb8271450c7bce3f67e4daa08d26c77e129e249e519e41fb7080831dda22ebaca12d7c3803b22c02799fd6145169091aa49b2a1fc8e25ad549a39fe559
|
data/README.md
CHANGED
@@ -19,6 +19,13 @@ Or install it yourself as:
|
|
19
19
|
|
20
20
|
$ gem install fetcheable_on_api
|
21
21
|
|
22
|
+
Finally, run the install generator:
|
23
|
+
|
24
|
+
$ rails generate fetcheable_on_api:install
|
25
|
+
|
26
|
+
It will create the following initializer `config/initializers/fetcheable_on_api.rb`.
|
27
|
+
This file contains all the informations about the existing configuration options.
|
28
|
+
|
22
29
|
## Usage
|
23
30
|
|
24
31
|
Imagine the following models called question and answer:
|
@@ -280,6 +287,28 @@ $ curl -X GET \
|
|
280
287
|
]
|
281
288
|
```
|
282
289
|
|
290
|
+
Multiple filter values can be combined in a comma-separated list.
|
291
|
+
|
292
|
+
```bash
|
293
|
+
$ curl -X GET \
|
294
|
+
'http://localhost:3000/questions?filter[content]=real,simple'
|
295
|
+
|
296
|
+
[
|
297
|
+
{
|
298
|
+
"id": 4,
|
299
|
+
"position": 2,
|
300
|
+
"content": "Is it so simple?",
|
301
|
+
"answer": "Yes"
|
302
|
+
},
|
303
|
+
{
|
304
|
+
"id": 5,
|
305
|
+
"position": 3,
|
306
|
+
"content": "Is this real life?",
|
307
|
+
"answer": "Yes this is real life"
|
308
|
+
}
|
309
|
+
]
|
310
|
+
```
|
311
|
+
|
283
312
|
You can also define a filter through an association like this:
|
284
313
|
|
285
314
|
```ruby
|
@@ -51,14 +51,16 @@ module FetcheableOnApi
|
|
51
51
|
.permit(filters_configuration.keys)
|
52
52
|
.to_hash
|
53
53
|
|
54
|
-
filtering = filter_params.map do |column,
|
55
|
-
|
56
|
-
|
54
|
+
filtering = filter_params.map do |column, values|
|
55
|
+
values.split(',').map do |value|
|
56
|
+
column_name = filters_configuration[column.to_sym].fetch(:as, column)
|
57
|
+
klass = filters_configuration[column.to_sym].fetch(:class_name, collection.klass)
|
57
58
|
|
58
|
-
|
59
|
+
klass.arel_table[column_name].matches("%#{value}%")
|
60
|
+
end.inject(:or)
|
59
61
|
end
|
60
62
|
|
61
|
-
collection.where(filtering.inject(:and))
|
63
|
+
collection.where(filtering.flatten.compact.inject(:and))
|
62
64
|
end
|
63
65
|
end
|
64
66
|
end
|