rest-api-generator 0.1.6 → 0.2.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: 2856805f0d16c6778be959778e29681c2e6ce306d9b3699e6e766da7f605948b
4
- data.tar.gz: f9ffabaa5ee380f54ab19a8ad88ddd58de27ee968e44e4ad64794d0110b6979f
3
+ metadata.gz: b15131fc7fcedabae2098ef8f2e5e255683acda454d022304ea855ac9ce0a98a
4
+ data.tar.gz: 413967d92aca8c5c65075a8dfd5d0159be5c00b29a918b5bc3a39275217b3b6b
5
5
  SHA512:
6
- metadata.gz: e92f35591532264731ac9acd69117082954c1ac342a89f298d40c60a11dfee9b02b9f2bc5f9e75251ced9937929d900bbfc8491a00b070220bed04c73f81f014
7
- data.tar.gz: 5914b7f9b6eb86efbf11ffb1497db57fa9ffec03ce9d4978cfae19e3d6425c52b5ab2fdc564c7baf3ec5acad478ec9d86cbccef19be253389176aee8bb7a2a3c
6
+ metadata.gz: 439eb464b272818413629d6ca448c1e976603d97543600f521ff9386ba47e514c4a380b367c087a75a96cdeb5a501d707b365ca5d52d35a4cd3adf08fbddf9ec
7
+ data.tar.gz: 79ec324210477f42878cadebaf87566083c6db14251de20206a2cb2a98f2fd9bcbaba969170ecc3cfcee5dae89a51a37381aeaddb29fe30c7fd4d817d769f2de
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,93 +1,101 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rest-api-generator (0.1.5)
4
+ rest-api-generator (0.2.0)
5
5
  anyway_config (>= 2.0.0)
6
+ pagy
6
7
  rails (>= 5.0)
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
10
11
  specs:
11
- actioncable (7.0.4)
12
- actionpack (= 7.0.4)
13
- activesupport (= 7.0.4)
12
+ actioncable (7.0.4.3)
13
+ actionpack (= 7.0.4.3)
14
+ activesupport (= 7.0.4.3)
14
15
  nio4r (~> 2.0)
15
16
  websocket-driver (>= 0.6.1)
16
- actionmailbox (7.0.4)
17
- actionpack (= 7.0.4)
18
- activejob (= 7.0.4)
19
- activerecord (= 7.0.4)
20
- activestorage (= 7.0.4)
21
- 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)
22
23
  mail (>= 2.7.1)
23
24
  net-imap
24
25
  net-pop
25
26
  net-smtp
26
- actionmailer (7.0.4)
27
- actionpack (= 7.0.4)
28
- actionview (= 7.0.4)
29
- activejob (= 7.0.4)
30
- 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)
31
32
  mail (~> 2.5, >= 2.5.4)
32
33
  net-imap
33
34
  net-pop
34
35
  net-smtp
35
36
  rails-dom-testing (~> 2.0)
36
- actionpack (7.0.4)
37
- actionview (= 7.0.4)
38
- activesupport (= 7.0.4)
37
+ actionpack (7.0.4.3)
38
+ actionview (= 7.0.4.3)
39
+ activesupport (= 7.0.4.3)
39
40
  rack (~> 2.0, >= 2.2.0)
40
41
  rack-test (>= 0.6.3)
41
42
  rails-dom-testing (~> 2.0)
42
43
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
43
- actiontext (7.0.4)
44
- actionpack (= 7.0.4)
45
- activerecord (= 7.0.4)
46
- activestorage (= 7.0.4)
47
- 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)
48
49
  globalid (>= 0.6.0)
49
50
  nokogiri (>= 1.8.5)
50
- actionview (7.0.4)
51
- activesupport (= 7.0.4)
51
+ actionview (7.0.4.3)
52
+ activesupport (= 7.0.4.3)
52
53
  builder (~> 3.1)
53
54
  erubi (~> 1.4)
54
55
  rails-dom-testing (~> 2.0)
55
56
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
56
- activejob (7.0.4)
57
- 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)
58
64
  globalid (>= 0.3.6)
