Skip to content

Commit

Permalink
Merge pull request #1155 from code-corps/1150-1154-archived-tasks-don…
Browse files Browse the repository at this point in the history
…t-need-lists-or-order

Archived tasks don't need task lists or order
  • Loading branch information
joshsmith committed Nov 6, 2017
2 parents 2010caa + a8d5f7c commit ead9892
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
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
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

0 comments on commit ead9892

Please sign in to comment.