Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements a basic version of a HttpController for automatic JSON API generation.
What's new
Volt::RestfullBaseController
For server side controllers you can now inherit from
Volt::RestfullBaseController
. You can either pass in the model as a param or set it viamodel :name
in the controller. The later overwrites the param.The following helpers are available:
model
The name of the model as symbolcollection
The collection on the store repocollection_name
The name of the collectionresource
The instance of the model based on the given id.resource_params
The params for the resource. Params are expected to be nested with the model name as key. For exampletodo: { name: 'the_name_for_the_todo' }
The controller also sets up your
resource
based on the action:create
A new instance that can be appended to thecollection
. Theresource_params
have already been applied to the instanceupdate
A buffered version of the model. You can update it viaresource.update(resource_params)
show
A resolved version of the model (usingsync
)delete
A resolved version of the model (usingsync
)index
Noresource
will be setupVolt::SimpleJsonController
A server side controller that inherits from
Volt::SimpleJsonController
automatically generates a simple JSON API for your models. You can either set the model viamodel :name
in the controller or pass in the model via the params. You can do so in your routes file:Volt::SimpleJsonController
implements theindex
,show
,create
,update
anddestroy
actions for you. For updating and creation you need set the Content-Type of the request to 'aplication/json'. The JSON itself has to be prefixed with the model name.For example:
{ "todo": { "name": "the_name" }}
Volt::SimpleJsonController
inherits fromVolt::RestfullBaseController
. So you have the same helpers available.What's missing
Volt::RestfullBaseController
.per_page
(and amax_per_page
) could either be set via the params or directly on the controller.Volt::SimpleJsonController
more generic. So you could set the format via the URL or content type and call the corresponding renderer.Volt::HttpController
does not have access to the routes. This is necessary to return the new location for a created instance. The corresponding code and test are in the code.