59
- activemodel (7.0.4)
60
- activesupport (= 7.0.4)
61
- activerecord (7.0.4)
62
- activemodel (= 7.0.4)
63
- activesupport (= 7.0.4)
64
- activestorage (7.0.4)
65
- actionpack (= 7.0.4)
66
- activejob (= 7.0.4)
67
- activerecord (= 7.0.4)
68
- 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)
69
75
  marcel (~> 1.0)
70
76
  mini_mime (>= 1.1.0)
71
- activesupport (7.0.4)
77
+ activesupport (7.0.4.3)
72
78
  concurrent-ruby (~> 1.0, >= 1.0.2)
73
79
  i18n (>= 1.6, < 2)
74
80
  minitest (>= 5.1)
75
81
  tzinfo (~> 2.0)
76
- addressable (2.8.1)
82
+ addressable (2.8.4)
77
83
  public_suffix (>= 2.0.2, < 6.0)
78
84
  ammeter (1.1.5)
79
85
  activesupport (>= 3.0)
80
86
  railties (>= 3.0)
81
87
  rspec-rails (>= 2.2)
82
- anyway_config (2.4.0)
88
+ anyway_config (2.4.1)
83
89
  ruby-next-core (>= 0.14.0)
84
90
  ast (2.4.2)
85
91
  builder (3.2.4)
86
- concurrent-ruby (1.1.10)
92
+ case_transform (0.2)
93
+ activesupport
94
+ concurrent-ruby (1.2.2)
87
95
  crass (1.0.6)
88
- database_cleaner (2.0.1)
89
- database_cleaner-active_record (~> 2.0.0)
90
- 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)
91
99
  activerecord (>= 5.a)
92
100
  database_cleaner-core (~> 2.0.0)
93
101
  database_cleaner-core (2.0.1)
@@ -96,14 +104,15 @@ GEM
96
104
  erubi (1.12.0)
97
105
  globalid (1.1.0)
98
106
  activesupport (>= 5.0)
99
- i18n (1.12.0)
107
+ i18n (1.13.0)
100
108
  concurrent-ruby (~> 1.0)
101
109
  json (2.6.3)
102
110
  json-schema (3.0.0)
103
111
  addressable (>= 2.8)
104
- loofah (2.19.1)
112
+ jsonapi-renderer (0.2.2)
113
+ loofah (2.21.2)
105
114
  crass (~> 1.0.2)
106
- nokogiri (>= 1.5.9)
115
+ nokogiri (>= 1.12.0)
107
116
  mail (2.8.1)
108
117
  mini_mime (>= 0.1.1)
109
118
  net-imap
@@ -112,8 +121,8 @@ GEM
112
121
  marcel (1.0.2)
113
122
  method_source (1.0.0)
114
123
  mini_mime (1.1.2)
115
- mini_portile2 (2.8.1)
116
- minitest (5.16.3)
124
+ mini_portile2 (2.8.2)
125
+ minitest (5.18.0)
117
126
  net-imap (0.3.4)
118
127
  date
119
128
  net-protocol
@@ -124,135 +133,148 @@ GEM
124
133
  net-smtp (0.3.3)
125
134
  net-protocol
126
135
  nio4r (2.5.9)
127
- nokogiri (1.13.10)
136
+ nokogiri (1.14.4)
128
137
  mini_portile2 (~> 2.8.0)
129
138
  racc (~> 1.4)
130
- nokogiri (1.13.10-x86_64-linux)
139
+ nokogiri (1.14.4-x86_64-linux)
131
140
  racc (~> 1.4)
132
- parallel (1.22.1)
133
- 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)
134
148
  ast (~> 2.4.1)
135
149
  public_suffix (5.0.1)
136
150
  racc (1.6.2)
137
- rack (2.2.4)
138
- rack-test (2.0.2)
151
+ rack (2.2.7)
152
+ rack-test (2.1.0)
139
153
  rack (>= 1.3)
140
- rails (7.0.4)
141
- actioncable (= 7.0.4)
142
- actionmailbox (= 7.0.4)
143
- actionmailer (= 7.0.4)
144
- actionpack (= 7.0.4)
145
- actiontext (= 7.0.4)
146
- actionview (= 7.0.4)
147
- activejob (= 7.0.4)
148
- activemodel (= 7.0.4)
149
- activerecord (= 7.0.4)
150
- activestorage (= 7.0.4)
151
- 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)
152
166
  bundler (>= 1.15.0)
