module Shamu::JsonApi::Rails::Responder

Support JSON API responses with the standard rails `#respond_with` method.

Public Instance Methods

to_json() click to toggle source

Render the response as JSON @return [String]

# File lib/shamu/json_api/rails/responder.rb, line 10
def to_json
  if has_errors?
    display_errors
  elsif get?
    display resource
  elsif put? || patch?
    display resource, :location => api_location
  elsif post?
    display resource, :status => :created, :location => api_location
  else
    head :no_content
  end
end
Also aliased as: to_json_api
to_json_api()
Alias for: to_json

Protected Instance Methods

display( resource, given_options = {} ) click to toggle source

@visibility private

Calls superclass method
# File lib/shamu/json_api/rails/responder.rb, line 28
def display( resource, given_options = {} )
  given_options.merge!( options )

  json =
    if resource.is_a?( Enumerable )
      controller.json_collection resource, **given_options
    else
      controller.json_resource resource, **given_options
    end

  super json.to_json, given_options
end
display_errors() click to toggle source

@visibility private

# File lib/shamu/json_api/rails/responder.rb, line 42
def display_errors
  controller.render format => controller.json_validation_errors( resource_errors ).to_json,
                    :status => :unprocessable_entity
end

Private Instance Methods

validation_resource?( resource ) click to toggle source
# File lib/shamu/json_api/rails/responder.rb, line 49
def validation_resource?( resource )
  resource.respond_to?( :valid? ) && resource.respond_to?( :errors )
end