fetcheable_on_api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +66 -0
- data/LICENSE.txt +21 -0
- data/README.md +336 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/fetcheable_on_api.gemspec +47 -0
- data/gemfiles/rails_4.1.gemfile +5 -0
- data/gemfiles/rails_4.2.gemfile +5 -0
- data/gemfiles/rails_5.0.gemfile +5 -0
- data/gemfiles/rails_5.1.gemfile +5 -0
- data/gemfiles/rails_5.2.gemfile +5 -0
- data/gemfiles/rails_head.gemfile +5 -0
- data/lib/fetcheable_on_api.rb +43 -0
- data/lib/fetcheable_on_api/filtreable.rb +64 -0
- data/lib/fetcheable_on_api/pagineable.rb +31 -0
- data/lib/fetcheable_on_api/sortable.rb +72 -0
- data/lib/fetcheable_on_api/version.rb +5 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e306962ed07df291f4da84e7a48ea260f4da40dd
|
4
|
+
data.tar.gz: 9feabe197b5dada9b5f6d6da92f4a1689c069667
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ec029100a872fd1b7b271f462511a155d6c78995ee9a7c07758f92bbc9ba45cdb716fe99060897ecca10c5aa4d8cb690cdded97a2599d6ff031e890e80377ea0
|
7
|
+
data.tar.gz: 86e4291e7a000d6116bc9f9d520860ed1fb9e725310c4f8faed50f2abb7a1ca944288955e949d93377499c19ec0ac1c058f7815c37bc325b219a4f1f49ae0632
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at fabien@tymate.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fetcheable_on_api (0.1.0)
|
5
|
+
activesupport (>= 4.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (5.2.1)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (>= 0.7, < 2)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
tzinfo (~> 1.1)
|
15
|
+
ast (2.4.0)
|
16
|
+
concurrent-ruby (1.0.5)
|
17
|
+
diff-lcs (1.3)
|
18
|
+
i18n (1.1.0)
|
19
|
+
concurrent-ruby (~> 1.0)
|
20
|
+
jaro_winkler (1.5.1)
|
21
|
+
minitest (5.11.3)
|
22
|
+
parallel (1.12.1)
|
23
|
+
parser (2.5.1.2)
|
24
|
+
ast (~> 2.4.0)
|
25
|
+
powerpack (0.1.2)
|
26
|
+
rainbow (3.0.0)
|
27
|
+
rake (10.5.0)
|
28
|
+
rspec (3.8.0)
|
29
|
+
rspec-core (~> 3.8.0)
|
30
|
+
rspec-expectations (~> 3.8.0)
|
31
|
+
rspec-mocks (~> 3.8.0)
|
32
|
+
rspec-core (3.8.0)
|
33
|
+
rspec-support (~> 3.8.0)
|
34
|
+
rspec-expectations (3.8.1)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.8.0)
|
37
|
+
rspec-mocks (3.8.0)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.8.0)
|
40
|
+
rspec-support (3.8.0)
|
41
|
+
rubocop (0.59.2)
|
42
|
+
jaro_winkler (~> 1.5.1)
|
43
|
+
parallel (~> 1.10)
|
44
|
+
parser (>= 2.5, != 2.5.1.1)
|
45
|
+
powerpack (~> 0.1)
|
46
|
+
rainbow (>= 2.2.2, < 4.0)
|
47
|
+
ruby-progressbar (~> 1.7)
|
48
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
49
|
+
ruby-progressbar (1.10.0)
|
50
|
+
thread_safe (0.3.6)
|
51
|
+
tzinfo (1.2.5)
|
52
|
+
thread_safe (~> 0.1)
|
53
|
+
unicode-display_width (1.4.0)
|
54
|
+
|
55
|
+
PLATFORMS
|
56
|
+
ruby
|
57
|
+
|
58
|
+
DEPENDENCIES
|
59
|
+
bundler (~> 1.16)
|
60
|
+
fetcheable_on_api!
|
61
|
+
rake (~> 10.0)
|
62
|
+
rspec (~> 3.0)
|
63
|
+
rubocop (~> 0.59.2)
|
64
|
+
|
65
|
+
BUNDLED WITH
|
66
|
+
1.16.5
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Fabien
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,336 @@
|
|
1
|
+
|
2
|
+
# FetcheableOnApi
|
3
|
+
|
4
|
+
FetcheableOnApi allows you to quickly and easily set up a filter system based on the JSONAPI specification for ActiveRecord objects.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'fetcheable_on_api'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install fetcheable_on_api
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Imagine the following models called question and answer:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class Question < ApplicationRecord
|
28
|
+
#
|
29
|
+
# Validations
|
30
|
+
#
|
31
|
+
validates :content,
|
32
|
+
presence: true
|
33
|
+
|
34
|
+
#
|
35
|
+
# Associations
|
36
|
+
#
|
37
|
+
has_one :answer,
|
38
|
+
class_name: 'Answer',
|
39
|
+
foreign_key: 'question_id',
|
40
|
+
dependent: :destroy,
|
41
|
+
inverse_of: :question
|
42
|
+
end
|
43
|
+
|
44
|
+
# == Schema Information
|
45
|
+
#
|
46
|
+
# Table name: questions
|
47
|
+
#
|
48
|
+
# id :bigint(8) not null, primary key
|
49
|
+
# content :text not null
|
50
|
+
# position :integer
|
51
|
+
# created_at :datetime not null
|
52
|
+
# updated_at :datetime not null
|
53
|
+
#
|
54
|
+
```
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
class Answer < ApplicationRecord
|
58
|
+
#
|
59
|
+
# Validations
|
60
|
+
#
|
61
|
+
validates :content,
|
62
|
+
presence: true
|
63
|
+
|
64
|
+
#
|
65
|
+
# Associations
|
66
|
+
#
|
67
|
+
belongs_to :question,
|
68
|
+
class_name: 'Question',
|
69
|
+
foreign_key: 'question_id',
|
70
|
+
inverse_of: :answer
|
71
|
+
end
|
72
|
+
|
73
|
+
# == Schema Information
|
74
|
+
#
|
75
|
+
# Table name: answers
|
76
|
+
#
|
77
|
+
# id :bigint(8) not null, primary key
|
78
|
+
# content :text not null
|
79
|
+
# question_id :bigint(8)
|
80
|
+
# created_at :datetime not null
|
81
|
+
# updated_at :datetime not null
|
82
|
+
#
|
83
|
+
```
|
84
|
+
And controller:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
class QuestionsController < ActionController::Base
|
88
|
+
# GET /questions
|
89
|
+
def index
|
90
|
+
questions = Question.joins(:answer).includes(:answer).all
|
91
|
+
render json: questions
|
92
|
+
end
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
### Sorting
|
97
|
+
|
98
|
+
You can now define the allowed attribute(s) in the sorting of the collection like this:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
class QuestionsController < ActionController::Base
|
102
|
+
#
|
103
|
+
# FetcheableOnApi
|
104
|
+
#
|
105
|
+
sort_by :position
|
106
|
+
|
107
|
+
# GET /questions
|
108
|
+
def index
|
109
|
+
questions = apply_fetcheable(Question.joins(:answer).includes(:answer).all)
|
110
|
+
render json: questions
|
111
|
+
end
|
112
|
+
end
|
113
|
+
```
|
114
|
+
This allows you to pass a new parameter in the query:
|
115
|
+
|
116
|
+
```bash
|
117
|
+
$ curl -X GET \
|
118
|
+
'http://localhost:3000/questions?sort=position'
|
119
|
+
|
120
|
+
[
|
121
|
+
{
|
122
|
+
"id": 3,
|
123
|
+
"position": 1,
|
124
|
+
"content": "How to simply sort a collection with this gem ?",
|
125
|
+
"answer": "Just add sort_by in your controller and call the apply_fetcheable method"
|
126
|
+
},
|
127
|
+
{
|
128
|
+
"id": 4,
|
129
|
+
"position": 2,
|
130
|
+
"content": "Is it so simple?",
|
131
|
+
"answer": "Yes"
|
132
|
+
},
|
133
|
+
{
|
134
|
+
"id": 5,
|
135
|
+
"position": 3,
|
136
|
+
"content": "Is this real life?",
|
137
|
+
"answer": "Yes this is real life"
|
138
|
+
}
|
139
|
+
]
|
140
|
+
```
|
141
|
+
|
142
|
+
FetcheableOnApi support multiple sort fields by allowing comma-separated (U+002C COMMA, “,”) sort fields:
|
143
|
+
```ruby
|
144
|
+
class QuestionsController < ActionController::Base
|
145
|
+
#
|
146
|
+
# FetcheableOnApi
|
147
|
+
#
|
148
|
+
sort_by :position, :id
|
149
|
+
|
150
|
+
# GET /questions
|
151
|
+
def index
|
152
|
+
questions = apply_fetcheable(Question.joins(:answer).includes(:answer).all)
|
153
|
+
render json: questions
|
154
|
+
end
|
155
|
+
end
|
156
|
+
```
|
157
|
+
|
158
|
+
```bash
|
159
|
+
$ curl -X GET \
|
160
|
+
'http://localhost:3000/questions?sort=position,id'
|
161
|
+
|
162
|
+
[
|
163
|
+
{
|
164
|
+
"id": 3,
|
165
|
+
"position": 1,
|
166
|
+
"content": "How to simply sort a collection with this gem ?",
|
167
|
+
"answer": "Just add sort_by in your controller and call the apply_fetcheable method"
|
168
|
+
},
|
169
|
+
{
|
170
|
+
"id": 4,
|
171
|
+
"position": 2,
|
172
|
+
"content": "Is it so simple?",
|
173
|
+
"answer": "Yes"
|
174
|
+
},
|
175
|
+
{
|
176
|
+
"id": 5,
|
177
|
+
"position": 3,
|
178
|
+
"content": "Is this real life?",
|
179
|
+
"answer": "Yes this is real life"
|
180
|
+
}
|
181
|
+
]
|
182
|
+
```
|
183
|
+
|
184
|
+
The default sort order for each sort field is ascending unless it is prefixed with a minus (U+002D HYPHEN-MINUS, “-“), in which case it is descending:
|
185
|
+
|
186
|
+
```bash
|
187
|
+
$ curl -X GET \
|
188
|
+
'http://localhost:3000/questions?sort=-position'
|
189
|
+
|
190
|
+
[
|
191
|
+
{
|
192
|
+
"id": 5,
|
193
|
+
"position": 3,
|
194
|
+
"content": "Is this real life?",
|
195
|
+
"answer": "Yes this is real life"
|
196
|
+
},
|
197
|
+
{
|
198
|
+
"id": 4,
|
199
|
+
"position": 2,
|
200
|
+
"content": "Is it so simple?",
|
201
|
+
"answer": "Yes"
|
202
|
+
},
|
203
|
+
{
|
204
|
+
"id": 3,
|
205
|
+
"position": 1,
|
206
|
+
"content": "How to simply sort a collection with this gem ?",
|
207
|
+
"answer": "Just add sort_by in your controller and call the apply_fetcheable method"
|
208
|
+
}
|
209
|
+
]
|
210
|
+
```
|
211
|
+
|
212
|
+
### Pagination
|
213
|
+
|
214
|
+
Pagination is automatically set on the controller and allows the use of a new parameter `page`.
|
215
|
+
The pagination is page-based strategy, it can use query parameters such as `page[number]` and `page[size]`
|
216
|
+
|
217
|
+
```bash
|
218
|
+
$ curl -X GET \
|
219
|
+
'http://localhost:3000/questions?page[number]=1&page[size]=2'
|
220
|
+
|
221
|
+
[
|
222
|
+
{
|
223
|
+
"id": 3,
|
224
|
+
"position": 1,
|
225
|
+
"content": "How to simply sort a collection with this gem ?",
|
226
|
+
"answer": "Just add sort_by in your controller and call the apply_fetcheable method"
|
227
|
+
},
|
228
|
+
{
|
229
|
+
"id": 4,
|
230
|
+
"position": 2,
|
231
|
+
"content": "Is it so simple?",
|
232
|
+
"answer": "Yes"
|
233
|
+
}
|
234
|
+
]
|
235
|
+
|
236
|
+
$ curl -X GET \
|
237
|
+
'http://localhost:3000/questions?page[number]=2&page[size]=2'
|
238
|
+
|
239
|
+
[
|
240
|
+
{
|
241
|
+
"id": 5,
|
242
|
+
"position": 3,
|
243
|
+
"content": "Is this real life?",
|
244
|
+
"answer": "Yes this is real life"
|
245
|
+
}
|
246
|
+
]
|
247
|
+
```
|
248
|
+
|
249
|
+
### Filtering
|
250
|
+
|
251
|
+
The `filter` query parameter is reserved for filtering data and the controller must set the attributes allowed to be filtered.
|
252
|
+
|
253
|
+
```ruby
|
254
|
+
class QuestionsController < ActionController::Base
|
255
|
+
#
|
256
|
+
# FetcheableOnApi
|
257
|
+
#
|
258
|
+
sort_by :position, :id
|
259
|
+
filter_by :content
|
260
|
+
|
261
|
+
# GET /questions
|
262
|
+
def index
|
263
|
+
questions = apply_fetcheable(Question.joins(:answer).includes(:answer).all)
|
264
|
+
render json: questions
|
265
|
+
end
|
266
|
+
end
|
267
|
+
```
|
268
|
+
|
269
|
+
```bash
|
270
|
+
$ curl -X GET \
|
271
|
+
'http://localhost:3000/questions?filter[content]=gem'
|
272
|
+
|
273
|
+
[
|
274
|
+
{
|
275
|
+
"id": 3,
|
276
|
+
"position": 1,
|
277
|
+
"content": "How to simply sort a collection with this gem ?",
|
278
|
+
"answer": "Just add sort_by in your controller and call the apply_fetcheable method"
|
279
|
+
}
|
280
|
+
]
|
281
|
+
```
|
282
|
+
|
283
|
+
You can also define a filter through an association like this:
|
284
|
+
|
285
|
+
```ruby
|
286
|
+
class QuestionsController < ActionController::Base
|
287
|
+
#
|
288
|
+
# FetcheableOnApi
|
289
|
+
#
|
290
|
+
sort_by :position, :id
|
291
|
+
filter_by :content
|
292
|
+
filter_by :answer,
|
293
|
+
class_name: Answer,
|
294
|
+
as: 'content'
|
295
|
+
|
296
|
+
# GET /questions
|
297
|
+
def index
|
298
|
+
questions = apply_fetcheable(Question.joins(:answer).includes(:answer).all)
|
299
|
+
render json: questions
|
300
|
+
end
|
301
|
+
end
|
302
|
+
```
|
303
|
+
|
304
|
+
```bash
|
305
|
+
$ curl -X GET \
|
306
|
+
'http://localhost:3000/questions?filter[answer]=apply_fetcheable'
|
307
|
+
|
308
|
+
[
|
309
|
+
{
|
310
|
+
"id": 3,
|
311
|
+
"position": 1,
|
312
|
+
"content": "How to simply sort a collection with this gem ?",
|
313
|
+
"answer": "Just add sort_by in your controller and call the apply_fetcheable method"
|
314
|
+
}
|
315
|
+
]
|
316
|
+
```
|
317
|
+
|
318
|
+
And that's all !
|
319
|
+
|
320
|
+
## Development
|
321
|
+
|
322
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
323
|
+
|
324
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
325
|
+
|
326
|
+
## Contributing
|
327
|
+
|
328
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fetcheable_on_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
329
|
+
|
330
|
+
## License
|
331
|
+
|
332
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
333
|
+
|
334
|
+
## Code of Conduct
|
335
|
+
|
336
|
+
Everyone interacting in the FetcheableOnApi project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/fetcheable_on_api/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'fetcheable_on_api'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'fetcheable_on_api/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'fetcheable_on_api'
|
7
|
+
spec.version = FetcheableOnApi::VERSION
|
8
|
+
spec.authors = ['Fabien']
|
9
|
+
spec.email = ['fab.piette@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'A controller filters engine gem'\
|
12
|
+
' based on jsonapi spec.'
|
13
|
+
spec.description = 'A controller filters engine gem'\
|
14
|
+
' based on jsonapi spec.'
|
15
|
+
spec.homepage = 'https://github.com/FabienPiette/fetcheable_on_api.git'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org.
|
19
|
+
# To allow pushes either set the 'allowed_push_host'
|
20
|
+
# to allow pushing to a single host or delete
|
21
|
+
# this section to allow pushing to any host.
|
22
|
+
if spec.respond_to?(:metadata)
|
23
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
24
|
+
else
|
25
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
26
|
+
'public gem pushes.'
|
27
|
+
end
|
28
|
+
|
29
|
+
# Specify which files should be added to
|
30
|
+
# the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in
|
32
|
+
# the RubyGem that have been added into git.
|
33
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
34
|
+
`git ls-files -z`.split("\x0")
|
35
|
+
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
36
|
+
end
|
37
|
+
spec.bindir = 'exe'
|
38
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
39
|
+
spec.require_paths = ['lib']
|
40
|
+
|
41
|
+
spec.add_dependency 'activesupport', '>= 4.1'
|
42
|
+
|
43
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
44
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
45
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
46
|
+
spec.add_development_dependency 'rubocop', '~> 0.59.2'
|
47
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'fetcheable_on_api/filtreable'
|
4
|
+
require 'fetcheable_on_api/pagineable'
|
5
|
+
require 'fetcheable_on_api/sortable'
|
6
|
+
require 'fetcheable_on_api/version'
|
7
|
+
require 'active_support'
|
8
|
+
|
9
|
+
module FetcheableOnApi
|
10
|
+
#
|
11
|
+
# Supports
|
12
|
+
#
|
13
|
+
|
14
|
+
#
|
15
|
+
# Public class methods
|
16
|
+
#
|
17
|
+
def self.included(klass)
|
18
|
+
klass.class_eval do
|
19
|
+
include Filtreable
|
20
|
+
include Sortable
|
21
|
+
include Pagineable
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
#
|
26
|
+
# Public instance methods
|
27
|
+
#
|
28
|
+
|
29
|
+
#
|
30
|
+
# Protected instance methods
|
31
|
+
#
|
32
|
+
protected
|
33
|
+
|
34
|
+
def apply_fetcheable(collection)
|
35
|
+
collection = apply_sort(collection)
|
36
|
+
collection = apply_pagination(collection)
|
37
|
+
apply_filters(collection)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
ActiveSupport.on_load :action_controller do
|
42
|
+
include Fetcheable
|
43
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FetcheableOnApi
|
4
|
+
module Filtreable
|
5
|
+
#
|
6
|
+
# Supports
|
7
|
+
#
|
8
|
+
|
9
|
+
#
|
10
|
+
# Public class methods
|
11
|
+
#
|
12
|
+
def self.included(base)
|
13
|
+
base.class_eval do
|
14
|
+
extend ClassMethods
|
15
|
+
class_attribute :filters_configuration, :instance_writer => false
|
16
|
+
self.filters_configuration = {}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
def filter_by(*attrs)
|
22
|
+
options = attrs.extract_options!
|
23
|
+
options.symbolize_keys!
|
24
|
+
options.assert_valid_keys(:as, :class_name)
|
25
|
+
|
26
|
+
self.filters_configuration = filters_configuration.dup
|
27
|
+
|
28
|
+
attrs.each do |attr|
|
29
|
+
filters_configuration[attr] ||= {
|
30
|
+
as: options[:as] || attr
|
31
|
+
}
|
32
|
+
|
33
|
+
filters_configuration[attr] = filters_configuration[attr].merge(options)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
#
|
39
|
+
# Public instance methods
|
40
|
+
#
|
41
|
+
|
42
|
+
#
|
43
|
+
# Protected instance methods
|
44
|
+
#
|
45
|
+
protected
|
46
|
+
|
47
|
+
def apply_filters(collection)
|
48
|
+
return collection if params[:filter].blank?
|
49
|
+
|
50
|
+
filter_params = params.require(:filter)
|
51
|
+
.permit(filters_configuration.keys)
|
52
|
+
.to_hash
|
53
|
+
|
54
|
+
filtering = filter_params.map do |column, value|
|
55
|
+
column_name = filters_configuration[column.to_sym].fetch(:as, column)
|
56
|
+
klass = filters_configuration[column.to_sym].fetch(:class_name, collection.klass)
|
57
|
+
|
58
|
+
klass.arel_table[column_name].matches("%#{value}%")
|
59
|
+
end
|
60
|
+
|
61
|
+
collection.where(filtering.inject(:and))
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FetcheableOnApi
|
4
|
+
module Pagineable
|
5
|
+
#
|
6
|
+
# Supports
|
7
|
+
#
|
8
|
+
|
9
|
+
#
|
10
|
+
# Public class methods
|
11
|
+
#
|
12
|
+
|
13
|
+
#
|
14
|
+
# Public instance methods
|
15
|
+
#
|
16
|
+
|
17
|
+
#
|
18
|
+
# Protected instance methods
|
19
|
+
#
|
20
|
+
protected
|
21
|
+
|
22
|
+
def apply_pagination(collection)
|
23
|
+
return collection if params[:page].blank?
|
24
|
+
|
25
|
+
limit = params[:page].fetch(:size, 25).to_i
|
26
|
+
offset = (params[:page].fetch(:number, 1).to_i - 1) * limit
|
27
|
+
|
28
|
+
collection.limit(limit).offset(offset)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FetcheableOnApi
|
4
|
+
module Sortable
|
5
|
+
#
|
6
|
+
# Supports
|
7
|
+
#
|
8
|
+
SORT_ORDER = {
|
9
|
+
'+' => :asc,
|
10
|
+
'-' => :desc
|
11
|
+
}
|
12
|
+
|
13
|
+
#
|
14
|
+
# Public class methods
|
15
|
+
#
|
16
|
+
def self.included(base)
|
17
|
+
base.class_eval do
|
18
|
+
extend ClassMethods
|
19
|
+
class_attribute :sorts_configuration, :instance_writer => false
|
20
|
+
self.sorts_configuration = {}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
def sort_by(*attrs)
|
26
|
+
options = attrs.extract_options!
|
27
|
+
options.symbolize_keys!
|
28
|
+
|
29
|
+
self.sorts_configuration = sorts_configuration.dup
|
30
|
+
|
31
|
+
attrs.each do |attr|
|
32
|
+
sorts_configuration[attr] ||= {
|
33
|
+
as: attr
|
34
|
+
}
|
35
|
+
|
36
|
+
sorts_configuration[attr] = self.sorts_configuration[attr].merge(options)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
#
|
42
|
+
# Public instance methods
|
43
|
+
#
|
44
|
+
|
45
|
+
#
|
46
|
+
# Protected instance methods
|
47
|
+
#
|
48
|
+
protected
|
49
|
+
|
50
|
+
def apply_sort(collection)
|
51
|
+
return collection if params[:sort].blank?
|
52
|
+
|
53
|
+
ordering = {}
|
54
|
+
sorted_params = params[:sort].split(',')
|
55
|
+
|
56
|
+
sorted_params.each do |attr|
|
57
|
+
sort_sign = (attr =~ /\A[+-]/) ? attr.slice!(0) : '+'
|
58
|
+
klass = collection.klass
|
59
|
+
|
60
|
+
if klass.attribute_names.include?(attr)
|
61
|
+
ordering[attr] = SORT_ORDER[sort_sign]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
ordering.select! do |attr|
|
66
|
+
sorts_configuration.key?(attr.to_sym)
|
67
|
+
end
|
68
|
+
|
69
|
+
collection.order(ordering)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fetcheable_on_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fabien
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.59.2
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.59.2
|
83
|
+
description: A controller filters engine gem based on jsonapi spec.
|
84
|
+
email:
|
85
|
+
- fab.piette@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- fetcheable_on_api.gemspec
|
102
|
+
- gemfiles/rails_4.1.gemfile
|
103
|
+
- gemfiles/rails_4.2.gemfile
|
104
|
+
- gemfiles/rails_5.0.gemfile
|
105
|
+
- gemfiles/rails_5.1.gemfile
|
106
|
+
- gemfiles/rails_5.2.gemfile
|
107
|
+
- gemfiles/rails_head.gemfile
|
108
|
+
- lib/fetcheable_on_api.rb
|
109
|
+
- lib/fetcheable_on_api/filtreable.rb
|
110
|
+
- lib/fetcheable_on_api/pagineable.rb
|
111
|
+
- lib/fetcheable_on_api/sortable.rb
|
112
|
+
- lib/fetcheable_on_api/version.rb
|
113
|
+
homepage: https://github.com/FabienPiette/fetcheable_on_api.git
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata:
|
117
|
+
allowed_push_host: https://rubygems.org
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.6.14
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: A controller filters engine gem based on jsonapi spec.
|
138
|
+
test_files: []
|