Extracting response helpers
First, let’s move the render functions in Goldcrest.View to a helper module, which can also be imported into the Goldcrest.Controller.__using__/1 macro, allowing the controller to use functions such as redirect and render:
defmodule Goldcrest.ResponseHelpers do
@moduledoc false
import Plug.Conn
require EEx
def render(conn, :json, data) when is_map(data) do
conn
|> content_type("application/json")
|> send_resp(200, Jason.encode!(data))
end
def render(conn, :json, data) when is_binary(data) do
data = ensure_json!(data)
conn
|> content_type("application/json")
|> send_resp(conn.status || 200, data)
end
def render(conn, :html, data) when is_binary(data) do
...