Skip to content

Commit

Permalink
Add part_type to conversation_part
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Dec 21, 2017
1 parent 241e0b1 commit 0e30023
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
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])
|> 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"
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

0 comments on commit 0e30023

Please sign in to comment.