-
-
Save paulcsmith/057696e7c1c0accc7483 to your computer and use it in GitHub Desktop.
Rendering JSON with views
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.CommentView do | |
use MyApp.Web, :view | |
@attributes ~W(id name inserted_at) | |
def render("index.json", %{data: comments}) do | |
for comment <- comments, do: render("show.json", %{data: comment}) | |
end | |
def render("show.json", %{data: comment}) do | |
comment | |
|> Map.take(@attributes) | |
|> put_assoc(:user, comment.user) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.MyModelView do | |
use MyApp.Web, :view | |
@attributes ~W(id body title inserted_at) | |
def render("show.json", %{data: my_model}) | |
my_model | |
|> Map.take(@attributes) | |
|> Map.update!(:title, &String.capitalize) | |
|> put_assoc(:user, my_model.user) | |
# Or you could do | |
# |> put_assoc(:user, my_model.user, :lite) | |
|> put_assoc(:comments, my_model.comments) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.UserView do | |
use MyApp.Web, :view | |
@attributes ~W(id name inserted_at) | |
def render("show.json", %{data: user}) | |
render("show_lite.json", %{data: user}) | |
|> Map.put(:image, ImageView.render("show.json", %{data: user.image}) | |
end | |
def render("show_lite.json", %{data: user}) | |
user | |
|> Map.take(@attributes) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Web do | |
def view do | |
quote do | |
# Default code. | |
# You would probably extract these functions to a module. | |
def put_assoc(map, :user, user) do | |
Map.put(map, :user, UserView.render("show.json", %{data: user}) | |
end | |
# Need to customize more? | |
def put_assoc(map, :user, user, :lite) do | |
Map.put(map, :user, UserView.render("show_lite.json", %{data: user}) | |
end | |
def put_assoc(map, :comments, comments) do | |
Map.put(map, :comments, CommentView.render("index.json", %{data: comments}) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I didn't test this so there might be some typos, but I added functions for adding associations more easily. Makes reading what will happen easier. Quicker to scan and see what it's doing. Also easier to use in other views. Still allows for customization and is a bit more imperative.