Class: Yodatra::ModelsController
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Yodatra::ModelsController
- Defined in:
- lib/yodatra/models_controller.rb
Overview
This is a generic model controller that expose a REST API for your models. The responses are encoded in JSON.
Simply create your controller that inherits from this class, keeping the naming convention.
For example, given a User model, creating a class UsersController < Yodatra::ModelsController, it will expose these routes: GET /users
=> retrieves all users <i>(attributes exposed are limited by the <b>read_scope</b> method defined in the <b>UsersController</b>)</i>
GET /users/:id
=> retrieves a user <i>(attributes exposed are limited by the <b>read_scope</b> method defined in the <b>UsersController</b>)</i>
POST /users
=> creates a user <i>(attributes assignable are limited by the <b>user_params</b> method defined in the <b>UsersController</b>)</i>
PUT /users/:id
=> updates a user <i>(attributes assignable are limited by the <b>user_params</b> method defined in the <b>UsersController</b>)</i>
DELETE /users/:id
=> deletes a user
If your model is referenced by another model, nested routes are also created for you. And you don’t need to worry about the references/joins, they are done automaticly! For example, imagine a Team model that has many Users, the following routes will be exposed: GET /team/:team_id/users, GET /team/:team_id/users/:id, POST /team/:team_id/users, PUT /team/:team_id/users/:id and DESTROY /team/:team_id/users/:id
Constant Summary collapse
- ONE_ROUTE =
Generic route to target ONE resource
%r{\A/([\w]+?)/([0-9]+)(?:/([\w]+?)/([0-9]+)){0,1}\Z}- ALL_ROUTE =
Generic route to target ALL resources
%r{\A/([\w]+?)(?:/([0-9]+)/([\w]+?)){0,1}\Z}- READ_ALL =
:read_all- READ_ONE =
:read- CREATE_ONE =
:create- UPDATE_ONE =
:update- DELETE_ONE =
:delete
Class Method Summary collapse
-
.disable(*opts) ⇒ Object
This helper gives the ability to disable default root by specifying a list of routes to disable.
- .model_name ⇒ Object
Instance Method Summary collapse
-
#retrieve_resources(disables) ⇒ Object
Defines a nested route or not and retrieves the correct resource (or resources).
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object (private)
create/update scope defaults to all data given in the POST/PUT
174 175 176 177 178 |
# File 'lib/yodatra/models_controller.rb', line 174 def method_missing(name, *args) if name.to_s == "#{model_name.underscore}_params" return params.reject{|k,v| %w(splat captures id updated_at created_at).include? k} end end |
Class Method Details
.disable(*opts) ⇒ Object
This helper gives the ability to disable default root by specifying a list of routes to disable.
124 125 126 127 128 129 130 |
# File 'lib/yodatra/models_controller.rb', line 124 def disable(*opts) opts.each do |key| method = "#{key}_disabled?".to_sym undef_method method if method_defined? method define_method method, Proc.new {|| true} end end |
.model_name ⇒ Object
117 118 119 |
# File 'lib/yodatra/models_controller.rb', line 117 def model_name self.name.split('::').last.gsub(/sController/, '') end |
Instance Method Details
#retrieve_resources(disables) ⇒ Object
Defines a nested route or not and retrieves the correct resource (or resources)
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/yodatra/models_controller.rb', line 97 def retrieve_resources(disables) pass unless involved? no_route if disabled? disables model = model_name.constantize nested = nested_resources if nested? if model.nil? || nested.nil? && nested? raise ActiveRecord::RecordNotFound else model = nested if nested? one_id = nested? ? params[:captures].fourth : params[:captures].second if params[:captures].length == 4 model = model.find one_id unless one_id.nil? yield(model) end rescue ActiveRecord::RecordNotFound record_not_found end |