Skip to content
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
20 changes: 16 additions & 4 deletions lib/code_corps/model/task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,27 @@ defmodule CodeCorps.Task do

def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:title, :markdown, :task_list_id, :position])
|> validate_required([:title, :task_list_id])
|> cast(params, [:archived, :title, :markdown, :task_list_id, :position])
|> validate_required([:title])
|> assoc_constraint(:task_list)
|> order_task()
|> handle_archived()
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
end

def handle_archived(changeset) do
case get_field(changeset, :archived) do
true ->
changeset
|> put_change(:task_list_id, nil)
|> put_change(:order, nil)
_ ->
order_task(changeset)
end
end

def order_task(changeset) do
changeset
|> validate_required([:task_list_id])
|> apply_position()
|> set_order(:position, :order, :task_list_id)
end
Expand All @@ -71,7 +83,7 @@ defmodule CodeCorps.Task do
def update_changeset(struct, %{} = params) do
struct
|> changeset(params)
|> cast(params, [:archived, :status])
|> cast(params, [:status])
|> validate_inclusion(:status, statuses())
|> set_closed_at()
|> update_modified_at()
Expand Down
68 changes: 62 additions & 6 deletions test/lib/code_corps/model/task_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,37 @@ defmodule CodeCorps.TaskTest do
end

test "renders body html from markdown" do
user = insert(:user)
project = insert(:project)
task_list = insert(:task_list)
changes = Map.merge(@valid_attrs, %{
markdown: "A **strong** body",
project_id: project.id,
task_list_id: task_list.id,
user_id: user.id
project_id: 1,
task_list_id: 1,
user_id: 1
})
changeset = Task.changeset(%Task{}, changes)
assert changeset.valid?
assert changeset |> get_change(:body) == "<p>A <strong>strong</strong> body</p>\n"
end

test "removes the order and task list when the task is archived" do
changes = Map.put(@valid_attrs, :archived, true)
changeset = Task.update_changeset(%Task{order: 1, task_list_id: 1}, changes)
%{archived: archived, order: order, task_list_id: task_list_id} = changeset.changes
assert changeset.valid?
assert archived
refute order
refute task_list_id
end

test "validates task list when the task is not archived and position is set" do
changes = Map.merge(@valid_attrs, %{
position: 1,
project_id: 1,
user_id: 1
})
changeset = Task.changeset(%Task{}, changes)
refute changeset.valid?
assert changeset.errors[:task_list_id]
end
end

describe "create_changeset/2" do
Expand All @@ -46,6 +64,26 @@ defmodule CodeCorps.TaskTest do
{:ok, %Task{created_at: created_at, modified_at: modified_at}} = Repo.insert(changeset)
assert created_at == modified_at
end

test "sets the order when the task is not archived and position is set" do
project = insert(:project)
task_list = insert(:task_list)
insert(:task, task_list: task_list, order: 1)
user = insert(:user)
changes = Map.merge(@valid_attrs, %{
position: 1,
project_id: project.id,
task_list_id: task_list.id,
user_id: user.id
})
changeset = Task.create_changeset(%Task{}, changes)
assert changeset.valid?
{:ok, %Task{order: order}} = Repo.insert(changeset)

# We really want to test the order is set, but we have no good way to
# test this since the column default is `0`
assert order !== 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah so assert order fails. Would have thought since refute only handles nil and false that assert would have worked! 🚶‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

assert order succeeds but that's insufficient to tell us anything. Since the default is 0 it could just be set to 0, but instead it will receive a random ranked value from EctoOrdered.

end
end

describe "update_changeset/2" do
Expand Down Expand Up @@ -78,5 +116,23 @@ defmodule CodeCorps.TaskTest do
assert changeset.valid?
assert archived
end

test "does not reset order when task was already archived" do
project = insert(:project)
user = insert(:user)
changes = Map.merge(@valid_attrs, %{
archived: true,
position: 1,
project_id: project.id,
user_id: user.id
})
changeset = Task.create_changeset(%Task{}, changes)
{:ok, %Task{order: order} = task} = Repo.insert(changeset)
refute order

changeset = Task.update_changeset(task, %{title: "New title"})
{:ok, %Task{order: order}} = Repo.update(changeset)
refute order
end
end
end