rest-api-generator 0.1.7 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f7576268dec99ae0feafe5da3a552dae8d0609ccd244f5e62a99007e2f54090b
4
- data.tar.gz: 0f8a227490885ada7b83f1373c854857889e710b1d24285a81b45490308419a6
3
+ metadata.gz: a42f65c1630afe3db2bc1acb7c66272b9703e61ac761d509b0561e6c9369d7df
4
+ data.tar.gz: 898e903b7b81efa687f272a18ec7179f42ac566da404b1f046002b74345c9f77
5
5
  SHA512:
6
- metadata.gz: 19bbdd850d24d6168fda588274287ad8645b255165487261dd6413420ab395fdc150675d97ed57ac6e2d395f2bf7cc5d48632f3b43bfe2a11f06964aae71b1d7
7
- data.tar.gz: 4653ea0a74f2377c9052eba66bb16230096598f5f3e5d52f42ff5ad5141b763b4054359c7bf360083d7e08b517249bf44b17a9a6e64765f51861961075068c65
6
+ metadata.gz: 83e49d124a05cee3c742a69d5d2b1f798014e331c2a662a2bd7b0b19cb99ca45d66bdd290fa996e0c7e969d302fc9ecd49e9c69554836fe582d55adbb06f8f66
7
+ data.tar.gz: 14bd2117229d7cc2cb6b17371f7d0767720e3b6051ddcfc5fa6fb95aec438c29db25800ec5d44505284e4c56ba817e36666fa9118262228d5ab6086d740b0016
data/Gemfile CHANGED
@@ -12,3 +12,9 @@ gem "rspec", "~> 3.0"
12
12
  gem "switchcop"
13
13
 
14
14
  gem "sqlite3"
15
+
16
+ # Serializers
17
+
18
+ gem "panko_serializer"
19
+
20
+ gem "active_model_serializers", "~> 0.10.0"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rest-api-generator (0.1.7)
4
+ rest-api-generator (0.3.0)
5
5
  anyway_config (>= 2.0.0)
6
6
  pagy
7
7
  rails (>= 5.0)
@@ -9,86 +9,93 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- actioncable (7.0.4)
13
- actionpack (= 7.0.4)
14
- activesupport (= 7.0.4)
12
+ actioncable (7.0.4.3)
13
+ actionpack (= 7.0.4.3)
14
+ activesupport (= 7.0.4.3)
15
15
  nio4r (~> 2.0)
16
16
  websocket-driver (>= 0.6.1)
17
- actionmailbox (7.0.4)
18
- actionpack (= 7.0.4)
19
- activejob (= 7.0.4)
20
- activerecord (= 7.0.4)
21
- activestorage (= 7.0.4)
22
- activesupport (= 7.0.4)
17
+ actionmailbox (7.0.4.3)
18
+ actionpack (= 7.0.4.3)
19
+ activejob (= 7.0.4.3)
20
+ activerecord (= 7.0.4.3)
21
+ activestorage (= 7.0.4.3)
22
+ activesupport (= 7.0.4.3)
23
23
  mail (>= 2.7.1)
24
24
  net-imap
25
25
  net-pop
26
26
  net-smtp
27
- actionmailer (7.0.4)
28
- actionpack (= 7.0.4)
29
- actionview (= 7.0.4)
30
- activejob (= 7.0.4)
31
- activesupport (= 7.0.4)
27
+ actionmailer (7.0.4.3)
28
+ actionpack (= 7.0.4.3)
29
+ actionview (= 7.0.4.3)
30
+ activejob (= 7.0.4.3)
31
+ activesupport (= 7.0.4.3)
32
32
  mail (~> 2.5, >= 2.5.4)
33
33
  net-imap
34
34
  net-pop
35
35
  net-smtp
36
36
  rails-dom-testing (~> 2.0)
37
- actionpack (7.0.4)
38
- actionview (= 7.0.4)
39
- activesupport (= 7.0.4)
37
+ actionpack (7.0.4.3)
38
+ actionview (= 7.0.4.3)
39
+ activesupport (= 7.0.4.3)
40
40
  rack (~> 2.0, >= 2.2.0)
41
41
  rack-test (>= 0.6.3)
42
42
  rails-dom-testing (~> 2.0)
43
43
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
44
- actiontext (7.0.4)
45
- actionpack (= 7.0.4)
46
- activerecord (= 7.0.4)
47
- activestorage (= 7.0.4)
48
- activesupport (= 7.0.4)
44
+ actiontext (7.0.4.3)
45
+ actionpack (= 7.0.4.3)
46
+ activerecord (= 7.0.4.3)
47
+ activestorage (= 7.0.4.3)
48
+ activesupport (= 7.0.4.3)
49
49
  globalid (>= 0.6.0)
