module JsonapiForRails::Controller::Utils::Render::InstanceMethods

Public Instance Methods

jsonapi_render(object) click to toggle source
# File lib/jsonapi_for_rails/controller/utils/render.rb, line 29
def jsonapi_render object
        # Status code
        @jsonapi_status = 200

        # Generate json
        @jsonapi_json = JSON.generate(object)

        # Render
        render(
                plain:        @jsonapi_json,
                status:       @jsonapi_status
        )

        # Set content type
        @jsonapi_content_type = JSONAPI[:content_type]
        response.headers['Content-Type'] = @jsonapi_content_type
end
jsonapi_render_errors(status, argument) click to toggle source
# File lib/jsonapi_for_rails/controller/utils/render.rb, line 47
def jsonapi_render_errors status, argument
        # Status code
        @jsonapi_status = status

        # Generate json
        if argument.kind_of? Hash
                message = argument
        elsif argument.kind_of? Array
                message = {
                        errors: argument
                }
        else
                message = {
                        errors: [
                                {detail: argument.to_s}
                        ]
                }
        end

        @jsonapi_json = JSON.generate(message)

        # Render
        render(
                plain:        @jsonapi_json,
                status:       @jsonapi_status
        )

        # Set content type
        @jsonapi_content_type = JSONAPI[:content_type]
        response.headers['Content-Type'] = @jsonapi_content_type
end