Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto API generation #324

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

jfahrer
Copy link
Contributor

@jfahrer jfahrer commented Oct 25, 2015

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 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:

rest '/api/issues', component: 'api', controller: 'api', model: 'issue'
rest '/api/todos', component: 'api', controller: 'api', model: 'todo'
get '/api/cats', component: 'api', controller: 'api', action: index, model: 'cats'

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
  • Generators for the new controller types
  • Documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant