Class: Apicasso::CrudController

Inherits:
ApplicationController show all
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

Orderable::SORT_ORDER

Instance Method Summary collapse

Methods included from Orderable

#ordering_params

Methods inherited from ApplicationController

#current_ability

Instance Method Details

#createObject

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)
  authorize_for(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

#destroyObject

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
  authorize_for(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

#indexObject 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

#schemaObject

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

#showObject

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

#updateObject

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
  authorize_for(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