153
- railties (= 7.0.4)
167
+ railties (= 7.0.4.3)
154
168
  rails-dom-testing (2.0.3)
155
169
  activesupport (>= 4.2.0)
156
170
  nokogiri (>= 1.6)
157
- rails-html-sanitizer (1.4.4)
171
+ rails-html-sanitizer (1.5.0)
158
172
  loofah (~> 2.19, >= 2.19.1)
159
- railties (7.0.4)
160
- actionpack (= 7.0.4)
161
- activesupport (= 7.0.4)
173
+ railties (7.0.4.3)
174
+ actionpack (= 7.0.4.3)
175
+ activesupport (= 7.0.4.3)
162
176
  method_source
163
177
  rake (>= 12.2)
164
178
  thor (~> 1.0)
165
179
  zeitwerk (~> 2.5)
166
180
  rainbow (3.1.1)
167
181
  rake (13.0.6)
168
- regexp_parser (2.6.1)
182
+ regexp_parser (2.8.0)
169
183
  rexml (3.2.5)
170
184
  rspec (3.12.0)
171
185
  rspec-core (~> 3.12.0)
172
186
  rspec-expectations (~> 3.12.0)
173
187
  rspec-mocks (~> 3.12.0)
174
- rspec-core (3.12.0)
188
+ rspec-core (3.12.2)
175
189
  rspec-support (~> 3.12.0)
176
- rspec-expectations (3.12.1)
190
+ rspec-expectations (3.12.3)
177
191
  diff-lcs (>= 1.2.0, < 2.0)
178
192
  rspec-support (~> 3.12.0)
179
- rspec-mocks (3.12.1)
193
+ rspec-mocks (3.12.5)
180
194
  diff-lcs (>= 1.2.0, < 2.0)
181
195
  rspec-support (~> 3.12.0)
182
- rspec-rails (6.0.1)
196
+ rspec-rails (6.0.2)
183
197
  actionpack (>= 6.1)
184
198
  activesupport (>= 6.1)
185
199
  railties (>= 6.1)
186
- rspec-core (~> 3.11)
187
- rspec-expectations (~> 3.11)
188
- rspec-mocks (~> 3.11)
189
- rspec-support (~> 3.11)
200
+ rspec-core (~> 3.12)
201
+ rspec-expectations (~> 3.12)
202
+ rspec-mocks (~> 3.12)
203
+ rspec-support (~> 3.12)
190
204
  rspec-support (3.12.0)
191
- rswag (2.8.0)
192
- rswag-api (= 2.8.0)
193
- rswag-specs (= 2.8.0)
194
- rswag-ui (= 2.8.0)
195
- 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)
196
210
  railties (>= 3.1, < 7.1)
197
- rswag-specs (2.8.0)
211
+ rswag-specs (2.9.0)
198
212
  activesupport (>= 3.1, < 7.1)
199
213
  json-schema (>= 2.2, < 4.0)
200
214
  railties (>= 3.1, < 7.1)
201
215
  rspec-core (>= 2.14)
202
- rswag-ui (2.8.0)
216
+ rswag-ui (2.9.0)
203
217
  actionpack (>= 3.1, < 7.1)
204
218
  railties (>= 3.1, < 7.1)
205
- rubocop (1.40.0)
219
+ rubocop (1.51.0)
206
220
  json (~> 2.3)
207
221
  parallel (~> 1.10)
208
- parser (>= 3.1.2.1)
222
+ parser (>= 3.2.0.0)
209
223
  rainbow (>= 2.2.2, < 4.0)
210
224
  regexp_parser (>= 1.8, < 3.0)
211
225
  rexml (>= 3.2.5, < 4.0)
212
- rubocop-ast (>= 1.23.0, < 2.0)
226
+ rubocop-ast (>= 1.28.0, < 2.0)
213
227
  ruby-progressbar (~> 1.7)
214
- unicode-display_width (>= 1.4.0, < 3.0)
215
- rubocop-ast (1.24.0)
216
- parser (>= 3.1.1.0)
217
- 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)
218
236
  rubocop (>= 1.7.0, < 2.0)
219
237
  rubocop-ast (>= 0.4.0)
