Created
January 31, 2017 02:04
-
-
Save alduro/738ce02758ffd39c39b9cccf543fe3c4 to your computer and use it in GitHub Desktop.
CategoryView.ex
This file contains 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
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