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 via model :name in the controller. The later overwrites the param.
The following helpers are available:
model The name of the model as symbol
collection The collection on the store repo
collection_name The name of the collection
resource 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 example todo: { 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 the collection. The resource_params have already been applied to the instance
update A buffered version of the model. You can update it via resource.update(resource_params)
show A resolved version of the model (using sync)
delete A resolved version of the model (using sync)
index No resource will be setup
Volt::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 via model :name in the controller or pass in the model via the params. You can do so in your routes file:
Volt::SimpleJsonController implements the index, show, create, update and destroy 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 from Volt::RestfullBaseController. So you have the same helpers available.
What's missing
Authentication
Pagination: My idea here was to implement the pagination in the Volt::RestfullBaseController. per_page (and a max_per_page) could either be set via the params or directly on the controller.
Make the 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.
Error handling. Especially for validations and permissions
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.