diff --git a/lib/bokken_web/controllers/admin/ninja/ninja_json.ex b/lib/bokken_web/controllers/admin/ninja/ninja_json.ex index 76c5d902..14740746 100644 --- a/lib/bokken_web/controllers/admin/ninja/ninja_json.ex +++ b/lib/bokken_web/controllers/admin/ninja/ninja_json.ex @@ -1,11 +1,12 @@ defmodule BokkenWeb.Admin.NinjaJSON do + alias Bokken.Accounts.Ninja alias Bokken.Uploaders.Avatar def index(%{ninjas: ninjas}) do %{data: for(ninja <- ninjas, do: data(ninja))} end - def data(%{ninja: ninja}) do + def data(%Ninja{} = ninja) do %{ id: ninja.id, photo: Avatar.url({ninja.photo, ninja}, :thumb), @@ -16,10 +17,10 @@ defmodule BokkenWeb.Admin.NinjaJSON do socials: ninja.socials, since: ninja.inserted_at } - |> Map.put(:guardian, guardian_attrs(ninja)) + |> Map.put(:guardian, guardian(ninja)) end - defp guardian_attrs(ninja) do + defp guardian(ninja) do if Ecto.assoc_loaded?(ninja.guardian) do %{ id: ninja.guardian.id, diff --git a/lib/bokken_web/views/auth_view.ex b/lib/bokken_web/views/auth_view.ex index fb8d6535..56b7d32e 100644 --- a/lib/bokken_web/views/auth_view.ex +++ b/lib/bokken_web/views/auth_view.ex @@ -4,7 +4,7 @@ defmodule BokkenWeb.AuthView do alias BokkenWeb.AuthView alias BokkenWeb.GuardianView alias BokkenWeb.MentorView - alias BokkenWeb.NinjaView + alias BokkenWeb.NinjaJSON alias BokkenWeb.OrganizerView def render("me.json", %{user: %{registered: false} = user}) do @@ -24,7 +24,7 @@ defmodule BokkenWeb.AuthView do end def render("me.json", %{user: %{role: :ninja, ninja: ninja} = user}) do - render_one(ninja, NinjaView, "ninja.json", current_user: user) + NinjaJSON.ninja(%{ninja: ninja, current_user: user}) |> Map.merge(render_one(user, AuthView, "user.json")) |> Map.put(:ninja_id, ninja.id) end diff --git a/lib/bokken_web/views/enrollment_view.ex b/lib/bokken_web/views/enrollment_view.ex index 727f15dd..919492ed 100644 --- a/lib/bokken_web/views/enrollment_view.ex +++ b/lib/bokken_web/views/enrollment_view.ex @@ -3,7 +3,7 @@ defmodule BokkenWeb.EnrollmentView do alias BokkenWeb.EnrollmentView alias BokkenWeb.EventView - alias BokkenWeb.NinjaView + alias BokkenWeb.NinjaJSON def render("index.json", %{enrollments: enrollments, current_user: current_user}) do %{ @@ -39,7 +39,7 @@ defmodule BokkenWeb.EnrollmentView do defp ninja(enrollment, current_user) do if Ecto.assoc_loaded?(enrollment.ninja) do - %{ninja: render_one(enrollment.ninja, NinjaView, "ninja.json", current_user: current_user)} + %{ninja: NinjaJSON.ninja(%{ninja: enrollment.ninja, current_user: current_user})} else %{ninja_id: enrollment.ninja_id} end diff --git a/lib/bokken_web/views/lecture_view.ex b/lib/bokken_web/views/lecture_view.ex index a7e5f9fb..a2324756 100644 --- a/lib/bokken_web/views/lecture_view.ex +++ b/lib/bokken_web/views/lecture_view.ex @@ -5,7 +5,7 @@ defmodule BokkenWeb.LectureView do alias BokkenWeb.FileJSON alias BokkenWeb.LectureView alias BokkenWeb.MentorView - alias BokkenWeb.NinjaView + alias BokkenWeb.NinjaJSON def render("index.json", %{lectures: lectures, current_user: current_user}) do %{data: render_many(lectures, LectureView, "lecture.json", current_user: current_user)} @@ -35,7 +35,7 @@ defmodule BokkenWeb.LectureView do defp ninja(lecture, current_user) do if Ecto.assoc_loaded?(lecture.ninja) do - %{ninja: render_one(lecture.ninja, NinjaView, "ninja.json", current_user: current_user)} + %{ninja: NinjaJSON.ninja(%{ninja: lecture.ninja, current_user: current_user})} else %{ninja_id: lecture.ninja_id} end diff --git a/lib/bokken_web/views/ninja_view.ex b/lib/bokken_web/views/ninja_view.ex deleted file mode 100644 index 337b335a..00000000 --- a/lib/bokken_web/views/ninja_view.ex +++ /dev/null @@ -1,59 +0,0 @@ -defmodule BokkenWeb.NinjaView do - use BokkenWeb, :view - - alias Bokken.Uploaders.Avatar - alias BokkenWeb.{NinjaView, SkillView} - - def render("index.json", %{ninjas: ninjas, current_user: current_user}) do - %{data: render_many(ninjas, NinjaView, "ninja.json", current_user: current_user)} - end - - def render("show.json", %{ninja: ninja, current_user: current_user}) do - %{data: render_one(ninja, NinjaView, "ninja.json", current_user: current_user)} - end - - def render("ninja.json", %{ninja: ninja, current_user: current_user}) do - data(ninja) - |> Map.merge(personal(ninja, current_user)) - |> Map.merge(sensitive(ninja, current_user)) - end - - defp data(ninja) do - %{ - id: ninja.id, - photo: Avatar.url({ninja.photo, ninja}, :thumb), - first_name: ninja.first_name, - last_name: ninja.last_name, - belt: ninja.belt, - socials: ninja.socials, - since: ninja.inserted_at, - guardian_id: ninja.guardian_id - } - |> Map.merge(skills(ninja)) - end - - defp personal(ninja, current_user) - when current_user.role == :organizer or current_user.id == ninja.id do - %{ - birthday: ninja.birthday - } - end - - defp personal(_ninja, _current_user), do: %{} - - defp sensitive(ninja, current_user) when current_user.role in [:organizer, :mentor] do - %{ - notes: ninja.notes - } - end - - defp sensitive(_ninja, _current_user), do: %{} - - defp skills(ninja) do - if Ecto.assoc_loaded?(ninja.skills) do - %{skills: render_many(ninja.skills, SkillView, "skill.json")} - else - %{} - end - end -end