Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include conversation parts in conversation #1349

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
@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
71 changes: 68 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 @@ -19,9 +18,55 @@ defmodule CodeCorpsWeb.ConversationViewTest do
"id" => conversation.id |> Integer.to_string,
"type" => "conversation",
"attributes" => %{
"inserted-at" => conversation.inserted_at,
"read-at" => conversation.read_at,
"status" => conversation.status,
"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,
"type" => "conversation",
"attributes" => %{
"inserted-at" => conversation.inserted_at,
"read-at" => conversation.read_at,
"status" => conversation.status,
"updated-at" => conversation.updated_at
},
"relationships" => %{
Expand Down Expand Up @@ -49,7 +94,27 @@ 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