50
50
  nokogiri (>= 1.8.5)
51
- actionview (7.0.4)
52
- activesupport (= 7.0.4)
51
+ actionview (7.0.4.3)
52
+ activesupport (= 7.0.4.3)
53
53
  builder (~> 3.1)
54
54
  erubi (~> 1.4)
55
55
  rails-dom-testing (~> 2.0)
56
56
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
57
- activejob (7.0.4)
58
- activesupport (= 7.0.4)
57
+ active_model_serializers (0.10.13)
58
+ actionpack (>= 4.1, < 7.1)
59
+ activemodel (>= 4.1, < 7.1)
60
+ case_transform (>= 0.2)
61
+ jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
62
+ activejob (7.0.4.3)
63
+ activesupport (= 7.0.4.3)
59
64
  globalid (>= 0.3.6)
60
- activemodel (7.0.4)
61
- activesupport (= 7.0.4)
62
- activerecord (7.0.4)
63
- activemodel (= 7.0.4)
64
- activesupport (= 7.0.4)
65
- activestorage (7.0.4)
66
- actionpack (= 7.0.4)
67
- activejob (= 7.0.4)
68
- activerecord (= 7.0.4)
69
- activesupport (= 7.0.4)
65
+ activemodel (7.0.4.3)
66
+ activesupport (= 7.0.4.3)
67
+ activerecord (7.0.4.3)
68
+ activemodel (= 7.0.4.3)
69
+ activesupport (= 7.0.4.3)
70
+ activestorage (7.0.4.3)
71
+ actionpack (= 7.0.4.3)
72
+ activejob (= 7.0.4.3)
73
+ activerecord (= 7.0.4.3)
74
+ activesupport (= 7.0.4.3)
70
75
  marcel (~> 1.0)
71
76
  mini_mime (>= 1.1.0)
72
- activesupport (7.0.4)
77
+ activesupport (7.0.4.3)
73
78
  concurrent-ruby (~> 1.0, >= 1.0.2)
74
79
  i18n (>= 1.6, < 2)
75
80
  minitest (>= 5.1)
76
81
  tzinfo (~> 2.0)
77
- addressable (2.8.1)
82
+ addressable (2.8.4)
78
83
  public_suffix (>= 2.0.2, < 6.0)
79
84
  ammeter (1.1.5)
80
85
  activesupport (>= 3.0)
81
86
  railties (>= 3.0)
82
87
  rspec-rails (>= 2.2)
83
- anyway_config (2.4.0)
88
+ anyway_config (2.4.1)
84
89
  ruby-next-core (>= 0.14.0)
85
90
  ast (2.4.2)
86
91
  builder (3.2.4)
87
- concurrent-ruby (1.1.10)
92
+ case_transform (0.2)
93
+ activesupport
94
+ concurrent-ruby (1.2.2)
88
95
  crass (1.0.6)
89
- database_cleaner (2.0.1)
90
- database_cleaner-active_record (~> 2.0.0)
91
- database_cleaner-active_record (2.0.1)
96
+ database_cleaner (2.0.2)
97
+ database_cleaner-active_record (>= 2, < 3)
98
+ database_cleaner-active_record (2.1.0)
92
99
  activerecord (>= 5.a)
93
100
  database_cleaner-core (~> 2.0.0)
94
101
  database_cleaner-core (2.0.1)
@@ -97,14 +104,15 @@ GEM
97
104
  erubi (1.12.0)
98
105
  globalid (1.1.0)
99
106
  activesupport (>= 5.0)
100
- i18n (1.12.0)
107
+ i18n (1.13.0)
101
108
  concurrent-ruby (~> 1.0)
102
109
  json (2.6.3)
103
110
  json-schema (3.0.0)
104
111
  addressable (>= 2.8)
105
- loofah (2.19.1)
112
+ jsonapi-renderer (0.2.2)
113
+ loofah (2.21.2)
106
114
  crass (~> 1.0.2)
107
- nokogiri (>= 1.5.9)
115
+ nokogiri (>= 1.12.0)
108
116
  mail (2.8.1)
109
117
  mini_mime (>= 0.1.1)
110
118
  net-imap
@@ -113,8 +121,8 @@ GEM
113
121
  marcel (1.0.2)
114
122
  method_source (1.0.0)
115
123
  mini_mime (1.1.2)
116
- mini_portile2 (2.8.1)
117
- minitest (5.16.3)
124
+ mini_portile2 (2.8.2)
125
+ minitest (5.18.0)
118
126
  net-imap (0.3.4)
119
127
  date
120
128
  net-protocol
@@ -125,136 +133,148 @@ GEM
125
133
  net-smtp (0.3.3)
