Class: Apicasso::CrudController
- Inherits:
-
ApplicationController
- Object
- ActionController::API
- ApplicationController
- Apicasso::CrudController
- Includes:
- Orderable
- Defined in:
- app/controllers/apicasso/crud_controller.rb
Overview
Controller to consume read-only data to be used on client’s frontend
Constant Summary
Constants included from Orderable
Instance Method Summary collapse
-
#create ⇒ Object
POST /:resource.
-
#destroy ⇒ Object
DELETE /:resource/1 Common behavior for an destroy API endpoint.
-
#index ⇒ Object
(also: #nested_index)
GET /:resource Returns a paginated, ordered and filtered query based response.
-
#schema ⇒ Object
OPTIONS /:resource OPTIONS /:resource/1/:nested_resource Will return a JSON with the schema of the current resource, using attribute names as keys and attirbute types as values.
-
#show ⇒ Object
GET /:resource/1 Common behavior for showing a record, with an addition of relation/methods including on response.
-
#update ⇒ Object
PATCH/PUT /:resource/1 Common behavior for an update API endpoint.
Methods included from Orderable
Methods inherited from ApplicationController
Instance Method Details
#create ⇒ Object
POST /:resource
62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/controllers/apicasso/crud_controller.rb', line 62 def create @object = resource.new(object_params) (action: :create, resource: resource.name.underscore.to_sym, object: @object) if @object.save render json: @object.to_json, status: :created else render json: @object.errors, status: :unprocessable_entity end end |
#destroy ⇒ Object
DELETE /:resource/1 Common behavior for an destroy API endpoint
47 48 49 50 51 52 53 54 55 56 |
# File 'app/controllers/apicasso/crud_controller.rb', line 47 def destroy (action: :destroy, resource: resource.name.underscore.to_sym, object: @object) if @object.destroy head :no_content, status: :ok else render json: @object.errors, status: :unprocessable_entity end end |
#index ⇒ Object Also known as: nested_index
GET /:resource Returns a paginated, ordered and filtered query based response. Consider this To get all ‘Channel` sorted by ascending `name` , filtered by the ones that have a `domain` that matches exactly `“domain.com”`, paginating records 42 per page and retrieving the page 42. Example:
GET /sites?sort=+name,-updated_at&q[domain_eq]=domain.com&page=42&per_page=42
19 20 21 22 |
# File 'app/controllers/apicasso/crud_controller.rb', line 19 def index set_access_control_headers render json: index_json end |
#schema ⇒ Object
OPTIONS /:resource OPTIONS /:resource/1/:nested_resource Will return a JSON with the schema of the current resource, using attribute names as keys and attirbute types as values.
78 79 80 |
# File 'app/controllers/apicasso/crud_controller.rb', line 78 def schema render json: resource_schema.to_json unless preflight? end |
#show ⇒ Object
GET /:resource/1 Common behavior for showing a record, with an addition of relation/methods including on response
27 28 29 30 |
# File 'app/controllers/apicasso/crud_controller.rb', line 27 def show set_access_control_headers render json: show_json end |
#update ⇒ Object
PATCH/PUT /:resource/1 Common behavior for an update API endpoint
34 35 36 37 38 39 40 41 42 43 |
# File 'app/controllers/apicasso/crud_controller.rb', line 34 def update (action: :update, resource: resource.name.underscore.to_sym, object: @object) if @object.update(object_params) render json: @object.to_json else render json: @object.errors, status: :unprocessable_entity end end |