introspective_grape 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/lib/introspective_grape/api.rb +15 -14
- data/lib/introspective_grape/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f52e3f6200f277c190a76450e09f65c582a9e56d
|
4
|
+
data.tar.gz: f2779c6afdfa697bc4d051d3ab461d9afcb91e6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc38af247ee9f040212122583701537567fc3a6551339c35034f88a0edef5acea6ed79b020fb7d5f3d16dddcc299bd12b882870d1a90826220bf00b6d8bfbb51
|
7
|
+
data.tar.gz: 08b47cd609c1501a9eeeb4293d5dc3dee9a97028c814aef22a9930f543ba529439358258f2b75a0f7af0d00597d5b73fb6d58ed57738683b6f7c397f384296e9
|
data/CHANGELOG.md
CHANGED
@@ -204,7 +204,7 @@ module IntrospectiveGrape
|
|
204
204
|
detail klass.create_documentation || "creates a new #{name} record"
|
205
205
|
end
|
206
206
|
dsl.params do
|
207
|
-
klass.generate_params(self,
|
207
|
+
klass.generate_params(self, :create, model, api_params, true)
|
208
208
|
end
|
209
209
|
dsl.post do
|
210
210
|
if @model
|
@@ -225,7 +225,7 @@ module IntrospectiveGrape
|
|
225
225
|
detail klass.update_documentation || "updates the details of a #{name}"
|
226
226
|
end
|
227
227
|
dsl.params do
|
228
|
-
klass.generate_params(self,
|
228
|
+
klass.generate_params(self, :update, model, api_params, true)
|
229
229
|
end
|
230
230
|
dsl.put ":#{routes.last.swaggerKey}" do
|
231
231
|
authorize @model, :update?
|
@@ -316,35 +316,36 @@ module IntrospectiveGrape
|
|
316
316
|
|
317
317
|
|
318
318
|
|
319
|
-
def generate_params(dsl,
|
319
|
+
def generate_params(dsl, action, model, fields, is_root_endpoint=false)
|
320
320
|
# We'll be doing a recursive walk (to handle nested attributes) down the
|
321
321
|
# whitelisted params, generating the Grape param definitions by introspecting
|
322
322
|
# on the model and its associations.
|
323
323
|
raise "Invalid action: #{action}" unless [:update, :create].include?(action)
|
324
324
|
# dsl : The Grape::Validations::ParamsScope object
|
325
|
-
# klass : A reference back to the original descendant of IntrospectiveGrape::API.
|
326
|
-
# You have to pass this around to remember who you were before the DSL
|
327
|
-
# scope hijacked your identity.
|
328
325
|
# action: create or update
|
329
326
|
# model : The ActiveRecord model class
|
330
327
|
# fields: The whitelisted data structure for Rails' strong params, from which we
|
331
328
|
# infer Grape's parameters
|
329
|
+
|
330
|
+
# skip the ID param at the root level endpoint, so we don't duplicate the URL parameter (api/v#/model/modelId)
|
331
|
+
fields -= [:id] if is_root_endpoint
|
332
332
|
|
333
|
-
|
333
|
+
fields.each do |field|
|
334
334
|
if field.kind_of?(Hash)
|
335
|
-
generate_nested_params(dsl,
|
336
|
-
elsif (action==:create &&
|
335
|
+
generate_nested_params(dsl,action,model,field)
|
336
|
+
elsif (action==:create && param_required?(model,field) )
|
337
337
|
# All params are optional on an update, only require them during creation.
|
338
338
|
# Updating a record with new child models will have to rely on ActiveRecord
|
339
339
|
# validations:
|
340
|
-
dsl.requires field, type:
|
340
|
+
dsl.requires field, type: param_type(model,field)
|
341
341
|
else
|
342
|
-
dsl.optional field, type:
|
342
|
+
dsl.optional field, type: param_type(model,field)
|
343
343
|
end
|
344
344
|
end
|
345
345
|
end
|
346
346
|
|
347
|
-
def generate_nested_params(dsl,
|
347
|
+
def generate_nested_params(dsl,action,model,fields)
|
348
|
+
klass = self
|
348
349
|
fields.each do |r,v|
|
349
350
|
# Look at model.reflections to find the association's class name:
|
350
351
|
reflection = r.to_s.sub(/_attributes$/,'') # the reflection name
|
@@ -360,7 +361,7 @@ module IntrospectiveGrape
|
|
360
361
|
# In case you need a refresher on how these work:
|
361
362
|
# http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
|
362
363
|
dsl.optional r, type: Array do |dl|
|
363
|
-
klass.generate_params(dl,
|
364
|
+
klass.generate_params(dl,action,relation,v)
|
364
365
|
klass.add_destroy_param(dl,model,reflection) unless action==:create
|
365
366
|
end
|
366
367
|
else
|
@@ -369,7 +370,7 @@ module IntrospectiveGrape
|
|
369
370
|
# ThroughReflection, HasOneReflection,
|
370
371
|
# HasAndBelongsToManyReflection, BelongsToReflection
|
371
372
|
dsl.optional r, type: Hash do |dl|
|
372
|
-
klass.generate_params(dl,
|
373
|
+
klass.generate_params(dl,action,relation,v)
|
373
374
|
klass.add_destroy_param(dl,model,reflection) unless action==:create
|
374
375
|
end
|
375
376
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: introspective_grape
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Buermann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|