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

Add part_type to conversation_part #1325

Merged
merged 1 commit into from
Dec 21, 2017
Merged
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
10 changes: 8 additions & 2 deletions lib/code_corps/messages/conversation_parts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ defmodule CodeCorps.Messages.ConversationParts do
i.e. a reply to the `CodeCorps.Conversation` by any participant.
"""

import Ecto.Changeset, only: [assoc_constraint: 2, cast: 3, validate_required: 2]
import Ecto.Changeset, only: [assoc_constraint: 2, cast: 3, validate_required: 2,
validate_inclusion: 3]

alias CodeCorps.{
ConversationPart,
Expand All @@ -24,9 +25,14 @@ defmodule CodeCorps.Messages.ConversationParts do
@spec create_changeset(ConversationPart.t, map) :: Ecto.Changeset.t
def create_changeset(%ConversationPart{} = conversation_part, attrs) do
conversation_part
|> cast(attrs, [:author_id, :body, :conversation_id])
|> cast(attrs, [:author_id, :body, :conversation_id, :part_type])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this will need an update cast as well? Don't see an update action however.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we would cast this in an update, since the part type should never change.

|> validate_required([:author_id, :body, :conversation_id])
|> validate_inclusion(:part_type, part_types())
|> assoc_constraint(:author)
|> assoc_constraint(:conversation)
end

defp part_types do
~w{ closed comment note reopened }
end
end
1 change: 1 addition & 0 deletions lib/code_corps/model/conversation_part.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule CodeCorps.ConversationPart do
schema "conversation_parts" do
field :body, :string, null: false
field :read_at, :utc_datetime, null: true
field :part_type, :string, default: "comment"

belongs_to :author, CodeCorps.User
belongs_to :conversation, CodeCorps.Conversation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule CodeCorps.Repo.Migrations.AddPartTypeToConversation do
use Ecto.Migration

def change do
alter table(:conversation_parts) do
add :part_type, :string, default: "comment"
end
end
end
36 changes: 36 additions & 0 deletions test/lib/code_corps/messages/conversation_parts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,45 @@ defmodule CodeCorps.Messages.ConversationPartsTest do

describe "create_changeset/2" do
test "with valid attributes" do
user_id = insert(:user, id: 1).id
insert(:conversation, id: 1)
attrs = @valid_attrs |> Map.merge(%{author_id: 1, conversation_id: 1})
{:ok, conversation} = ConversationParts.create_changeset(%ConversationPart{}, attrs) |> Repo.insert
assert conversation.body == "Test body."
assert conversation.part_type == "comment"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you also probably want to assert the conversation.user_id here is equal to the user.id.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

assert conversation.author_id == user_id
end

test "validates part_type inclusion: note" do
insert(:user, id: 1)
insert(:conversation, id: 1)
attrs = @valid_attrs |> Map.merge(%{author_id: 1, conversation_id: 1, part_type: "note"})
changeset = ConversationParts.create_changeset(%ConversationPart{}, attrs)
assert changeset.valid?
assert changeset.changes.part_type == "note"
end

test "validates part_type inclusion: reopened" do
attrs = @valid_attrs |> Map.merge(%{author_id: 1, conversation_id: 1, part_type: "reopened"})
changeset = ConversationParts.create_changeset(%ConversationPart{}, attrs)
assert changeset.valid?
assert changeset.changes.part_type == "reopened"
end

test "validates part_type inclusion: closed" do
attrs = @valid_attrs |> Map.merge(%{author_id: 1, conversation_id: 1, part_type: "closed"})
changeset = ConversationParts.create_changeset(%ConversationPart{}, attrs)
assert changeset.valid?
assert changeset.changes.part_type == "closed"
end

test "validates part_type inclusion: wat" do
insert(:user, id: 1)
insert(:conversation, id: 1)
attrs = @valid_attrs |> Map.merge(%{author_id: 1, conversation_id: 1, part_type: "wat"})
changeset = ConversationParts.create_changeset(%ConversationPart{}, attrs)
refute changeset.valid?
assert_error_message(changeset, :part_type, "is invalid")
end

test "requires author_id" do
Expand Down