Sha256: 96df57195c49dc33c669d560187e1e1c1ce1baa08a3bda2d3781b84628d9a783

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

module Faceted

  module Controller

    # For rendering a response with a single object, e.g.
    # render_response(@addresses)
    def render_response(obj)
      render :json => {
        success:  obj.success,
        response: obj.to_hash,
        errors:   obj.errors
      }.to_json
    end

    # For rendering a response with a multiple objects, e.g.
    # render_response_with_collection(:addresses, @addresses)
    def render_response_with_collection(key, array)
      render :json => {
        success: true,
        response: {"#{key}".to_sym => array},
        errors:   nil
      }.to_json
    end

    # In your base API controller:
    # rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
    def render_400(exception)
      render :json => {
        success: false,
        response: nil,
        errors: "Record not found: #{exception.message}"
      }, :status => 404
    end

    # In your base API controller:
    # rescue_from Exception, :with => :render_500
    def render_500(exception)
      Rails.logger.info("!!! #{self.class.name} exception caught: #{exception} #{exception.backtrace.join("\n")}")
      render :json => {
        success: false,
        response: nil,
        errors: "#{exception.message}"
      }, :status => 500
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
faceted-0.6.4 lib/faceted/controller.rb
faceted-0.6.3 lib/faceted/controller.rb
faceted-0.6.2 lib/faceted/controller.rb
faceted-0.6.1 lib/faceted/controller.rb
faceted-0.6.0 lib/faceted/controller.rb