126
134
  net-protocol
127
135
  nio4r (2.5.9)
128
- nokogiri (1.13.10)
136
+ nokogiri (1.14.4)
129
137
  mini_portile2 (~> 2.8.0)
130
138
  racc (~> 1.4)
131
- nokogiri (1.13.10-x86_64-linux)
139
+ nokogiri (1.14.4-x86_64-linux)
132
140
  racc (~> 1.4)
133
- pagy (6.0.3)
134
- parallel (1.22.1)
135
- parser (3.1.3.0)
141
+ oj (3.14.3)
142
+ pagy (6.0.4)
143
+ panko_serializer (0.7.9)
144
+ activesupport
145
+ oj (> 3.11.0, < 4.0.0)
146
+ parallel (1.23.0)
147
+ parser (3.2.2.1)
136
148
  ast (~> 2.4.1)
137
149
  public_suffix (5.0.1)
138
150
  racc (1.6.2)
139
- rack (2.2.4)
140
- rack-test (2.0.2)
151
+ rack (2.2.7)
152
+ rack-test (2.1.0)
141
153
  rack (>= 1.3)
142
- rails (7.0.4)
143
- actioncable (= 7.0.4)
144
- actionmailbox (= 7.0.4)
145
- actionmailer (= 7.0.4)
146
- actionpack (= 7.0.4)
147
- actiontext (= 7.0.4)
148
- actionview (= 7.0.4)
149
- activejob (= 7.0.4)
150
- activemodel (= 7.0.4)
151
- activerecord (= 7.0.4)
152
- activestorage (= 7.0.4)
153
- activesupport (= 7.0.4)
154
+ rails (7.0.4.3)
155
+ actioncable (= 7.0.4.3)
156
+ actionmailbox (= 7.0.4.3)
157
+ actionmailer (= 7.0.4.3)
158
+ actionpack (= 7.0.4.3)
159
+ actiontext (= 7.0.4.3)
160
+ actionview (= 7.0.4.3)
161
+ activejob (= 7.0.4.3)
162
+ activemodel (= 7.0.4.3)
163
+ activerecord (= 7.0.4.3)
164
+ activestorage (= 7.0.4.3)
165
+ activesupport (= 7.0.4.3)
154
166
  bundler (>= 1.15.0)
155
- railties (= 7.0.4)
167
+ railties (= 7.0.4.3)
156
168
  rails-dom-testing (2.0.3)
157
169
  activesupport (>= 4.2.0)
158
170
  nokogiri (>= 1.6)
159
- rails-html-sanitizer (1.4.4)
171
+ rails-html-sanitizer (1.5.0)
160
172
  loofah (~> 2.19, >= 2.19.1)
161
- railties (7.0.4)
162
- actionpack (= 7.0.4)
163
- activesupport (= 7.0.4)
173
+ railties (7.0.4.3)
174
+ actionpack (= 7.0.4.3)
175
+ activesupport (= 7.0.4.3)
164
176
  method_source
165
177
  rake (>= 12.2)
166
178
  thor (~> 1.0)
167
179
  zeitwerk (~> 2.5)
168
180
  rainbow (3.1.1)
169
181
  rake (13.0.6)
170
- regexp_parser (2.6.1)
182
+ regexp_parser (2.8.0)
171
183
  rexml (3.2.5)
172
184
  rspec (3.12.0)
173
185
  rspec-core (~> 3.12.0)
174
186
  rspec-expectations (~> 3.12.0)
175
187
  rspec-mocks (~> 3.12.0)
176
- rspec-core (3.12.0)
188
+ rspec-core (3.12.2)
177
189
  rspec-support (~> 3.12.0)
178
- rspec-expectations (3.12.1)
190
+ rspec-expectations (3.12.3)
179
191
  diff-lcs (>= 1.2.0, < 2.0)
180
192
  rspec-support (~> 3.12.0)
181
- rspec-mocks (3.12.1)
193
+ rspec-mocks (3.12.5)
182
194
  diff-lcs (>= 1.2.0, < 2.0)
183
195
  rspec-support (~> 3.12.0)
184
- rspec-rails (6.0.1)
196
+ rspec-rails (6.0.2)
185
197
  actionpack (>= 6.1)
186
198
  activesupport (>= 6.1)
187
199
  railties (>= 6.1)
188
- rspec-core (~> 3.11)
189
- rspec-expectations (~> 3.11)
190
- rspec-mocks (~> 3.11)
191
- rspec-support (~> 3.11)
200
+ rspec-core (~> 3.12)
201
+ rspec-expectations (~> 3.12)
202
+ rspec-mocks (~> 3.12)
203
+ rspec-support (~> 3.12)
192
204
  rspec-support (3.12.0)
