rest-api-generator 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/Gemfile.lock +171 -44
- data/README.md +232 -64
- data/Rakefile +1 -1
- data/lib/generators/rest_api_generator/helpers.rb +30 -0
- data/lib/generators/rest_api_generator/resource_generator.rb +73 -13
- data/lib/generators/rest_api_generator/templates/child_api_controller.rb.tt +9 -8
- data/lib/generators/rest_api_generator/templates/child_api_spec.rb.tt +37 -34
- data/lib/generators/rest_api_generator/templates/implicit_child_resource_controller.rb.tt +4 -0
- data/lib/generators/rest_api_generator/templates/implicit_resource_controller.rb.tt +4 -0
- data/lib/generators/rest_api_generator/templates/rest_api_controller.rb.tt +3 -1
- data/lib/generators/rest_api_generator/templates/rest_api_spec.rb.tt +5 -5
- data/lib/rest-api-generator.rb +3 -0
- data/lib/rest_api_generator/application_controller.rb +8 -0
- data/lib/rest_api_generator/child_resource_controller.rb +97 -0
- data/lib/rest_api_generator/filterable.rb +30 -0
- data/lib/rest_api_generator/helpers/render.rb +1 -1
- data/lib/rest_api_generator/orderable.rb +30 -0
- data/lib/rest_api_generator/resource_controller.rb +73 -0
- data/lib/rest_api_generator/version.rb +1 -1
- data/lib/rest_api_generator.rb +11 -1
- data/rest-api-generator.gemspec +41 -0
- metadata +73 -11
- data/.rspec +0 -3
- data/.rubocop.yml +0 -23
- data/CODE_OF_CONDUCT.md +0 -84
- data/LICENSE.txt +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf4062a70bed1e9006d96375d4110b3918f3bf5d074304c4619abfa4fe33eb03
|
4
|
+
data.tar.gz: ba52d11b288dd8004e68327a8db4aabac8bef8be93eea85382f3bf349406ea39
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1ef3d60ca714e5edf5db7cdcf6b0287ef22c2123c907070cb3b6608ca159ceba200642a8b45894cb88e0fab73d5abaeb728e19a05c9e2760cd0e17ccb155732
|
7
|
+
data.tar.gz: 9849fa88d3b3a9f5a070c7f4a4ca29fc3deb8447adb80260004aa68076cd0a27db9cbeb5d214aed2505df4a1c1a48e26378dc41b6626159eb635d1a7d2c0218e
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,107 +1,234 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rest-api-generator (0.1.
|
5
|
-
|
4
|
+
rest-api-generator (0.1.2)
|
5
|
+
rails (>= 5.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
|
11
|
-
|
12
|
-
activesupport (= 7.0.
|
10
|
+
actioncable (7.0.4)
|
11
|
+
actionpack (= 7.0.4)
|
12
|
+
activesupport (= 7.0.4)
|
13
|
+
nio4r (~> 2.0)
|
14
|
+
websocket-driver (>= 0.6.1)
|
15
|
+
actionmailbox (7.0.4)
|
16
|
+
actionpack (= 7.0.4)
|
17
|
+
activejob (= 7.0.4)
|
18
|
+
activerecord (= 7.0.4)
|
19
|
+
activestorage (= 7.0.4)
|
20
|
+
activesupport (= 7.0.4)
|
21
|
+
mail (>= 2.7.1)
|
22
|
+
net-imap
|
23
|
+
net-pop
|
24
|
+
net-smtp
|
25
|
+
actionmailer (7.0.4)
|
26
|
+
actionpack (= 7.0.4)
|
27
|
+
actionview (= 7.0.4)
|
28
|
+
activejob (= 7.0.4)
|
29
|
+
activesupport (= 7.0.4)
|
30
|
+
mail (~> 2.5, >= 2.5.4)
|
31
|
+
net-imap
|
32
|
+
net-pop
|
33
|
+
net-smtp
|
34
|
+
rails-dom-testing (~> 2.0)
|
35
|
+
actionpack (7.0.4)
|
36
|
+
actionview (= 7.0.4)
|
37
|
+
activesupport (= 7.0.4)
|
13
38
|
rack (~> 2.0, >= 2.2.0)
|
14
39
|
rack-test (>= 0.6.3)
|
15
40
|
rails-dom-testing (~> 2.0)
|
16
41
|
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
17
|
-
|
18
|
-
|
42
|
+
actiontext (7.0.4)
|
43
|
+
actionpack (= 7.0.4)
|
44
|
+
activerecord (= 7.0.4)
|
45
|
+
activestorage (= 7.0.4)
|
46
|
+
activesupport (= 7.0.4)
|
47
|
+
globalid (>= 0.6.0)
|
48
|
+
nokogiri (>= 1.8.5)
|
49
|
+
actionview (7.0.4)
|
50
|
+
activesupport (= 7.0.4)
|
19
51
|
builder (~> 3.1)
|
20
52
|
erubi (~> 1.4)
|
21
53
|
rails-dom-testing (~> 2.0)
|
22
54
|
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
23
|
-
|
55
|
+
activejob (7.0.4)
|
56
|
+
activesupport (= 7.0.4)
|
57
|
+
globalid (>= 0.3.6)
|
58
|
+
activemodel (7.0.4)
|
59
|
+
activesupport (= 7.0.4)
|
60
|
+
activerecord (7.0.4)
|
61
|
+
activemodel (= 7.0.4)
|
62
|
+
activesupport (= 7.0.4)
|
63
|
+
activestorage (7.0.4)
|
64
|
+
actionpack (= 7.0.4)
|
65
|
+
activejob (= 7.0.4)
|
66
|
+
activerecord (= 7.0.4)
|
67
|
+
activesupport (= 7.0.4)
|
68
|
+
marcel (~> 1.0)
|
69
|
+
mini_mime (>= 1.1.0)
|
70
|
+
activesupport (7.0.4)
|
24
71
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
25
72
|
i18n (>= 1.6, < 2)
|
26
73
|
minitest (>= 5.1)
|
27
74
|
tzinfo (~> 2.0)
|
75
|
+
ammeter (1.1.5)
|
76
|
+
activesupport (>= 3.0)
|
77
|
+
railties (>= 3.0)
|
78
|
+
rspec-rails (>= 2.2)
|
28
79
|
ast (2.4.2)
|
29
80
|
builder (3.2.4)
|
30
81
|
concurrent-ruby (1.1.10)
|
31
82
|
crass (1.0.6)
|
83
|
+
database_cleaner (2.0.1)
|
84
|
+
database_cleaner-active_record (~> 2.0.0)
|
85
|
+
database_cleaner-active_record (2.0.1)
|
86
|
+
activerecord (>= 5.a)
|
87
|
+
database_cleaner-core (~> 2.0.0)
|
88
|
+
database_cleaner-core (2.0.1)
|
89
|
+
date (3.3.3)
|
32
90
|
diff-lcs (1.5.0)
|
33
|
-
erubi (1.
|
34
|
-
|
91
|
+
erubi (1.12.0)
|
92
|
+
globalid (1.0.0)
|
93
|
+
activesupport (>= 5.0)
|
94
|
+
i18n (1.12.0)
|
35
95
|
concurrent-ruby (~> 1.0)
|
36
|
-
|
96
|
+
json (2.6.3)
|
97
|
+
loofah (2.19.1)
|
37
98
|
crass (~> 1.0.2)
|
38
99
|
nokogiri (>= 1.5.9)
|
100
|
+
mail (2.8.0)
|
101
|
+
mini_mime (>= 0.1.1)
|
102
|
+
net-imap
|
103
|
+
net-pop
|
104
|
+
net-smtp
|
105
|
+
marcel (1.0.2)
|
39
106
|
method_source (1.0.0)
|
40
|
-
|
41
|
-
|
107
|
+
mini_mime (1.1.2)
|
108
|
+
minitest (5.16.3)
|
109
|
+
net-imap (0.3.4)
|
110
|
+
date
|
111
|
+
net-protocol
|
112
|
+
net-pop (0.1.2)
|
113
|
+
net-protocol
|
114
|
+
net-protocol (0.2.1)
|
115
|
+
timeout
|
116
|
+
net-smtp (0.3.3)
|
117
|
+
net-protocol
|
118
|
+
nio4r (2.5.8)
|
119
|
+
nokogiri (1.13.10-x86_64-linux)
|
42
120
|
racc (~> 1.4)
|
43
121
|
parallel (1.22.1)
|
44
|
-
parser (3.1.
|
122
|
+
parser (3.1.3.0)
|
45
123
|
ast (~> 2.4.1)
|
46
|
-
racc (1.6.
|
47
|
-
rack (2.2.
|
48
|
-
rack-test (
|
49
|
-
rack (>= 1.
|
124
|
+
racc (1.6.2)
|
125
|
+
rack (2.2.4)
|
126
|
+
rack-test (2.0.2)
|
127
|
+
rack (>= 1.3)
|
128
|
+
rails (7.0.4)
|
129
|
+
actioncable (= 7.0.4)
|
130
|
+
actionmailbox (= 7.0.4)
|
131
|
+
actionmailer (= 7.0.4)
|
132
|
+
actionpack (= 7.0.4)
|
133
|
+
actiontext (= 7.0.4)
|
134
|
+
actionview (= 7.0.4)
|
135
|
+
activejob (= 7.0.4)
|
136
|
+
activemodel (= 7.0.4)
|
137
|
+
activerecord (= 7.0.4)
|
138
|
+
activestorage (= 7.0.4)
|
139
|
+
activesupport (= 7.0.4)
|
140
|
+
bundler (>= 1.15.0)
|
141
|
+
railties (= 7.0.4)
|
50
142
|
rails-dom-testing (2.0.3)
|
51
143
|
activesupport (>= 4.2.0)
|
52
144
|
nokogiri (>= 1.6)
|
53
|
-
rails-html-sanitizer (1.4.
|
54
|
-
loofah (~> 2.
|
55
|
-
railties (7.0.
|
56
|
-
actionpack (= 7.0.
|
57
|
-
activesupport (= 7.0.
|
145
|
+
rails-html-sanitizer (1.4.4)
|
146
|
+
loofah (~> 2.19, >= 2.19.1)
|
147
|
+
railties (7.0.4)
|
148
|
+
actionpack (= 7.0.4)
|
149
|
+
activesupport (= 7.0.4)
|
58
150
|
method_source
|
59
151
|
rake (>= 12.2)
|
60
152
|
thor (~> 1.0)
|
61
153
|
zeitwerk (~> 2.5)
|
62
154
|
rainbow (3.1.1)
|
63
155
|
rake (13.0.6)
|
64
|
-
regexp_parser (2.
|
156
|
+
regexp_parser (2.6.1)
|
65
157
|
rexml (3.2.5)
|
66
|
-
rspec (3.
|
67
|
-
rspec-core (~> 3.
|
68
|
-
rspec-expectations (~> 3.
|
69
|
-
rspec-mocks (~> 3.
|
70
|
-
rspec-core (3.
|
71
|
-
rspec-support (~> 3.
|
72
|
-
rspec-expectations (3.
|
158
|
+
rspec (3.12.0)
|
159
|
+
rspec-core (~> 3.12.0)
|
160
|
+
rspec-expectations (~> 3.12.0)
|
161
|
+
rspec-mocks (~> 3.12.0)
|
162
|
+
rspec-core (3.12.0)
|
163
|
+
rspec-support (~> 3.12.0)
|
164
|
+
rspec-expectations (3.12.1)
|
73
165
|
diff-lcs (>= 1.2.0, < 2.0)
|
74
|
-
rspec-support (~> 3.
|
75
|
-
rspec-mocks (3.
|
166
|
+
rspec-support (~> 3.12.0)
|
167
|
+
rspec-mocks (3.12.1)
|
76
168
|
diff-lcs (>= 1.2.0, < 2.0)
|
77
|
-
rspec-support (~> 3.
|
78
|
-
rspec-
|
79
|
-
|
169
|
+
rspec-support (~> 3.12.0)
|
170
|
+
rspec-rails (6.0.1)
|
171
|
+
actionpack (>= 6.1)
|
172
|
+
activesupport (>= 6.1)
|
173
|
+
railties (>= 6.1)
|
174
|
+
rspec-core (~> 3.11)
|
175
|
+
rspec-expectations (~> 3.11)
|
176
|
+
rspec-mocks (~> 3.11)
|
177
|
+
rspec-support (~> 3.11)
|
178
|
+
rspec-support (3.12.0)
|
179
|
+
rubocop (1.40.0)
|
180
|
+
json (~> 2.3)
|
80
181
|
parallel (~> 1.10)
|
81
|
-
parser (>= 3.1.
|
182
|
+
parser (>= 3.1.2.1)
|
82
183
|
rainbow (>= 2.2.2, < 4.0)
|
83
184
|
regexp_parser (>= 1.8, < 3.0)
|
84
|
-
rexml
|
85
|
-
rubocop-ast (>= 1.
|
185
|
+
rexml (>= 3.2.5, < 4.0)
|
186
|
+
rubocop-ast (>= 1.23.0, < 2.0)
|
86
187
|
ruby-progressbar (~> 1.7)
|
87
188
|
unicode-display_width (>= 1.4.0, < 3.0)
|
88
|
-
rubocop-ast (1.
|
189
|
+
rubocop-ast (1.24.0)
|
89
190
|
parser (>= 3.1.1.0)
|
191
|
+
rubocop-performance (1.15.2)
|
192
|
+
rubocop (>= 1.7.0, < 2.0)
|
193
|
+
rubocop-ast (>= 0.4.0)
|
194
|
+
rubocop-rails (2.17.4)
|
195
|
+
activesupport (>= 4.2.0)
|
196
|
+
rack (>= 1.1)
|
197
|
+
rubocop (>= 1.33.0, < 2.0)
|
198
|
+
rubocop-rspec (2.16.0)
|
199
|
+
rubocop (~> 1.33)
|
200
|
+
rubocop-shopify (2.10.1)
|
201
|
+
rubocop (~> 1.35)
|
90
202
|
ruby-progressbar (1.11.0)
|
203
|
+
sqlite3 (1.5.4-x86_64-linux)
|
204
|
+
switchcop (0.1.2)
|
205
|
+
rubocop (~> 1.40.0)
|
206
|
+
rubocop-performance (~> 1.15.1)
|
207
|
+
rubocop-rails (~> 2.17.3)
|
208
|
+
rubocop-rspec (~> 2.16.0)
|
209
|
+
rubocop-shopify (~> 2.10.1)
|
91
210
|
thor (1.2.1)
|
92
|
-
|
211
|
+
timeout (0.3.1)
|
212
|
+
tzinfo (2.0.5)
|
93
213
|
concurrent-ruby (~> 1.0)
|
94
|
-
unicode-display_width (2.
|
95
|
-
|
214
|
+
unicode-display_width (2.3.0)
|
215
|
+
websocket-driver (0.7.5)
|
216
|
+
websocket-extensions (>= 0.1.0)
|
217
|
+
websocket-extensions (0.1.5)
|
218
|
+
zeitwerk (2.6.6)
|
96
219
|
|
97
220
|
PLATFORMS
|
98
221
|
x86_64-linux
|
99
222
|
|
100
223
|
DEPENDENCIES
|
224
|
+
ammeter (~> 1.1.5)
|
225
|
+
database_cleaner
|
101
226
|
rake (~> 13.0)
|
102
227
|
rest-api-generator!
|
103
228
|
rspec (~> 3.0)
|
104
|
-
|
229
|
+
rspec-rails (~> 6.0.0)
|
230
|
+
sqlite3
|
231
|
+
switchcop
|
105
232
|
|
106
233
|
BUNDLED WITH
|
107
234
|
2.3.3
|
data/README.md
CHANGED
@@ -1,23 +1,46 @@
|
|
1
|
-
#
|
1
|
+
# rest-api-generator
|
2
2
|
|
3
|
-
This gem
|
3
|
+
This gem helps you to build a Ruby on Rails REST API faster, using a scaffold-like generator that follows the best
|
4
|
+
practices.
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
## How it works?
|
7
|
+
|
8
|
+
The gems use vanilla Rails generators in combination with our templates to create all the resources needed to build a
|
9
|
+
REST API.
|
10
|
+
|
11
|
+
Following [Switch Dreams's](https://www.switchdreams.com.br/]) coding practices, the controllers are built with:
|
12
|
+
|
13
|
+
- We use an error module to centralize error handling, rescuing from a custom and some of ActiveRecord exceptions.
|
14
|
+
The inspiration for this strategy was
|
15
|
+
this [article](https://medium.com/rails-ember-beyond/error-handling-in-rails-the-modular-way-9afcddd2fe1b.)
|
13
16
|
|
14
|
-
|
17
|
+
- For tests, we use RSpec and FactoryBot.
|
18
|
+
|
19
|
+
## Current Features
|
20
|
+
|
21
|
+
- [Automatic rest api crud generation](#example)
|
22
|
+
- [Nested Resource](#nested-resource)
|
23
|
+
- [Modular error handler](#modular-error-handler)
|
24
|
+
- [Resource ordering](#ordering)
|
25
|
+
- [Resource filter](#filtering)
|
26
|
+
|
27
|
+
## Next Features
|
28
|
+
|
29
|
+
- Generate nested resource end-points 🚧
|
30
|
+
- Automated documentation 🚧 https://github.com/SwitchDreams/rest-api-generator/issues/12
|
31
|
+
- Serialization https://github.com/SwitchDreams/rest-api-generator/issues/14
|
32
|
+
https://github.com/SwitchDreams/rest-api-generator/issues/11
|
33
|
+
- Pagination https://github.com/SwitchDreams/rest-api-generator/issues/15
|
34
|
+
- Integration with AVO
|
35
|
+
- Select fields
|
36
|
+
- User auth module
|
15
37
|
|
16
38
|
## Installation
|
17
39
|
|
18
40
|
Add this line to your application's Gemfile:
|
19
41
|
|
20
42
|
```ruby
|
43
|
+
# Build a Ruby on Rails REST API faster
|
21
44
|
gem 'rest-api-generator'
|
22
45
|
```
|
23
46
|
|
@@ -30,92 +53,237 @@ Or install it yourself as:
|
|
30
53
|
$ gem install rest-api-generator
|
31
54
|
|
32
55
|
## Requirements
|
33
|
-
|
56
|
+
|
57
|
+
1. You need to have installed RSpec and FactoryBot in your application.
|
34
58
|
|
35
59
|
<ul>
|
36
|
-
<li>
|
60
|
+
<li>RSpec: https://github.com/rspec/rspec-rails</li>
|
37
61
|
<li>Factory bot: https://github.com/thoughtbot/factory_bot_rails</li>
|
38
62
|
</ul>
|
39
63
|
|
40
|
-
|
64
|
+
2. Include in ApplicationController the error handler module:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
|
68
|
+
class ApplicationController < ActionController::API
|
69
|
+
include RestApiGenerator::ErrorHandler
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
This error handler will rescue from: `ActiveRecord::RecordNotFound`
|
74
|
+
, `ActiveRecord::ActiveRecordError`, `ActiveRecord::RecordInvalid`, `ActiveModel::ValidationError`
|
75
|
+
, `RestApiGenerator::CustomError`.
|
41
76
|
|
42
77
|
## Usage
|
43
|
-
### Run command
|
44
|
-
$ rails g generator table_name attributes
|
45
78
|
|
46
|
-
|
47
|
-
#### Model
|
48
|
-
this will generate a table and a migration with the table name and it's attribute, it invokes the model generator
|
79
|
+
### Generate Resource
|
49
80
|
|
50
|
-
|
51
|
-
|
52
|
-
|
81
|
+
```bash
|
82
|
+
$ rails g rest_api_generator:resource table_name attributes
|
83
|
+
```
|
53
84
|
|
54
|
-
|
55
|
-
saves instance of generated model to database and return json of instance with status ok
|
85
|
+
This command will create:
|
56
86
|
|
57
|
-
|
58
|
-
|
87
|
+
- **Model and Migration**: Using rails default model generator
|
88
|
+
- **Controller**: A controller that implementes CRUD by inheritance of `RestApiGenerator::ResourceController`, or you
|
89
|
+
can use eject option for create a controller
|
90
|
+
that implements index, show, create, update and destroy methods.
|
91
|
+
- **Specs for the created controller**
|
92
|
+
- **Factory bot factory for created model**
|
93
|
+
- **Routes**: with rails resources
|
59
94
|
|
60
|
-
|
61
|
-
deletes instance of generated model from database
|
95
|
+
### Example
|
62
96
|
|
63
|
-
|
64
|
-
|
97
|
+
```bash
|
98
|
+
$ rails g rest_api_generator:resource car name:string color:string
|
99
|
+
```
|
65
100
|
|
66
|
-
|
67
|
-
returns JSON instance of generated model from database with status ok
|
101
|
+
Will generate following controller and the other files:
|
68
102
|
|
103
|
+
```ruby
|
104
|
+
# app/controllers/cars_controller.rb
|
105
|
+
class CarsController < RestApiGenerator::ResourceController
|
106
|
+
end
|
107
|
+
```
|
69
108
|
|
70
|
-
|
71
|
-
|
72
|
-
### Spec
|
109
|
+
For a better experience you can override some methods from the
|
110
|
+
[default controller](https://github.com/SwitchDreams/rest-api-generator/blob/main/lib/rest_api_generator/resource_controller.rb)
|
73
111
|
|
74
|
-
|
75
|
-
checks if instance was saved in database
|
112
|
+
### Options
|
76
113
|
|
77
|
-
|
78
|
-
|
114
|
+
| Option | Goal | Default | Usage Example |
|
115
|
+
|--------|--------------------------------------------------------------|---------|-----------------|
|
116
|
+
| father | Generate nested resource | nil | --father Users |
|
117
|
+
| scope | Scope the resource for other route or namespace organization | nil | --scope Api::V1 |
|
118
|
+
| eject | Eject the controller to high customization | false | true |
|
79
119
|
|
80
|
-
|
81
|
-
checks if instance was deleted from database
|
120
|
+
#### Scope
|
82
121
|
|
83
|
-
|
84
|
-
|
122
|
+
In REST api one of the best practices is versioning the end-points, and you can achieve this using scope options,
|
123
|
+
example:
|
85
124
|
|
86
|
-
|
87
|
-
|
125
|
+
```bash
|
126
|
+
# Command
|
127
|
+
rails g rest_api_generator:resource car name:string color:string --scope Api::V1
|
128
|
+
```
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
# GET api/v1/cars
|
132
|
+
module Api::V1
|
133
|
+
class CarsController < RestApiGenerator::ResourceController
|
134
|
+
end
|
135
|
+
end
|
136
|
+
```
|
137
|
+
|
138
|
+
For this option you need to manually setup routes, for this example:
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
# routes.rb
|
142
|
+
namespace :api do
|
143
|
+
namespace :v1 do
|
144
|
+
resources :cars
|
145
|
+
end
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
149
|
+
#### Nested resource
|
88
150
|
|
89
|
-
|
90
|
-
|
91
|
-
|
151
|
+
In REST api sometimes we need to build a nested resource, for example when we need to get all devices from a user, for
|
152
|
+
this we have nested resource option:
|
153
|
+
|
154
|
+
```bash
|
155
|
+
# Command
|
156
|
+
rails g rest_api_generator:resource Devices name:string color:string users:references --scope Users
|
157
|
+
```
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
# GET users/:user_id/devices
|
161
|
+
module Users
|
162
|
+
class DevicesController < RestApiGenerator::ChildResourceController
|
163
|
+
end
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
For this option you need to manually setup routes, for this example:
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
# routes.rb
|
171
|
+
resources :users do
|
172
|
+
resources :devices, controller: 'users/devices'
|
173
|
+
end
|
174
|
+
```
|
92
175
|
|
93
|
-
|
94
|
-
### Routes
|
95
|
-
the routes generated are the basic resources routes:
|
96
|
-
table_name: resources
|
176
|
+
Considerations:
|
97
177
|
|
98
|
-
|
99
|
-
### Scope
|
100
|
-
It's possible to add flag in the command to generate an endpoint with a parent
|
101
|
-
$ rails g generator table_name attributes --scope father_name
|
178
|
+
- The children model needs to belongs_to parent model and parent model needs to have has_many children model
|
102
179
|
|
103
|
-
|
180
|
+
#### Eject
|
181
|
+
|
182
|
+
Or you can use the `eject` option for create the controller with the implemented methods:
|
183
|
+
|
184
|
+
```bash
|
185
|
+
rails g rest_api_generator:resource car name:string color:string --eject true
|
186
|
+
```
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
|
190
|
+
class CarsController < ApplicationController
|
191
|
+
before_action :set_car, only: %i[show update destroy]
|
192
|
+
|
193
|
+
def index
|
194
|
+
@car = Car.all
|
195
|
+
render json: @car, status: :ok
|
196
|
+
end
|
197
|
+
|
198
|
+
def show
|
199
|
+
render json: @car, status: :ok
|
200
|
+
end
|
201
|
+
|
202
|
+
def create
|
203
|
+
@car = Car.create!(car_params)
|
204
|
+
render json: @car, status: :created
|
205
|
+
end
|
206
|
+
|
207
|
+
def update
|
208
|
+
@car = Car.update!(car_params)
|
209
|
+
render json: @car, status: :ok
|
210
|
+
end
|
211
|
+
|
212
|
+
def destroy
|
213
|
+
@car.destroy!
|
214
|
+
end
|
215
|
+
|
216
|
+
private
|
217
|
+
|
218
|
+
def set_car
|
219
|
+
@car = Car.find(params[:id])
|
220
|
+
end
|
221
|
+
|
222
|
+
def car_params
|
223
|
+
params.require(:car).permit(:name, :color)
|
224
|
+
end
|
225
|
+
end
|
226
|
+
```
|
227
|
+
|
228
|
+
### Resource Features
|
229
|
+
|
230
|
+
#### Modular Error Handler
|
231
|
+
|
232
|
+
The error module will return a json in this following format when any active record or custom error raises.
|
233
|
+
|
234
|
+
```json
|
235
|
+
{
|
236
|
+
"status": 422,
|
237
|
+
"error": "",
|
238
|
+
"message": ""
|
239
|
+
}
|
240
|
+
```
|
241
|
+
|
242
|
+
This is good to padronize the error handler in front-end too.
|
243
|
+
|
244
|
+
#### Ordering
|
245
|
+
|
246
|
+
For ordering use this format:
|
247
|
+
|
248
|
+
- Ordering asc: `GET /cars?sort=+name or GET /cars?sort=name`
|
249
|
+
- Ordering desc: `GET /card?sort=-name`
|
250
|
+
|
251
|
+
By default, every resource column can be the key for ordering.
|
252
|
+
|
253
|
+
#### Filtering
|
254
|
+
|
255
|
+
For filter is needed to add some scopes in Model file, example:
|
256
|
+
|
257
|
+
```ruby
|
258
|
+
# app/models/car.rb
|
259
|
+
|
260
|
+
class Car < ApplicationRecord
|
261
|
+
include RestApiGenerator::Filterable
|
262
|
+
|
263
|
+
filter_scope :filter_by_color, ->(color) { where(color: color) }
|
264
|
+
filter_scope :filter_by_name, ->(name) { where("name LIKE ?", "%#{name}%") }
|
265
|
+
end
|
266
|
+
```
|
267
|
+
|
268
|
+
And It's done, you can filter your index end-point:
|
269
|
+
|
270
|
+
- `GET /cars?color=blue or GET /cars?color=red&name=Ferrari`
|
104
271
|
|
105
272
|
## Development
|
106
273
|
|
107
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
|
274
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
|
275
|
+
also run `bin/console` for an interactive prompt that will allow you to experiment.
|
108
276
|
|
109
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
|
277
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
|
278
|
+
version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
|
279
|
+
push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
110
280
|
|
111
281
|
## Contributing
|
112
282
|
|
113
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
283
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/SwitchDreams/rest-api-generator. This project
|
284
|
+
is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
|
285
|
+
the [code of conduct](https://github.com/SwitchDreams/rest-api-generator/blob/main/CODE_OF_CONDUCT.md).
|
114
286
|
|
115
287
|
## License
|
116
288
|
|
117
289
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
118
|
-
|
119
|
-
## Code of Conduct
|
120
|
-
|
121
|
-
Everyone interacting in the Rest::Api::Generator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rest-api-generator/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
CHANGED
@@ -17,5 +17,35 @@ module RestApiGenerator
|
|
17
17
|
Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
|
18
18
|
end
|
19
19
|
end
|
20
|
+
|
21
|
+
def option_to_path(option)
|
22
|
+
option.downcase.split("::").join("/")
|
23
|
+
end
|
24
|
+
|
25
|
+
def scope_route_path
|
26
|
+
return "" if options["scope"].blank?
|
27
|
+
|
28
|
+
option_to_path(options["scope"])
|
29
|
+
end
|
30
|
+
|
31
|
+
def nested_routes
|
32
|
+
return "" if options["father"].blank?
|
33
|
+
|
34
|
+
"#{options["father"].downcase.pluralize}/\#{#{options["father"].singularize.downcase}.id}/#{plural_name}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def initial_route
|
38
|
+
scope_route_path + "/" + nested_routes
|
39
|
+
end
|
40
|
+
|
41
|
+
def spec_routes
|
42
|
+
{
|
43
|
+
index: initial_route,
|
44
|
+
show: initial_route + "\#{#{singular_name}.id}",
|
45
|
+
create: initial_route,
|
46
|
+
update: initial_route + "\#{#{singular_name}.id}",
|
47
|
+
delete: initial_route + "\#{#{singular_name}.id}",
|
48
|
+
}
|
49
|
+
end
|
20
50
|
end
|
21
51
|
end
|