220
- rubocop-rails (2.17.4)
238
+ rubocop-rails (2.19.1)
221
239
  activesupport (>= 4.2.0)
222
240
  rack (>= 1.1)
223
241
  rubocop (>= 1.33.0, < 2.0)
224
- rubocop-rspec (2.16.0)
242
+ rubocop-rspec (2.22.0)
225
243
  rubocop (~> 1.33)
226
- rubocop-shopify (2.10.1)
227
- rubocop (~> 1.35)
244
+ rubocop-capybara (~> 2.17)
245
+ rubocop-factory_bot (~> 2.22)
246
+ rubocop-shopify (2.13.0)
247
+ rubocop (~> 1.50)
228
248
  ruby-next-core (0.15.3)
229
- ruby-progressbar (1.11.0)
230
- sqlite3 (1.5.4)
249
+ ruby-progressbar (1.13.0)
250
+ sqlite3 (1.6.2)
231
251
  mini_portile2 (~> 2.8.0)
232
- sqlite3 (1.5.4-x86_64-linux)
233
- switchcop (0.1.2)
234
- rubocop (~> 1.40.0)
235
- rubocop-performance (~> 1.15.1)
236
- rubocop-rails (~> 2.17.3)
237
- rubocop-rspec (~> 2.16.0)
238
- rubocop-shopify (~> 2.10.1)
239
- 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)
240
260
  timeout (0.3.2)
241
- tzinfo (2.0.5)
261
+ tzinfo (2.0.6)
242
262
  concurrent-ruby (~> 1.0)
243
- unicode-display_width (2.3.0)
263
+ unicode-display_width (2.4.2)
244
264
  websocket-driver (0.7.5)
245
265
  websocket-extensions (>= 0.1.0)
246
266
  websocket-extensions (0.1.5)
247
- zeitwerk (2.6.6)
267
+ zeitwerk (2.6.8)
248
268
 
249
269
  PLATFORMS
250
270
  ruby
251
271
  x86_64-linux
252
272
 
253
273
  DEPENDENCIES
274
+ active_model_serializers (~> 0.10.0)
254
275
  ammeter (~> 1.1.5)
255
276
  database_cleaner
277
+ panko_serializer
256
278
  rake (~> 13.0)
257
279
  rest-api-generator!
258
280
  rspec (~> 3.0)
