Sha256: b899ec8b8bc46c6c68879b55e686f8f76c5af30dfaf0bada4cf91d7a89e5ec57

Contents?: true

Size: 1.28 KB

Versions: 16

Compression:

Stored size: 1.28 KB

Contents

module Faceted

  module Controller

    # For rendering a response with a single object, e.g.
    # render_response(@address)
    def render_response(obj, code=nil)
      render :json => {
        success:  obj.success,
        response: obj.to_hash,
        errors:   obj.errors
      }, :status => code || obj.success ? 200 : 400
    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
      }
    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: "#{exception.message}"
      }, :status => 400
    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

16 entries across 16 versions & 1 rubygems

Version Path
faceted-1.6.2 lib/faceted/controller.rb
faceted-1.6.1 lib/faceted/controller.rb
faceted-1.6.0 lib/faceted/controller.rb
faceted-1.5.1 lib/faceted/controller.rb
faceted-1.5.0 lib/faceted/controller.rb
faceted-1.4.0 lib/faceted/controller.rb
faceted-1.3.3 lib/faceted/controller.rb
faceted-1.3.2 lib/faceted/controller.rb
faceted-1.3.1 lib/faceted/controller.rb
faceted-1.3.0 lib/faceted/controller.rb
faceted-1.2.3 lib/faceted/controller.rb
faceted-1.2.2 lib/faceted/controller.rb
faceted-1.2.1 lib/faceted/controller.rb
faceted-1.2.0 lib/faceted/controller.rb
faceted-1.1.1 lib/faceted/controller.rb
faceted-1.1.0 lib/faceted/controller.rb