193
- rswag (2.8.0)
194
- rswag-api (= 2.8.0)
195
- rswag-specs (= 2.8.0)
196
- rswag-ui (= 2.8.0)
197
- rswag-api (2.8.0)
205
+ rswag (2.9.0)
206
+ rswag-api (= 2.9.0)
207
+ rswag-specs (= 2.9.0)
208
+ rswag-ui (= 2.9.0)
209
+ rswag-api (2.9.0)
198
210
  railties (>= 3.1, < 7.1)
199
- rswag-specs (2.8.0)
211
+ rswag-specs (2.9.0)
200
212
  activesupport (>= 3.1, < 7.1)
201
213
  json-schema (>= 2.2, < 4.0)
202
214
  railties (>= 3.1, < 7.1)
203
215
  rspec-core (>= 2.14)
204
- rswag-ui (2.8.0)
216
+ rswag-ui (2.9.0)
205
217
  actionpack (>= 3.1, < 7.1)
206
218
  railties (>= 3.1, < 7.1)
207
- rubocop (1.40.0)
219
+ rubocop (1.51.0)
208
220
  json (~> 2.3)
209
221
  parallel (~> 1.10)
210
- parser (>= 3.1.2.1)
222
+ parser (>= 3.2.0.0)
211
223
  rainbow (>= 2.2.2, < 4.0)
212
224
  regexp_parser (>= 1.8, < 3.0)
213
225
  rexml (>= 3.2.5, < 4.0)
214
- rubocop-ast (>= 1.23.0, < 2.0)
226
+ rubocop-ast (>= 1.28.0, < 2.0)
215
227
  ruby-progressbar (~> 1.7)
216
- unicode-display_width (>= 1.4.0, < 3.0)
217
- rubocop-ast (1.24.0)
218
- parser (>= 3.1.1.0)
219
- rubocop-performance (1.15.2)
228
+ unicode-display_width (>= 2.4.0, < 3.0)
229
+ rubocop-ast (1.28.1)
230
+ parser (>= 3.2.1.0)
231
+ rubocop-capybara (2.18.0)
232
+ rubocop (~> 1.41)
233
+ rubocop-factory_bot (2.22.0)
234
+ rubocop (~> 1.33)
235
+ rubocop-performance (1.17.1)
220
236
  rubocop (>= 1.7.0, < 2.0)
221
237
  rubocop-ast (>= 0.4.0)
222
- rubocop-rails (2.17.4)
238
+ rubocop-rails (2.19.1)
223
239
  activesupport (>= 4.2.0)
224
240
  rack (>= 1.1)
225
241
  rubocop (>= 1.33.0, < 2.0)
226
- rubocop-rspec (2.16.0)
242
+ rubocop-rspec (2.22.0)
227
243
  rubocop (~> 1.33)
228
- rubocop-shopify (2.10.1)
229
- rubocop (~> 1.35)
244
+ rubocop-capybara (~> 2.17)
245
+ rubocop-factory_bot (~> 2.22)
246
+ rubocop-shopify (2.13.0)
247
+ rubocop (~> 1.50)
230
248
  ruby-next-core (0.15.3)
231
- ruby-progressbar (1.11.0)
232
- sqlite3 (1.5.4)
249
+ ruby-progressbar (1.13.0)
250
+ sqlite3 (1.6.2)
233
251
  mini_portile2 (~> 2.8.0)
234
- sqlite3 (1.5.4-x86_64-linux)
235
- switchcop (0.1.2)
236
- rubocop (~> 1.40.0)
237
- rubocop-performance (~> 1.15.1)
238
- rubocop-rails (~> 2.17.3)
239
- rubocop-rspec (~> 2.16.0)
240
- rubocop-shopify (~> 2.10.1)
241
- thor (1.2.1)
252
+ sqlite3 (1.6.2-x86_64-linux)
253
+ switchcop (0.1.3)
254
+ rubocop (~> 1.48)
255
+ rubocop-performance (~> 1.16)
256
+ rubocop-rails (~> 2.18)
257
+ rubocop-rspec (~> 2.19)
258
+ rubocop-shopify (~> 2.12)
259
+ thor (1.2.2)
242
260
  timeout (0.3.2)
243
- tzinfo (2.0.5)
261
+ tzinfo (2.0.6)
244
262
  concurrent-ruby (~> 1.0)
245
- unicode-display_width (2.3.0)
263
+ unicode-display_width (2.4.2)
246
264
  websocket-driver (0.7.5)
247
265
  websocket-extensions (>= 0.1.0)
248
266
  websocket-extensions (0.1.5)
249
- zeitwerk (2.6.6)
267
+ zeitwerk (2.6.8)
250
268
 