data/README.md CHANGED
@@ -24,13 +24,12 @@ Following [Switch Dreams's](https://www.switchdreams.com.br/]) coding practices,
24
24
  - :memo: [Automated documentation](#specsdocs)
25
25
  - [Resource ordering](#ordering)
26
26
  - [Resource filter](#filtering)
27
+ - [Resource pagination](#pagination)
28
+ - [Resource serialization](#serialization)
27
29
  - [Configurable](#configuration)
28
30
 
29
31
  ## Next Features
30
32
 
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
33
  - Integration with AVO
35
34
  - Select fields
36
35
  - User auth module
@@ -249,7 +248,6 @@ rails g rest_api_generator:spec:rswag Car name:string color:string
249
248
  By default, the plain rspec and rswag specs are going to be generated in the _spec/requests_ and _spec/docs_
250
249
  directories, respectively. You can override using the (config options)[#configuration]]. :
251
250
 
252
-
253
251
  ### Resource Features
254
252
 
255
253
  #### Modular Error Handler
@@ -294,9 +292,74 @@ And It's done, you can filter your index end-point:
294
292
 
295
293
  - `GET /cars?color=blue or GET /cars?color=red&name=Ferrari`
296
294
 
295
+ ### Pagination
296
+
297
+ For pagination, you need to create pagy initialializer file (pagy.rb) in the config directory of your project.
298
+ Follow [pagy's example](https://ddnexus.github.io/pagy/quick-start/) for more information.
299
+
300
+ Next, you should add some lines on top of the previously created pagy file:
301
+
302
+ ```ruby
303
+ # config/initializers/pagy.rb
304
+ require "pagy"
305
+ require "pagy/extras/headers"
306
+ ```
307
+
308
+ At last, change the pagination variable on RestApiGenerator initializer to true;
309
+
310
+ ```rb
311
+ # config/initializers/rest_api_generator.rb
312
+ config.pagination = true # default: false
313
+ ```
314
+
315
+ Note, if the parent controller is changed, it is necessary to include Pagy::Backend in the new parent.
316
+
317
+ ```rb
318
+ # new_parent_controller.rb
319
+ class NewParentController < ActionController::Base
320
+ include Pagy::Backend
321
+ end
322
+ ```
323
+
324
+ ### Serialization
325
+
326
+ If you are working with [ams](https://github.com/rails-api/active_model_serializers), the serializer will work without
327
+ any extra configuration.
328
+ But if you need to customize you can override the serializer method in the controller:
329
+
330
+ ```ruby
331
+ # Example with panko serializer: https://github.com/panko-serializer/panko_serializer
332
+ class CarsController < RestApiGenerator::ResourceController
333
+
334
+ # serializer used in show, create, update.
335
+ def serializer(resource)
336
+ Panko::CarSerializer.new.serialize_to_json(resource)
337
+ end
338
+
339
+ # serializer used in index.
340
+ def index_serializer(resources)
341
+ Panko::ArraySerializer.new(resources, each_serializer: Panko::CarSerializer).to_json
342
+ end
343
+ end
344
+ ```
345
+
346
+ ```ruby
347
+ # Example with ams
348
+ class CarsController < RestApiGenerator::ResourceController
349
+ def serializer(resource)
350
+ ActiveModelSerializers::SerializableResource.new(resource, each_serializer: Ams::CarSerializer).to_json
351
+ end
352
+ end
353
+ ```
354
+
355
+ The gem is tested with [panko serializer](https://github.com/panko-serializer/panko_serializer)
356
+ and [ams](https://github.com/rails-api/active_model_serializers). But should works with any serializer, feel free to add
357
+ tests for your favorite serializer.
358
+
297
359
  ## Configuration
298
360
 
299
- You can override this gem configuration using the initializer or any other method from [anyway_config](https://github.com/palkan/anyway_config):
361
+ You can override this gem configuration using the initializer or any other method
362
+ from [anyway_config](https://github.com/palkan/anyway_config):
300
363
 
301
364
  ```rb
302
365
  # config/initializers/rest_api_generator.rb
@@ -305,6 +368,7 @@ RestApiGenerator.configure do |config|
305
368
  config.test_path = "custom_test_dir/requests" # default: spec/requests
306
369
  config.docs_path = "custom_docs_dir/rswag" # default: spec/docs
307
370
  config.parent_class = "ApplicationController" # default: RestApiGenerator::ResourceController
371
+ config.pagination = true # default: false
308
372
  end
309
373
  ```
310
374
 
@@ -4,5 +4,6 @@ require "action_controller"
4
4
 
5
5
  module RestApiGenerator
6
6
  class ApplicationController < ActionController::Base
7
+ include Pagy::Backend
7
8
  end
8
9
  end
@@ -3,6 +3,7 @@
3
3
  module RestApiGenerator
4
4
  class ChildResourceController < RestApiGenerator.configuration.parent_controller.constantize
5
5
  include Orderable
6
+ include Serializable
6
7
 
7
8
  before_action :set_parent_resource
8
9
  before_action :set_resource, only: [:show, :update, :destroy]
@@ -11,21 +12,25 @@ module RestApiGenerator
11
12
  @resources = resources
12
13
  @resources = @resources.filter_resource(params_for_filter) if resource_class.include?(Filterable)
13
14
  @resources = @resources.order(ordering_params(params[:sort])) if params[:sort]
14
- render json: @resources, status: :ok
15
+ if pagination
16
+ @pagy, @resources = pagy(@resources)
17
+ pagy_headers_merge(@pagy)
18
+ end
19
+ render json: index_serializer(@resources), status: :ok
15
20
  end
16
21
 
17
22
  def show
18
- render json: @resource, status: :ok
23
+ render json: serializer(@resource), status: :ok
19
24
  end
20
25
 
21
26
  def create
22
27
  @resource = resources.create!(resource_created_params)
23
- render json: @resource, status: :created
28
+ render json: serializer(@resource), status: :created
24
29
  end
25
30
 
26
31
  def update
27
32
  @resource.update!(resource_updated_params)
28
- render json: @resource, status: :ok
33
+ render json: serializer(@resource), status: :ok
29
34
  end
30
35
 
31
36
  def destroy
@@ -93,5 +98,9 @@ module RestApiGenerator
93
98
  def record_id
94
99
  params.permit(:id)[:id]
95
100
  end
101
+
102
+ def pagination
103
+ RestApiGenerator.configuration.pagination
104
+ end
96
105
  end
97
106
  end
@@ -3,6 +3,7 @@
3
3
  module RestApiGenerator
4
4
  class ResourceController < RestApiGenerator.configuration.parent_controller.constantize
5
5
  include Orderable
6
+ include Serializable
6
7
 
7
8
  before_action :set_resource, only: [:show, :update, :destroy]
8
9
 
@@ -10,21 +11,25 @@ module RestApiGenerator
10
11
  @resources = resource_class.all
11
12
  @resources = @resources.filter_resource(params_for_filter) if resource_class.include?(Filterable)
12
13
  @resources = @resources.order(ordering_params(params[:sort])) if params[:sort]
13
- render json: @resources, status: :ok
14
+ if pagination
15
+ @pagy, @resources = pagy(@resources)
16
+ pagy_headers_merge(@pagy)
17
+ end
18
+ render json: index_serializer(@resources), status: :ok
14
19
  end
15
20
 
16
21
  def show
17
- render json: @resource, status: :ok
22
+ render json: serializer(@resource), status: :ok
18
23
  end
19
24
 
20
25
  def create
21
26
  @resource = resource_class.create!(resource_created_params)
22
- render json: @resource, status: :created
27
+ render json: serializer(@resource), status: :created
23
28
  end
24
29
 
25
30
  def update
26
31
  @resource.update!(resource_updated_params)
27
- render json: @resource, status: :ok
32
+ render json: serializer(@resource), status: :ok
28
33
  end
29
34
 
30
35
  def destroy
@@ -69,5 +74,9 @@ module RestApiGenerator
69
74
  def record_id
70
75
  params.permit(:id)[:id]
71
76
  end
77
+
78
+ def pagination
79
+ RestApiGenerator.configuration.pagination
80
+ end
72
81
  end
73
82
  end
@@ -10,6 +10,7 @@ module RestApiGenerator
10
10
 
11
11
  attr_config test_path: "spec/requests",
12
12
  docs_path: "spec/docs",
13
- parent_controller: "RestApiGenerator::ApplicationController"
13
+ parent_controller: "RestApiGenerator::ApplicationController",
14
+ pagination: false
14
15
  end
15
16
  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.6"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -8,6 +8,7 @@ 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"
11
12
 
12
13
  module RestApiGenerator
13
14
  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",
@@ -38,6 +39,7 @@ Gem::Specification.new do |spec|
38
39
  spec.metadata["rubygems_mfa_required"] = "true"
39
40
 
40
41
  spec.add_dependency "anyway_config", ">= 2.0.0"
42
+ spec.add_dependency "pagy"
41
43
  spec.add_dependency "rails", ">= 5.0"
42
44
 
43
45
  spec.add_development_dependency "ammeter", "~> 1.1.5"
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.6
4
+ version: 0.2.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-11 00:00:00.000000000 Z
11
+ date: 2023-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: anyway_config
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 2.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: pagy
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rails
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -147,6 +161,7 @@ files:
147
161
  - lib/rest_api_generator/helpers/render.rb
148
162
  - lib/rest_api_generator/orderable.rb
149
163
  - lib/rest_api_generator/rails.rb
164
+ - lib/rest_api_generator/serializable.rb
150
165
  - lib/rest_api_generator/version.rb
151
166
  - rest-api-generator.gemspec
152
167
  - sig/rest_api_generator.rbs
@@ -158,6 +173,7 @@ metadata:
158
173
  homepage_uri: https://github.com/SwitchDreams/rest-api-generator
159
174
  source_code_uri: https://github.com/SwitchDreams/rest-api-generator
160
175
  changelog_uri: https://github.com/SwitchDreams/rest-api-generator
176
+ github_repo: https://github.com/SwitchDreams/rest-api-generator
161
177
  rubygems_mfa_required: 'true'
162
178
  post_install_message:
163
179
  rdoc_options: []