Module: Faceted::Controller

Defined in:
lib/faceted/controller.rb

Instance Method Summary collapse

Instance Method Details

#render_400(exception) ⇒ Object

In your base API controller: rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found



27
28
29
30
31
32
33
# File 'lib/faceted/controller.rb', line 27

def render_400(exception)
  render :json => {
    success: false,
    response: nil,
    errors: "#{exception.message}"
  }, :status => 400
end

#render_500(exception) ⇒ Object

In your base API controller: rescue_from Exception, :with => :render_500



37
38
39
40
41
42
43
44
# File 'lib/faceted/controller.rb', line 37

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

#render_response(obj, code = nil) ⇒ Object

For rendering a response with a single object, e.g. render_response(@address)



7
8
9
10
11
12
13
# File 'lib/faceted/controller.rb', line 7

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

#render_response_with_collection(key, array) ⇒ Object

For rendering a response with a multiple objects, e.g. render_response_with_collection(:addresses, @addresses)



17
18
19
20
21
22
23
# File 'lib/faceted/controller.rb', line 17

def render_response_with_collection(key, array)
  render :json => {
    success: true,
    response: {"#{key}".to_sym => array},
    errors: nil
  }
end