Skip to content

Instantly share code, notes, and snippets.

@alduro
Created January 31, 2017 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alduro/738ce02758ffd39c39b9cccf543fe3c4 to your computer and use it in GitHub Desktop.
Save alduro/738ce02758ffd39c39b9cccf543fe3c4 to your computer and use it in GitHub Desktop.
CategoryView.ex
8 def render("show.json", %{category: category}) do
9 category |> IO.inspect
10 %{id: category.id,
11 name: category.name,
12 slug: category.slug,
13 parent_id: category.parent_id,
14 products: render_many(category.products, ExRushlookApi.ProductView, "show.json", as: :product) }
15 |> add_subcategories(category)
16 end
17
18 def render("category.json", %{category: category}) do
19 %{id: category.id,
20 name: category.name,
21 slug: category.slug,
22 parent_id: category.parent_id}
23 end
24
25 defp add_subcategories(json, %{children: children}) when is_list(children) do
26 results =
27 children
28 |> ExRushlookApi.Repo.preload(:children)
29 |> render_many(ExRushlookApi.CategoryView, "show.json")
30 Map.put(json, :children, results)
31 end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment