Skip to content

Commit 8caa708

Browse files
authored
Merge pull request #1325 from code-corps/part-type
Add part_type to conversation_part
2 parents 241e0b1 + 0e30023 commit 8caa708

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

lib/code_corps/messages/conversation_parts.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ defmodule CodeCorps.Messages.ConversationParts do
44
i.e. a reply to the `CodeCorps.Conversation` by any participant.
55
"""
66

7-
import Ecto.Changeset, only: [assoc_constraint: 2, cast: 3, validate_required: 2]
7+
import Ecto.Changeset, only: [assoc_constraint: 2, cast: 3, validate_required: 2,
8+
validate_inclusion: 3]
89

910
alias CodeCorps.{
1011
ConversationPart,
@@ -24,9 +25,14 @@ defmodule CodeCorps.Messages.ConversationParts do
2425
@spec create_changeset(ConversationPart.t, map) :: Ecto.Changeset.t
2526
def create_changeset(%ConversationPart{} = conversation_part, attrs) do
2627
conversation_part
27-
|> cast(attrs, [:author_id, :body, :conversation_id])
28+
|> cast(attrs, [:author_id, :body, :conversation_id, :part_type])
2829
|> validate_required([:author_id, :body, :conversation_id])
30+
|> validate_inclusion(:part_type, part_types())
2931
|> assoc_constraint(:author)
3032
|> assoc_constraint(:conversation)
3133
end
34+
35+
defp part_types do
36+
~w{ closed comment note reopened }
37+
end
3238
end

lib/code_corps/model/conversation_part.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ defmodule CodeCorps.ConversationPart do
1919
schema "conversation_parts" do
2020
field :body, :string, null: false
2121
field :read_at, :utc_datetime, null: true
22+
field :part_type, :string, default: "comment"
2223

2324
belongs_to :author, CodeCorps.User
2425
belongs_to :conversation, CodeCorps.Conversation
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule CodeCorps.Repo.Migrations.AddPartTypeToConversation do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:conversation_parts) do
6+
add :part_type, :string, default: "comment"
7+
end
8+
end
9+
end

test/lib/code_corps/messages/conversation_parts_test.exs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,45 @@ defmodule CodeCorps.Messages.ConversationPartsTest do
1313

1414
describe "create_changeset/2" do
1515
test "with valid attributes" do
16+
user_id = insert(:user, id: 1).id
17+
insert(:conversation, id: 1)
1618
attrs = @valid_attrs |> Map.merge(%{author_id: 1, conversation_id: 1})
19+
{:ok, conversation} = ConversationParts.create_changeset(%ConversationPart{}, attrs) |> Repo.insert
20+
assert conversation.body == "Test body."
21+
assert conversation.part_type == "comment"
22+
assert conversation.author_id == user_id
23+
end
24+
25+
test "validates part_type inclusion: note" do
26+
insert(:user, id: 1)
27+
insert(:conversation, id: 1)
28+
attrs = @valid_attrs |> Map.merge(%{author_id: 1, conversation_id: 1, part_type: "note"})
29+
changeset = ConversationParts.create_changeset(%ConversationPart{}, attrs)
30+
assert changeset.valid?
31+
assert changeset.changes.part_type == "note"
32+
end
33+
34+
test "validates part_type inclusion: reopened" do
35+
attrs = @valid_attrs |> Map.merge(%{author_id: 1, conversation_id: 1, part_type: "reopened"})
1736
changeset = ConversationParts.create_changeset(%ConversationPart{}, attrs)
1837
assert changeset.valid?
38+
assert changeset.changes.part_type == "reopened"
39+
end
40+
41+
test "validates part_type inclusion: closed" do
42+
attrs = @valid_attrs |> Map.merge(%{author_id: 1, conversation_id: 1, part_type: "closed"})
43+
changeset = ConversationParts.create_changeset(%ConversationPart{}, attrs)
44+
assert changeset.valid?
45+
assert changeset.changes.part_type == "closed"
46+
end
47+
48+
test "validates part_type inclusion: wat" do
49+
insert(:user, id: 1)
50+
insert(:conversation, id: 1)
51+
attrs = @valid_attrs |> Map.merge(%{author_id: 1, conversation_id: 1, part_type: "wat"})
52+
changeset = ConversationParts.create_changeset(%ConversationPart{}, attrs)
53+
refute changeset.valid?
54+
assert_error_message(changeset, :part_type, "is invalid")
1955
end
2056

2157
test "requires author_id" do

0 commit comments

Comments
 (0)