module Granola::Rails

Helpers to integrate Granola into Rails' rendering flow.

Example:

class API::UsersController < ApplicationController
  def show
    user = User.find(params[:id])
    render json: user, with: API::UserSerializer
  end
end

Constants

VERSION

Public Class Methods

install_renderer(format) click to toggle source

Internal: Let ActionController know about any renderers you add to Granola by defining an ActionController::Renderer based on Granola's.

format - A Symbol with a rendering format (e.g. `:json`).

Returns nothing.

# File lib/granola/rails.rb, line 27
def self.install_renderer(format)
  ActionController::Renderers.remove(format)
  ActionController::Renderers.add(format) do |object, options|
    granola(object, **options.merge(as: format))
  end
end

Public Instance Methods

granola(object, **options) click to toggle source

Public: Low-level API to respond with a serialized object. You should prefer using ActionController's `render` with the serialization format (e.g. `render json: object, with: SomeSerializer`).

object - An Object to be passed to a Granola::Serializer. **options - Keywords to configure this response. See

`Granola::Rack#granola` for a full list.

Returns nothing.

Calls superclass method
# File lib/granola/rails.rb, line 45
def granola(object, **options)
  options = { status: self.status, env: request.env }.update(options)
  status, headers, body = super(object, **options)

  response.status = status
  response.headers.update(headers)
  self.response_body = body
end