app/controllers/apicasso/crud_controller.rb in apicasso-0.6.8 vs app/controllers/apicasso/crud_controller.rb in apicasso-0.7.0

- old
+ new

@@ -1,17 +1,32 @@ # frozen_string_literal: true module Apicasso # Controller to consume read-only data to be used on client's frontend class CrudController < Apicasso::ApplicationController - before_action :set_root_resource - before_action :set_object, except: %i[index create schema] + before_action :set_root_resource, except: %i[ql batch_create batch_update] + before_action :set_object, except: %i[index create schema ql batch_create batch_update] before_action :set_nested_resource, only: %i[nested_index] before_action :set_records, only: %i[index nested_index] include SqlSecurity include CrudUtils include Orderable + + # POST /:resource + # Common behavior for an create API endpoint + 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 + # 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"`, @@ -27,10 +42,13 @@ # relation/methods including on response def show render json: show_json end + # GET /:resource/1/:nested_resource + alias nested_index index + # PATCH/PUT /:resource/1 # Common behavior for an update API endpoint def update authorize_for(action: :update, resource: resource.name.underscore.to_sym, @@ -53,26 +71,10 @@ else render json: @object.errors, status: :unprocessable_entity end end - # GET /:resource/1/:nested_resource - alias nested_index index - - # POST /:resource - 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 - # 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. def schema @@ -131,11 +133,11 @@ def index_json if params[:group].present? @records.group(params[:group][:by].split(',')) .send(:calculate, params[:group][:calculate], - params[:group][:field]) - else + params[:group][:fields]) + else collection_response end end # The response for show action, which can be a fieldset