Skip to content

Commit

Permalink
Include conversation parts in conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Dec 28, 2017
1 parent 810cc30 commit 238986f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 15 deletions.
11 changes: 8 additions & 3 deletions lib/code_corps_web/controllers/conversation_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ defmodule CodeCorpsWeb.ConversationController do
@spec show(Conn.t, map) :: Conn.t
def show(%Conn{} = conn, %{"id" => id}) do
with %User{} = current_user <- conn |> CodeCorps.Guardian.Plug.current_resource,
%Conversation{} = conversation <- Messages.get_conversation(id) |> preload(),
%Conversation{} = conversation <- Messages.get_conversation(id) |> preload_show(),
{:ok, :authorized} <- current_user |> Policy.authorize(:show, conversation, %{}) do
conn |> render("show.json-api", data: conversation)
conn |> render("show.json-api", data: conversation, opts: [include: "conversation_parts"])
end
end

Expand All @@ -41,9 +41,14 @@ defmodule CodeCorpsWeb.ConversationController do
end
end

@preloads [:conversation_parts, :message, :user]
@preloads [:message, :user, :conversation_parts]
@preloads_show [:message, :user, conversation_parts: [:author]]

def preload(data) do
Repo.preload(data, @preloads)
end

def preload_show(data) do
Repo.preload(data, @preloads_show)
end
end
3 changes: 1 addition & 2 deletions lib/code_corps_web/views/conversation_part_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ defmodule CodeCorpsWeb.ConversationPartView do
attributes [:body, :inserted_at, :read_at, :updated_at]

has_one :author, type: "user", field: :author_id
has_one :conversation, type: "conversation", field: :conversation_id
end
end
2 changes: 1 addition & 1 deletion lib/code_corps_web/views/conversation_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ defmodule CodeCorpsWeb.ConversationView do
has_one :user, type: "user", field: :user_id
has_one :message, type: "message", field: :message_id

has_many :conversation_parts, serializer: CodeCorpsWeb.ConversationPartView, identifiers: :always
has_many :conversation_parts, serializer: CodeCorpsWeb.ConversationPartView, include: false, identifiers: :when_included
end
6 changes: 0 additions & 6 deletions test/lib/code_corps_web/views/conversation_part_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ defmodule CodeCorpsWeb.ConversationPartViewTest do
"id" => conversation_part.author_id |> Integer.to_string,
"type" => "user"
}
},
"conversation" => %{
"data" => %{
"id" => conversation_part.conversation_id |> Integer.to_string,
"type" => "conversation"
}
}
}
},
Expand Down
69 changes: 66 additions & 3 deletions test/lib/code_corps_web/views/conversation_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ defmodule CodeCorpsWeb.ConversationViewTest do

alias CodeCorps.Repo

test "renders all attributes and relationships properly" do
test "renders index attributes and relationships properly" do
conversation = insert(:conversation)
conversation_part = insert(:conversation_part, conversation: conversation)

rendered_json =
CodeCorpsWeb.ConversationView
Expand All @@ -14,6 +13,52 @@ defmodule CodeCorpsWeb.ConversationViewTest do
data: conversation |> Repo.preload(:conversation_parts)
)

expected_json = %{
"data" => %{
"id" => conversation.id |> Integer.to_string,
"type" => "conversation",
"attributes" => %{
"read-at" => conversation.read_at,
"status" => conversation.status,
"inserted-at" => conversation.inserted_at,
"updated-at" => conversation.updated_at
},
"relationships" => %{
"conversation-parts" => %{},
"message" => %{
"data" => %{
"id" => conversation.message_id |> Integer.to_string,
"type" => "message"
}
},
"user" => %{
"data" => %{
"id" => conversation.user_id |> Integer.to_string,
"type" => "user"
}
}
}
},
"jsonapi" => %{
"version" => "1.0"
}
}

assert rendered_json == expected_json
end

test "renders show attributes and relationships properly" do
conversation = insert(:conversation)
conversation_part = insert(:conversation_part, conversation: conversation)

rendered_json =
CodeCorpsWeb.ConversationView
|> render(
"show.json-api",
data: conversation |> Repo.preload(:conversation_parts),
opts: [include: "conversation_parts"]
)

expected_json = %{
"data" => %{
"id" => conversation.id |> Integer.to_string,
Expand Down Expand Up @@ -49,7 +94,25 @@ defmodule CodeCorpsWeb.ConversationViewTest do
},
"jsonapi" => %{
"version" => "1.0"
}
},
"included" => [%{
"attributes" => %{
"body" => conversation_part.body,
"inserted-at" => conversation_part.inserted_at,
"read-at" => conversation_part.read_at,
"updated-at" => conversation_part.updated_at
},
"relationships" => %{
"author" => %{
"data" => %{
"id" => conversation_part.author.id |> Integer.to_string,
"type" => "user"
}
}
},
"id" => conversation_part.id |> Integer.to_string,
"type" => "conversation-part"
}]
}

assert rendered_json == expected_json
Expand Down

0 comments on commit 238986f

Please sign in to comment.