Skip to content

Commit

Permalink
Merge pull request #541 from rossta/add_full_name_to_user_view
Browse files Browse the repository at this point in the history
Add full name attribute to user view
  • Loading branch information
joshsmith committed Dec 8, 2016
2 parents 1d82926 + 584cd58 commit 79ee20d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
48 changes: 41 additions & 7 deletions test/views/user_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CodeCorps.UserViewTest do
import Phoenix.View, only: [render: 3]

test "renders all attributes and relationships properly" do
user = insert(:user)
user = insert(:user, first_name: "First", last_name: "Last")
organization_membership = insert(:organization_membership, member: user)
slugged_route = insert(:slugged_route, user: user)
stripe_connect_subscription = insert(:stripe_connect_subscription, user: user)
Expand All @@ -23,9 +23,10 @@ defmodule CodeCorps.UserViewTest do
"attributes" => %{
"biography" => user.biography,
"email" => "",
"first-name" => user.first_name,
"first-name" => "First",
"inserted-at" => user.inserted_at,
"last-name" => user.last_name,
"last-name" => "Last",
"name" => "First Last",
"photo-large-url" => CodeCorps.UserPhoto.url({user.photo, user}, :large),
"photo-thumb-url" => CodeCorps.UserPhoto.url({user.photo, user}, :thumb),
"state" => "signed_up",
Expand Down Expand Up @@ -96,11 +97,44 @@ defmodule CodeCorps.UserViewTest do
rendered_json = render(CodeCorps.UserView, "show.json-api", data: users, conn: conn)

emails =
rendered_json["data"]
|> Enum.map(&Map.get(&1, "attributes"))
|> Enum.map(&Map.get(&1, "email"))
|> Enum.filter(fn(email) -> email != "" end)
rendered_json["data"]
|> Enum.map(&Map.get(&1, "attributes"))
|> Enum.map(&Map.get(&1, "email"))
|> Enum.filter(fn(email) -> email != "" end)

assert emails == [auth_user.email]
end

test "renders first and last name as name" do
user = build(:user, first_name: "First", last_name: "Last")

assert render_user_json(user)["data"]["attributes"]["name"] == "First Last"
end

test "renders first name only as name" do
user = build(:user, first_name: "", last_name: "Last")

assert render_user_json(user)["data"]["attributes"]["name"] == "Last"
end

test "renders last name only as name" do
user = build(:user, first_name: "First", last_name: "")

assert render_user_json(user)["data"]["attributes"]["name"] == "First"
end

test "renders nil name if first or last name blank" do
user = build(:user, first_name: "", last_name: "")

assert render_user_json(user)["data"]["attributes"]["name"] == nil

user = build(:user, first_name: nil, last_name: nil)

assert render_user_json(user)["data"]["attributes"]["name"] == nil
end

defp render_user_json(user) do
conn = Phoenix.ConnTest.build_conn |> assign(:current_user, user)
render(CodeCorps.UserView, "show.json-api", data: user, conn: conn)
end
end
13 changes: 12 additions & 1 deletion web/views/user_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule CodeCorps.UserView do
use JaSerializer.PhoenixView

attributes [
:biography, :email, :first_name, :last_name,
:biography, :email, :first_name, :last_name, :name,
:photo_large_url, :photo_thumb_url, :state, :state_transition, :twitter,
:username, :website, :inserted_at, :updated_at
]
Expand Down Expand Up @@ -41,4 +41,15 @@ defmodule CodeCorps.UserView do
if user.id == current_user.id, do: user.email, else: ""
end
def email(_user, _conn), do: ""

@doc """
Returns the user's full name when both first and last name are present.
Returns the only user's first name or last name when the other is missing,
otherwise returns nil.
"""
def name(%{first_name: first_name, last_name: last_name}, _conn) do
"#{first_name} #{last_name}" |> String.trim |> normalize_name
end
defp normalize_name(name) when name in ["", nil], do: nil
defp normalize_name(name), do: name
end

0 comments on commit 79ee20d

Please sign in to comment.