Skip to content

Commit 02423b3

Browse files
committed
Archived tasks don't need task lists or order
1 parent 2010caa commit 02423b3

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

lib/code_corps/model/task.ex

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,27 @@ defmodule CodeCorps.Task do
4141

4242
def changeset(struct, params \\ %{}) do
4343
struct
44-
|> cast(params, [:title, :markdown, :task_list_id, :position])
45-
|> validate_required([:title, :task_list_id])
44+
|> cast(params, [:archived, :title, :markdown, :task_list_id, :position])
45+
|> validate_required([:title])
4646
|> assoc_constraint(:task_list)
47-
|> order_task()
47+
|> handle_archived()
4848
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
4949
end
5050

51+
def handle_archived(changeset) do
52+
case changeset do
53+
%Changeset{valid?: true, changes: %{archived: true}} ->
54+
changeset
55+
|> put_change(:task_list_id, nil)
56+
|> put_change(:order, nil)
57+
_ ->
58+
order_task(changeset)
59+
end
60+
end
61+
5162
def order_task(changeset) do
5263
changeset
64+
|> validate_required([:task_list_id])
5365
|> apply_position()
5466
|> set_order(:position, :order, :task_list_id)
5567
end
@@ -71,7 +83,7 @@ defmodule CodeCorps.Task do
7183
def update_changeset(struct, %{} = params) do
7284
struct
7385
|> changeset(params)
74-
|> cast(params, [:archived, :status])
86+
|> cast(params, [:status])
7587
|> validate_inclusion(:status, statuses())
7688
|> set_closed_at()
7789
|> update_modified_at()

test/lib/code_corps/model/task_test.exs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,37 @@ defmodule CodeCorps.TaskTest do
1616
end
1717

1818
test "renders body html from markdown" do
19-
user = insert(:user)
20-
project = insert(:project)
21-
task_list = insert(:task_list)
2219
changes = Map.merge(@valid_attrs, %{
2320
markdown: "A **strong** body",
24-
project_id: project.id,
25-
task_list_id: task_list.id,
26-
user_id: user.id
21+
project_id: 1,
22+
task_list_id: 1,
23+
user_id: 1
2724
})
2825
changeset = Task.changeset(%Task{}, changes)
2926
assert changeset.valid?
3027
assert changeset |> get_change(:body) == "<p>A <strong>strong</strong> body</p>\n"
3128
end
29+
30+
test "removes the order and task list when the task is archived" do
31+
changes = Map.put(@valid_attrs, :archived, true)
32+
changeset = Task.update_changeset(%Task{order: 1, task_list_id: 1}, changes)
33+
%{archived: archived, order: order, task_list_id: task_list_id} = changeset.changes
34+
assert changeset.valid?
35+
assert archived
36+
refute order
37+
refute task_list_id
38+
end
39+
40+
test "validates task list when the task is not archived and position is set" do
41+
changes = Map.merge(@valid_attrs, %{
42+
position: 1,
43+
project_id: 1,
44+
user_id: 1
45+
})
46+
changeset = Task.changeset(%Task{}, changes)
47+
refute changeset.valid?
48+
assert changeset.errors[:task_list_id]
49+
end
3250
end
3351

3452
describe "create_changeset/2" do
@@ -46,6 +64,26 @@ defmodule CodeCorps.TaskTest do
4664
{:ok, %Task{created_at: created_at, modified_at: modified_at}} = Repo.insert(changeset)
4765
assert created_at == modified_at
4866
end
67+
68+
test "sets the order when the task is not archived and position is set" do
69+
project = insert(:project)
70+
task_list = insert(:task_list)
71+
insert(:task, task_list: task_list, order: 1)
72+
user = insert(:user)
73+
changes = Map.merge(@valid_attrs, %{
74+
position: 1,
75+
project_id: project.id,
76+
task_list_id: task_list.id,
77+
user_id: user.id
78+
})
79+
changeset = Task.create_changeset(%Task{}, changes)
80+
assert changeset.valid?
81+
{:ok, %Task{order: order}} = Repo.insert(changeset)
82+
83+
# We really want to test the order is set, but we have no good way to
84+
# test this since the column default is `0`
85+
assert order > 0
86+
end
4987
end
5088

5189
describe "update_changeset/2" do

0 commit comments

Comments
 (0)