Sha256: 3015b01403637311aa6dd84c2a618ec6cb36dbf96529b0efcb756fffd614f466

Contents?: true

Size: 864 Bytes

Versions: 6

Compression:

Stored size: 864 Bytes

Contents

class ApiResponder < ActionController::Responder
  def respond
    return display_errors if has_errors?
    return head :no_content if delete?

    display resource, :status_code => status_code
  end

  private

  def display(resource, given_options = {})
    controller.render options.merge(given_options).merge({
      :json => serializer.as_json
    })
  end

  def serializer
    serializer_class = ActiveModel::Serializer.serializer_for(resource)
    if serializer_class.present?
      serializer_class.new(resource, options)
    else
      resource
    end
  end

  def status_code
    return :created if post?
    return :ok
  end

  def display_errors
    controller.render({
      :status => :unprocessable_entity,
      :json => { msg: "invalid_attributes", errors: format_errors }
    })
  end

  def format_errors
    resource.errors.as_json
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
potassium-1.3.4 lib/potassium/templates/application/assets/api/responder.rb
potassium-1.3.3 lib/potassium/templates/application/assets/api/responder.rb
potassium-1.3.2 lib/potassium/templates/application/assets/api/responder.rb
potassium-1.3.0 lib/potassium/templates/application/assets/api/responder.rb
potassium-1.2.4 lib/potassium/templates/application/assets/api/responder.rb
potassium-1.2.3 lib/potassium/templates/application/assets/api/responder.rb