diff --git a/lib/code_corps/messages/conversation_parts.ex b/lib/code_corps/messages/conversation_parts.ex index 3d1b047a5..75ac32182 100644 --- a/lib/code_corps/messages/conversation_parts.ex +++ b/lib/code_corps/messages/conversation_parts.ex @@ -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, @@ -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, :part_type, :conversation_id]) |> 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{ comment note reopened closed } + end end diff --git a/lib/code_corps/model/conversation_part.ex b/lib/code_corps/model/conversation_part.ex index 02e92cd85..0520f4100 100644 --- a/lib/code_corps/model/conversation_part.ex +++ b/lib/code_corps/model/conversation_part.ex @@ -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 belongs_to :author, CodeCorps.User belongs_to :conversation, CodeCorps.Conversation diff --git a/priv/repo/migrations/20171220154922_add_part_type_to_conversation.exs b/priv/repo/migrations/20171220154922_add_part_type_to_conversation.exs new file mode 100644 index 000000000..4ea3f639d --- /dev/null +++ b/priv/repo/migrations/20171220154922_add_part_type_to_conversation.exs @@ -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 + end + end +end diff --git a/test/lib/code_corps/messages/conversation_parts_test.exs b/test/lib/code_corps/messages/conversation_parts_test.exs index 947e20493..7008edfc2 100644 --- a/test/lib/code_corps/messages/conversation_parts_test.exs +++ b/test/lib/code_corps/messages/conversation_parts_test.exs @@ -8,7 +8,8 @@ defmodule CodeCorps.Messages.ConversationPartsTest do } @valid_attrs %{ - body: "Test body." + body: "Test body.", + part_type: "comment" } describe "create_changeset/2" do @@ -16,6 +17,33 @@ defmodule CodeCorps.Messages.ConversationPartsTest do attrs = @valid_attrs |> Map.merge(%{author_id: 1, conversation_id: 1}) changeset = ConversationParts.create_changeset(%ConversationPart{}, attrs) assert changeset.valid? + assert changeset.changes.body == "Test body." + assert changeset.changes.part_type == "comment" + end + + test "validates part_type inclusion: note" do + attrs = @valid_attrs |> Map.merge(%{author_id: 1, conversation_id: 1, part_type: "note"}) + changeset = ConversationParts.create_changeset(%ConversationPart{}, attrs) + assert changeset.valid? + 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? + 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? + end + + test "validates part_type inclusion: wat" do + 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