Sha256: 7163015fa906ed55de0a7318adda77b525c7bf1de90a3d83e57544654cd2aaa3

Contents?: true

Size: 842 Bytes

Versions: 15

Compression:

Stored size: 842 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?
    :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

15 entries across 15 versions & 1 rubygems

Version Path
potassium-6.0.0 lib/potassium/assets/api/responder.rb
potassium-5.2.3 lib/potassium/assets/api/responder.rb
potassium-5.2.2 lib/potassium/assets/api/responder.rb
potassium-5.2.1 lib/potassium/assets/api/responder.rb
potassium-5.2.0 lib/potassium/assets/api/responder.rb
potassium-5.1.4 lib/potassium/assets/api/responder.rb
potassium-5.1.3 lib/potassium/assets/api/responder.rb
potassium-5.1.2 lib/potassium/assets/api/responder.rb
potassium-5.1.1 lib/potassium/assets/api/responder.rb
potassium-3.0.0 lib/potassium/assets/api/responder.rb
potassium-2.3.0 lib/potassium/assets/api/responder.rb
potassium-2.2.0 lib/potassium/assets/api/responder.rb
potassium-2.1.0 lib/potassium/assets/api/responder.rb
potassium-2.0.0 lib/potassium/assets/api/responder.rb
potassium-1.3.5 lib/potassium/templates/application/assets/api/responder.rb