Class: Apicasso::CrudController

Inherits:
ApplicationController show all
Includes:
CrudUtils, Orderable, SqlSecurity
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

Constants included from SqlSecurity

SqlSecurity::DESCENDANTS_UNDERSCORED, SqlSecurity::GROUP_CALCULATE

Instance Method Summary collapse

Methods included from Orderable

#ordering_params

Methods included from CrudUtils

#action_to_cancancan, #active_storage_param, #associations_array, #common_relation_param, #has_many_params, #has_one_params, #include_options, #next_link_for, #object_params, #page_link, #pagination_metadata_for, #parsed_associations, #parsed_include, #parsed_methods, #parsed_query, #parsed_select, #previous_link_for, #relation_param, #resource, #resource_params, #resource_schema

Methods included from SqlSecurity

#sql_injection

Methods inherited from ApplicationController

#current_ability

Instance Method Details

#createObject

POST /:resource Common behavior for an create API endpoint



16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/apicasso/crud_controller.rb', line 16

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



65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/apicasso/crud_controller.rb', line 65

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


36
37
38
# File 'app/controllers/apicasso/crud_controller.rb', line 36

def index
  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.



80
81
82
# File 'app/controllers/apicasso/crud_controller.rb', line 80

def schema
  render json: resource_schema.to_json
end

#showObject

GET /:resource/1 Common behavior for showing a record, with an addition of relation/methods including on response



43
44
45
# File 'app/controllers/apicasso/crud_controller.rb', line 43

def show
  render json: show_json
end

#updateObject

PATCH/PUT /:resource/1 Common behavior for an update API endpoint



52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/apicasso/crud_controller.rb', line 52

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