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 :namein the controller. The later overwrites the param.The following helpers are available:
modelThe name of the model as symbolcollectionThe collection on the store repocollection_nameThe name of the collectionresourceThe instance of the model based on the given id.resource_paramsThe 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
resourcebased on the action:createA new instance that can be appended to thecollection. Theresource_paramshave already been applied to the instanceupdateA buffered version of the model. You can update it viaresource.update(resource_params)showA resolved version of the model (usingsync)deleteA resolved version of the model (usingsync)indexNoresourcewill be setupVolt::SimpleJsonController
A server side controller that inherits from
Volt::SimpleJsonControllerautomatically generates a simple JSON API for your models. You can either set the model viamodel :namein the controller or pass in the model via the params. You can do so in your routes file:Volt::SimpleJsonControllerimplements theindex,show,create,updateanddestroyactions 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::SimpleJsonControllerinherits 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::SimpleJsonControllermore generic. So you could set the format via the URL or content type and call the corresponding renderer.Volt::HttpControllerdoes 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.