Sha256: 1977ed356e8197e07731f8a716400de6c00df19dce50b5b09d3b8a5699521bf1

Contents?: true

Size: 937 Bytes

Versions: 4

Compression:

Stored size: 937 Bytes

Contents

module Api
  class BaseApiController < ApplicationController
    # Disable CSRF protection for API calls
    protect_from_forgery with: :null_session

    # Disable cookie usage
    before_action :destroy_session

    # Handle objects that aren't found
    rescue_from ActiveRecord::RecordNotFound, with: :not_found

    def respond_with_errors(object)
      serialized_errors = object.errors.messages.map do |field, errors|
        errors.map do |error_message|
          {
            status: 422,
            source: { pointer: "/data/attributes/#{field}" },
            detail: error_message,
          }
        end
      end.flatten

      render json: { errors: serialized_errors },
             status: :unprocessable_entity
    end

    private

    def destroy_session
      request.session_options[:skip] = true
    end

    def not_found
      render json: { errors: 'Not found' },
             status: 404
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
voyage-1.44.0.14 lib/voyage/templates/api_base_controller.rb
voyage-1.44.0.13 lib/voyage/templates/api_base_controller.rb
voyage-1.44.0.11 lib/voyage/templates/api_base_controller.rb
voyage-1.44.0.12 lib/voyage/templates/api_base_controller.rb