251
269
  PLATFORMS
252
270
  ruby
253
271
  x86_64-linux
254
272
 
255
273
  DEPENDENCIES
274
+ active_model_serializers (~> 0.10.0)
256
275
  ammeter (~> 1.1.5)
257
276
  database_cleaner
277
+ panko_serializer
258
278
  rake (~> 13.0)
259
279
  rest-api-generator!
260
280
  rspec (~> 3.0)
data/README.md CHANGED
@@ -3,6 +3,11 @@
3
3
  This gem helps you to build a Ruby on Rails REST API faster, using a scaffold-like generator that follows the best
4
4
  practices.
5
5
 
6
+ ## Get started
7
+
8
+ :zap: **Quick Start**: [docs](https://rest-api-generator.switchdreams.com.br/quick-start)\
9
+ :books: **Documentation**: [docs](https://rest-api-generator.switchdreams.com.br/)
10
+
6
11
  ## How it works?
7
12
 
8
13
  The gems use vanilla Rails generators in combination with our templates to create all the resources needed to build a
@@ -25,319 +30,16 @@ Following [Switch Dreams's](https://www.switchdreams.com.br/]) coding practices,
25
30
  - [Resource ordering](#ordering)
26
31
  - [Resource filter](#filtering)
27
32
  - [Resource pagination](#pagination)
33
+ - [Resource serialization](#serialization)
28
34
  - [Configurable](#configuration)
35
+ - [Callbacks](#callbacks)
29
36
 
30
37
  ## Next Features
31
38
 
32
- - Serialization https://github.com/SwitchDreams/rest-api-generator/issues/14
33
- https://github.com/SwitchDreams/rest-api-generator/issues/11
34
- - Pagination https://github.com/SwitchDreams/rest-api-generator/issues/15
35
39
  - Integration with AVO
36
40
  - Select fields
37
41
  - User auth module
38
42
 
39
- ## Installation
40
-
41
- Add this line to your application's Gemfile:
42
-
43
- ```ruby
44
- # Build a Ruby on Rails REST API faster
45
- gem 'rest-api-generator'
46
- ```
47
-
48
- And then execute:
49
-
50
- $ bundle install
51
-
52
- Or install it yourself as:
53
-
54
- $ gem install rest-api-generator
55
-
56
- ## Requirements
57
-
58
- 1. You need to have installed RSpec and FactoryBot in your application.
59
-
60
- <ul>
61
- <li>RSpec: https://github.com/rspec/rspec-rails</li>
62
- <li>Factory bot: https://github.com/thoughtbot/factory_bot_rails</li>
63
- </ul>
64
-
65
- 2. Include in ApplicationController the error handler module:
66
-
67
- ```ruby
68
-
69
- class ApplicationController < ActionController::API
70
- include RestApiGenerator::ErrorHandler
71
- end
72
- ```
73
-
74
- This error handler will rescue from: `ActiveRecord::RecordNotFound`
75
- , `ActiveRecord::ActiveRecordError`, `ActiveRecord::RecordInvalid`, `ActiveModel::ValidationError`
76
- , `RestApiGenerator::CustomError`.
77
-
78
- ## Usage
79
-
80
- ### Generate Resource
81
-
82
- ```bash
83
- $ rails g rest_api_generator:resource table_name attributes
84
- ```
85
-
86
- This command will create:
87
-
88
- - **Model and Migration**: Using rails default model generator
89
- - **Controller**: A controller that implementes CRUD by inheritance of `RestApiGenerator::ResourceController`, or you
90
- can use eject option for create a controller
91
- that implements index, show, create, update and destroy methods.
92
- - **Specs for the created controller**
93
- - **Factory bot factory for created model**
94
- - **Routes**: with rails resources
95
-
96
- ### Example
97
-
98
- ```bash
99
- $ rails g rest_api_generator:resource car name:string color:string
100
- ```
101
-
102
- Will generate following controller and the other files:
103
-
104
- ```ruby
105
- # app/controllers/cars_controller.rb
106
- class CarsController < RestApiGenerator::ResourceController
107
- end
108
- ```
109
-
110
- For a better experience you can override some methods from the
111
- [default controller](https://github.com/SwitchDreams/rest-api-generator/blob/main/lib/rest_api_generator/resource_controller.rb)
112
-
113
- ### Options
114
-
115
- | Option | Goal | Default | Usage Example |
116
- |--------|--------------------------------------------------------------|---------|-----------------|
117
- | father | Generate nested resource | nil | --father Users |
118
- | scope | Scope the resource for other route or namespace organization | nil | --scope Api::V1 |
119
- | eject | Eject the controller to high customization | false | true |
120
- | spec | Choose the spec format. Current options: "rspec" or "rswag" | rspec | --spec rswag |
121
-
122
- #### Scope
123
-
124
- In REST api one of the best practices is versioning the end-points, and you can achieve this using scope options,
125
- example:
126
-
127
- ```bash
128
- # Command
129
- rails g rest_api_generator:resource car name:string color:string --scope Api::V1
130
- ```
131
-
132
- ```ruby
133
- # GET api/v1/cars
134
- module Api::V1
135
- class CarsController < RestApiGenerator::ResourceController
136
- end
137
- end
138
- ```
139
-
140
- For this option you need to manually setup routes, for this example:
141
-
142
- ```ruby
143
- # routes.rb
144
- namespace :api do
145
- namespace :v1 do
146
- resources :cars
147
- end
148
- end
149
- ```
150
-
151
- #### Nested resource
152
-
153
- In REST api sometimes we need to build a nested resource, for example when we need to get all devices from a user, for
154
- this we have nested resource option:
155
-
156
- ```bash
157
- # Command
158
- rails g rest_api_generator:resource Devices name:string color:string users:references --scope Users
159
- ```
160
-
161
- ```ruby
162
- # GET users/:user_id/devices
163
- module Users
164
- class DevicesController < RestApiGenerator::ChildResourceController
165
- end
166
- end
167
- ```
168
-
169
- For this option you need to manually setup routes, for this example:
170
-
171
- ```ruby
172
- # routes.rb
173
- resources :users do
174
- resources :devices, controller: 'users/devices'
175
- end
176
- ```
177
-
178
- Considerations:
179
-
180
- - The children model needs to belongs_to parent model and parent model needs to have has_many children model
181
-
182
- #### Eject
183
-
184
- Or you can use the `eject` option for create the controller with the implemented methods:
185
-
186
- ```bash
187
- rails g rest_api_generator:resource car name:string color:string --eject true
188
- ```
189
-
190
- ```ruby
191
-
192
- class CarsController < ApplicationController
193
- before_action :set_car, only: %i[show update destroy]
194
-
195
- def index
196
- @car = Car.all
197
- render json: @car, status: :ok
198
- end
199
-
200
- def show
201
- render json: @car, status: :ok
202
- end
203
-
204
- def create
205
- @car = Car.create!(car_params)
206
- render json: @car, status: :created
207
- end
208
-
209
- def update
210
- @car = Car.update!(car_params)
211
- render json: @car, status: :ok
212
- end
213
-
214
- def destroy
215
- @car.destroy!
216
- end
217
-
218
- private
219
-
220
- def set_car
221
- @car = Car.find(params[:id])
222
- end
223
-
224
- def car_params
225
- params.require(:car).permit(:name, :color)
226
- end
227
- end
228
- ```
229
-
230
- #### Specs/Docs
231
-
232
- The default generated spec for this gem is using plain rspec, but you can choose rswag, for scaffold you specs and docs
233
- at the same time:
234
-
235
- For this you need to setup https://github.com/rswag/rswag and you the following flag when generating resources.
236
-
237
- ```shell
238
- rails g rest_api_generator:resource Car name:string color:string --spec rswag
239
- ```
240
-
241
- This spec options work as generators too, so you can call them individually:
242
-
243
- ```shell
244
- # rest_api_generator:spec:rswag or rest_api_generator:spec:rspec
245
- rails g rest_api_generator:spec:rswag Car name:string color:string
246
- ```
247
-
248
- #### Configuration for specs
249
-
250
- By default, the plain rspec and rswag specs are going to be generated in the _spec/requests_ and _spec/docs_
251
- directories, respectively. You can override using the (config options)[#configuration]]. :
252
-
253
-
254
- ### Resource Features
255
-
256
- #### Modular Error Handler
257
-
258
- The error module will return a json in this following format when any active record or custom error raises.
259
-
260
- ```json
261
- {
262
- "status": 422,
263
- "error": "",
264
- "message": ""
265
- }
266
- ```
267
-
268
- This is good to padronize the error handler in front-end too.
269
-
270
- #### Ordering
271
-
272
- For ordering use this format:
273
-
274
- - Ordering asc: `GET /cars?sort=+name or GET /cars?sort=name`
275
- - Ordering desc: `GET /card?sort=-name`
276
-
277
- By default, every resource column can be the key for ordering.
278
-
279
- #### Filtering
280
-
281
- For filter is needed to add some scopes in Model file, example:
282
-
283
- ```ruby
284
- # app/models/car.rb
285
-
286
- class Car < ApplicationRecord
287
- include RestApiGenerator::Filterable
288
-
289
- filter_scope :filter_by_color, ->(color) { where(color: color) }
290
- filter_scope :filter_by_name, ->(name) { where("name LIKE ?", "%#{name}%") }
291
- end
292
- ```
293
-
294
- And It's done, you can filter your index end-point:
295
-
296
- - `GET /cars?color=blue or GET /cars?color=red&name=Ferrari`
297
-
298
- ### Pagination
299
-
300
- For pagination, you need to create pagy initialializer file (pagy.rb) in the config directory of your project. Follow [pagy's example](https://ddnexus.github.io/pagy/quick-start/) for more information.
301
-
302
- Next, you should add some lines on top of the previously created pagy file:
303
-
304
- ```ruby
305
- # config/initializers/pagy.rb
306
- require "pagy"
307
- require "pagy/extras/headers"
308
- ```
309
-
310
- At last, change the pagination variable on RestApiGenerator initializer to true;
311
-
312
- ```rb
313
- # config/initializers/rest_api_generator.rb
314
- config.pagination = true # default: false
315
- ```
316
-
317
- Note, if the parent controller is changed, it is necessary to include Pagy::Backend in the new parent.
318
-
319
- ```rb
320
- # new_parent_controller.rb
321
- class NewParentController < ActionController::Base
322
- include Pagy::Backend
323
- end
324
- ```
325
-
326
- ## Configuration
327
-
328
- You can override this gem configuration using the initializer or any other method from [anyway_config](https://github.com/palkan/anyway_config):
329
-
330
- ```rb
331
- # config/initializers/rest_api_generator.rb
332
-
333
- RestApiGenerator.configure do |config|
334
- config.test_path = "custom_test_dir/requests" # default: spec/requests
335
- config.docs_path = "custom_docs_dir/rswag" # default: spec/docs
336
- config.parent_class = "ApplicationController" # default: RestApiGenerator::ResourceController
337
- config.pagination = true # default: false
338
- end
339
- ```
340
-
341
43
  ## Development
342
44
 
343
45
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
@@ -2,7 +2,9 @@
2
2
 
3
3
  module RestApiGenerator
4
4
  class ChildResourceController < RestApiGenerator.configuration.parent_controller.constantize
5
+ include ControllerCallbacks
5
6
  include Orderable
7
+ include Serializable
6
8
 
7
9
  before_action :set_parent_resource
8
10
  before_action :set_resource, only: [:show, :update, :destroy]
@@ -15,21 +17,21 @@ module RestApiGenerator
15
17
  @pagy, @resources = pagy(@resources)
16
18
  pagy_headers_merge(@pagy)
17
19
  end
18
- render json: @resources, status: :ok
20
+ render json: index_serializer(@resources), status: :ok
19
21
  end
20
22
 
21
23
  def show
22
- render json: @resource, status: :ok
24
+ render json: serializer(@resource), status: :ok
23
25
  end
24
26
 
25
27
  def create
26
28
  @resource = resources.create!(resource_created_params)
27
- render json: @resource, status: :created
29
+ render json: serializer(@resource), status: :created
28
30
  end
29
31
 
30
32
  def update
31
33
  @resource.update!(resource_updated_params)
32
- render json: @resource, status: :ok
34
+ render json: serializer(@resource), status: :ok
33
35
  end
34
36
 
35
37
  def destroy
@@ -73,11 +75,15 @@ module RestApiGenerator
73
75
 
74
76
  # Before actions
75
77
  def set_parent_resource
76
- @parent_resource = parent_resource_class.find(parent_record_id)
78
+ run_callbacks :set_parent_resource do
79
+ @parent_resource = parent_resource_class.find(parent_record_id)
80
+ end
77
81
  end
78
82
 
79
83
  def set_resource
80
- @resource = resources.find(record_id)
84
+ run_callbacks :set_resource do
85
+ @resource = resources.find(record_id)
86
+ end
81
87
  end
82
88
 
83
89
  # UsersController => User
@@ -2,10 +2,11 @@
2
2
 
3
3
  module RestApiGenerator
4
4
  class ResourceController < RestApiGenerator.configuration.parent_controller.constantize
5
+ include ControllerCallbacks
5
6
  include Orderable
7
+ include Serializable
6
8
 
7
9
  before_action :set_resource, only: [:show, :update, :destroy]
8
-
9
10
  def index
10
11
  @resources = resource_class.all
11
12
  @resources = @resources.filter_resource(params_for_filter) if resource_class.include?(Filterable)
@@ -14,21 +15,21 @@ module RestApiGenerator
14
15
  @pagy, @resources = pagy(@resources)
15
16
  pagy_headers_merge(@pagy)
16
17
  end
17
- render json: @resources, status: :ok
18
+ render json: index_serializer(@resources), status: :ok
18
19
  end
19
20
 
20
21
  def show
21
- render json: @resource, status: :ok
22
+ render json: serializer(@resource), status: :ok
22
23
  end
23
24
 
24
25
  def create
25
26
  @resource = resource_class.create!(resource_created_params)
26
- render json: @resource, status: :created
27
+ render json: serializer(@resource), status: :created
27
28
  end
28
29
 
29
30
  def update
30
31
  @resource.update!(resource_updated_params)
31
- render json: @resource, status: :ok
32
+ render json: serializer(@resource), status: :ok
32
33
  end
33
34
 
34
35
  def destroy
@@ -62,7 +63,9 @@ module RestApiGenerator
62
63
  end
63
64
 
64
65
  def set_resource
65
- @resource = resource_class.find(record_id)
66
+ run_callbacks :set_resource do
67
+ @resource = resource_class.find(record_id)
68
+ end
66
69
  end
67
70
 
68
71
  # UsersController => User
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RestApiGenerator
4
+ module ControllerCallbacks
5
+ extend ActiveSupport::Concern
6
+ include ActiveSupport::Callbacks
7
+
8
+ included do
9
+ define_callbacks :set_resource
10
+ define_callbacks :set_parent_resource
11
+ end
12
+
13
+ module ClassMethods
14
+ # Code from rails source code
15
+ [:before, :after, :around].each do |callback|
16
+ define_method "#{callback}_set_resource" do |*names, &blk|
17
+ _insert_callbacks(names, blk) do |name, options|
18
+ set_callback(:set_resource, callback, name, options)
19
+ end
20
+ end
21
+
22
+ define_method "#{callback}_set_parent_resource" do |*names, &blk|
23
+ _insert_callbacks(names, blk) do |name, options|
24
+ set_callback(:set_parent_resource, callback, name, options)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/concern"
5
+
6
+ module RestApiGenerator
7
+ module Serializable
8
+ extend ActiveSupport::Concern
9
+
10
+ def serializer(resource)
11
+ resource
12
+ end
13
+
14
+ def index_serializer(resources)
15
+ serializer(resources)
16
+ end
17
+ end
18
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RestApiGenerator
4
- VERSION = "0.1.7"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -8,6 +8,8 @@ require_relative "rest_api_generator/custom_error"
8
8
  require_relative "rest_api_generator/helpers/render"
9
9
  require_relative "rest_api_generator/filterable"
10
10
  require_relative "rest_api_generator/orderable"
11
+ require_relative "rest_api_generator/serializable"
12
+ require_relative "rest_api_generator/controller_callbacks"
11
13
 
12
14
  module RestApiGenerator
13
15
  class Error < StandardError; end
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.metadata["homepage_uri"] = spec.homepage
22
22
  spec.metadata["source_code_uri"] = "https://github.com/SwitchDreams/rest-api-generator"
23
23
  spec.metadata["changelog_uri"] = "https://github.com/SwitchDreams/rest-api-generator"
24
+ spec.metadata["github_repo"] = "https://github.com/SwitchDreams/rest-api-generator"
24
25
 
25
26
  spec.files = Dir["{bin,sig,lib,public,app}/**/*",
26
27
  "MIT-LICENSE",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-api-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - PedroAugustoRamalhoDuarte
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-26 00:00:00.000000000 Z
11
+ date: 2023-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: anyway_config
@@ -155,12 +155,14 @@ files:
155
155
  - lib/rest-api-generator.rb
156
156
  - lib/rest_api_generator.rb
157
157
  - lib/rest_api_generator/config.rb
158
+ - lib/rest_api_generator/controller_callbacks.rb
158
159
  - lib/rest_api_generator/custom_error.rb
159
160
  - lib/rest_api_generator/error_handler.rb
160
161
  - lib/rest_api_generator/filterable.rb
161
162
  - lib/rest_api_generator/helpers/render.rb
162
163
  - lib/rest_api_generator/orderable.rb
163
164
  - lib/rest_api_generator/rails.rb
165
+ - lib/rest_api_generator/serializable.rb
164
166
  - lib/rest_api_generator/version.rb
165
167
  - rest-api-generator.gemspec
166
168
  - sig/rest_api_generator.rbs
@@ -172,6 +174,7 @@ metadata:
172
174
  homepage_uri: https://github.com/SwitchDreams/rest-api-generator
173
175
  source_code_uri: https://github.com/SwitchDreams/rest-api-generator
174
176
  changelog_uri: https://github.com/SwitchDreams/rest-api-generator
177
+ github_repo: https://github.com/SwitchDreams/rest-api-generator
175
178
  rubygems_mfa_required: 'true'
176
179
  post_install_message:
177
180
  rdoc_options: []