Skip to content

Commit 1ba8911

Browse files
authored
Merge pull request #1157 from code-corps/task-list-modify
Add pull_requests && done to task_list
2 parents ead9892 + 994f7f3 commit 1ba8911

8 files changed

Lines changed: 265 additions & 12 deletions

File tree

lib/code_corps/model/task_list.ex

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ defmodule CodeCorps.TaskList do
77
@type t :: %__MODULE__{}
88

99
schema "task_lists" do
10+
field :done, :boolean, default: false
1011
field :inbox, :boolean, default: false
1112
field :name, :string
1213
field :order, :integer
1314
field :position, :integer, virtual: true
15+
field :pull_requests, :boolean, default: false
1416

1517
belongs_to :project, CodeCorps.Project
1618
has_many :tasks, CodeCorps.Task
@@ -29,11 +31,11 @@ defmodule CodeCorps.TaskList do
2931
name: "Backlog",
3032
position: 2
3133
}, %{
32-
inbox: false,
34+
pull_requests: true,
3335
name: "In Progress",
3436
position: 3
3537
}, %{
36-
inbox: false,
38+
done: true,
3739
name: "Done",
3840
position: 4
3941
}
@@ -52,8 +54,11 @@ defmodule CodeCorps.TaskList do
5254

5355
def create_changeset(struct, params) do
5456
struct
55-
|> cast(params, [:inbox])
57+
|> cast(params, [:done, :inbox, :pull_requests])
5658
|> changeset(params)
57-
|> validate_required([:inbox])
59+
|> validate_required([:done, :inbox, :pull_requests])
60+
|> unique_constraint(:done, name: "task_lists_project_id_done_index")
61+
|> unique_constraint(:inbox, name: "task_lists_project_id_inbox_index")
62+
|> unique_constraint(:pull_requests, name: "task_lists_project_id_pull_requests_index")
5863
end
5964
end

lib/code_corps_web/views/task_list_view.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule CodeCorpsWeb.TaskListView do
33
use CodeCorpsWeb, :view
44
use JaSerializer.PhoenixView
55

6-
attributes [:inbox, :name, :order, :inserted_at, :updated_at]
6+
attributes [:done, :inbox, :name, :order, :pull_requests, :inserted_at, :updated_at]
77

88
has_one :project, type: "project", field: :project_id
99

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
defmodule CodeCorps.Repo.Migrations.AddDoneToTaskList do
2+
use Ecto.Migration
3+
4+
import Ecto.Query
5+
6+
alias CodeCorps.Repo
7+
8+
def up do
9+
alter table(:task_lists) do
10+
add :done, :boolean, default: false
11+
end
12+
13+
flush()
14+
15+
from(tl in "task_lists", where: [name: "Done"], update: [set: [done: true]])
16+
|> Repo.update_all([])
17+
18+
task_list_query =
19+
from(tl in "task_lists", where: [done: true], select: [:id])
20+
21+
# tests do not have any data, so we need to account for potential nil
22+
case task_list_query |> Repo.one do
23+
%{id: done_list_id} ->
24+
task_update_query = from t in "tasks",
25+
where: [status: "closed"],
26+
update: [set: [task_list_id: ^done_list_id]]
27+
task_update_query |> Repo.update_all([])
28+
nil -> nil
29+
end
30+
end
31+
32+
def down do
33+
alter table(:task_lists) do
34+
remove :done
35+
end
36+
end
37+
end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
defmodule CodeCorps.Repo.Migrations.AddPullRequestsToTaskList do
2+
use Ecto.Migration
3+
4+
import Ecto.Query
5+
6+
alias CodeCorps.Repo
7+
8+
def up do
9+
alter table(:task_lists) do
10+
add :pull_requests, :boolean, default: false
11+
end
12+
13+
flush()
14+
15+
# set all "In Progress" task lists to now contain pull requests
16+
from(
17+
tl in "task_lists",
18+
where: [name: "In Progress"],
19+
update: [set: [pull_requests: true]]
20+
) |> Repo.update_all([])
21+
22+
# get projects paired with associated pull request task list as ids
23+
task_parent_data = from(
24+
p in "projects",
25+
left_join:
26+
tl in "task_lists",
27+
on: tl.project_id == p.id,
28+
where: tl.pull_requests == true,
29+
select: {p.id, tl.id}
30+
) |> Repo.all
31+
32+
# get all tasks for projects, associated to github pull requests and
33+
# assign them to the pull request task list
34+
task_parent_data |> Enum.each(fn {project_id, pr_list_id} ->
35+
from(
36+
t in "tasks",
37+
where: [project_id: ^project_id],
38+
where: t.status != "closed",
39+
where: not is_nil(t.github_issue_id),
40+
inner_join:
41+
gi in "github_issues",
42+
on: t.github_issue_id == gi.id,
43+
where: not is_nil(gi.github_pull_request_id),
44+
update: [set: [task_list_id: ^pr_list_id]]
45+
) |> Repo.update_all([])
46+
end)
47+
end
48+
49+
def down do
50+
alter table(:task_lists) do
51+
remove :pull_requests
52+
end
53+
end
54+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule CodeCorps.Repo.Migrations.AddUniqueConstraintsToSpecificTaskLists do
2+
@moduledoc false
3+
4+
use Ecto.Migration
5+
6+
def change do
7+
# There is already a "task_lists_project_id_index", so we name explicitly
8+
9+
create unique_index(
10+
"task_lists", [:project_id],
11+
where: "done = true", name: "task_lists_project_id_done_index")
12+
13+
create unique_index(
14+
"task_lists", [:project_id],
15+
where: "pull_requests = true", name: "task_lists_project_id_pull_requests_index")
16+
17+
create unique_index(
18+
"task_lists", [:project_id],
19+
where: "inbox = true", name: "task_lists_project_id_inbox_index")
20+
end
21+
end

priv/repo/structure.sql

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,9 @@ CREATE TABLE task_lists (
14861486
project_id bigint,
14871487
inserted_at timestamp without time zone NOT NULL,
14881488
updated_at timestamp without time zone NOT NULL,
1489-
inbox boolean DEFAULT false
1489+
inbox boolean DEFAULT false,
1490+
done boolean DEFAULT false,
1491+
pull_requests boolean DEFAULT false
14901492
);
14911493

14921494

@@ -3072,13 +3074,34 @@ CREATE INDEX task_lists_inbox_index ON task_lists USING btree (inbox);
30723074
CREATE INDEX task_lists_order_index ON task_lists USING btree ("order");
30733075

30743076

3077+
--
3078+
-- Name: task_lists_project_id_done_index; Type: INDEX; Schema: public; Owner: -
3079+
--
3080+
3081+
CREATE UNIQUE INDEX task_lists_project_id_done_index ON task_lists USING btree (project_id) WHERE (done = true);
3082+
3083+
3084+
--
3085+
-- Name: task_lists_project_id_inbox_index; Type: INDEX; Schema: public; Owner: -
3086+
--
3087+
3088+
CREATE UNIQUE INDEX task_lists_project_id_inbox_index ON task_lists USING btree (project_id) WHERE (inbox = true);
3089+
3090+
30753091
--
30763092
-- Name: task_lists_project_id_index; Type: INDEX; Schema: public; Owner: -
30773093
--
30783094

30793095
CREATE INDEX task_lists_project_id_index ON task_lists USING btree (project_id);
30803096

30813097

3098+
--
3099+
-- Name: task_lists_project_id_pull_requests_index; Type: INDEX; Schema: public; Owner: -
3100+
--
3101+
3102+
CREATE UNIQUE INDEX task_lists_project_id_pull_requests_index ON task_lists USING btree (project_id) WHERE (pull_requests = true);
3103+
3104+
30823105
--
30833106
-- Name: task_skills_skill_id_index; Type: INDEX; Schema: public; Owner: -
30843107
--
@@ -3844,5 +3867,5 @@ ALTER TABLE ONLY users
38443867
-- PostgreSQL database dump complete
38453868
--
38463869

3847-
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713), (20170308222552), (20170313130611), (20170318032449), (20170318082740), (20170324194827), (20170424215355), (20170501225441), (20170505224222), (20170526095401), (20170602000208), (20170622205732), (20170626231059), (20170628092119), (20170628213609), (20170629183404), (20170630140136), (20170706132431), (20170707213648), (20170711122252), (20170717092127), (20170725060612), (20170727052644), (20170731130121), (20170814131722), (20170913114958), (20170921014405), (20170925214512), (20170925230419), (20170926134646), (20170927100300), (20170928234412), (20171003134956), (20171003225853), (20171006063358), (20171006161407), (20171012215106), (20171012221231), (20171016125229), (20171016125516), (20171016223356), (20171016235656), (20171017235433), (20171019191035), (20171025184225), (20171026010933), (20171027061833), (20171028011642), (20171028173508), (20171030182857), (20171031232023), (20171031234356), (20171101023309), (20171104013543);
3870+
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713), (20170308222552), (20170313130611), (20170318032449), (20170318082740), (20170324194827), (20170424215355), (20170501225441), (20170505224222), (20170526095401), (20170602000208), (20170622205732), (20170626231059), (20170628092119), (20170628213609), (20170629183404), (20170630140136), (20170706132431), (20170707213648), (20170711122252), (20170717092127), (20170725060612), (20170727052644), (20170731130121), (20170814131722), (20170913114958), (20170921014405), (20170925214512), (20170925230419), (20170926134646), (20170927100300), (20170928234412), (20171003134956), (20171003225853), (20171006063358), (20171006161407), (20171012215106), (20171012221231), (20171016125229), (20171016125516), (20171016223356), (20171016235656), (20171017235433), (20171019191035), (20171025184225), (20171026010933), (20171027061833), (20171028011642), (20171028173508), (20171030182857), (20171031232023), (20171031234356), (20171101023309), (20171104013543), (20171106045740), (20171106050209), (20171106103153);
38483871

test/lib/code_corps/model/task_list_test.exs

Lines changed: 116 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule CodeCorps.TaskListTest do
22
use CodeCorps.ModelCase
33

44
alias CodeCorps.TaskList
5+
alias Ecto.Changeset
56

67
@valid_attrs %{name: "some content", position: 42}
78
@invalid_attrs %{}
@@ -16,12 +17,122 @@ defmodule CodeCorps.TaskListTest do
1617
refute changeset.valid?
1718
end
1819

19-
test "is not inbox by default" do
20+
test "defaults :done to 'false'" do
2021
{:ok, record} =
21-
%TaskList{}
22-
|> TaskList.changeset(@valid_attrs)
23-
|> CodeCorps.Repo.insert
22+
%TaskList{} |> TaskList.changeset(@valid_attrs) |> Repo.insert
23+
assert record.done == false
24+
end
25+
26+
test "defaults :inbox to 'false'" do
27+
{:ok, record} =
28+
%TaskList{} |> TaskList.changeset(@valid_attrs) |> Repo.insert
29+
assert record.inbox == false
30+
end
31+
32+
test "defaults :pull_requests to 'false'" do
33+
{:ok, record} =
34+
%TaskList{} |> TaskList.changeset(@valid_attrs) |> Repo.insert
35+
assert record.pull_requests == false
36+
end
37+
38+
describe "create_changeset" do
39+
test "casts done" do
40+
attrs = @valid_attrs |> Map.merge(%{done: true})
41+
changeset = %TaskList{} |> TaskList.create_changeset(attrs)
42+
assert changeset |> Changeset.get_change(:done) == true
43+
end
44+
45+
test "casts inbox" do
46+
attrs = @valid_attrs |> Map.merge(%{inbox: true})
47+
changeset = %TaskList{} |> TaskList.create_changeset(attrs)
48+
assert changeset |> Changeset.get_change(:inbox) == true
49+
end
50+
51+
test "casts pull_requests" do
52+
attrs = @valid_attrs |> Map.merge(%{pull_requests: true})
53+
changeset = %TaskList{} |> TaskList.create_changeset(attrs)
54+
assert changeset |> Changeset.get_change(:pull_requests) == true
55+
end
56+
57+
test "requires done" do
58+
attrs = @valid_attrs |> Map.merge(%{done: nil})
59+
changeset = %TaskList{} |> TaskList.create_changeset(attrs)
60+
refute changeset.valid?
61+
assert changeset |> Map.get(:errors) |> Keyword.get(:done)
62+
end
63+
64+
test "requires inbox" do
65+
attrs = @valid_attrs |> Map.merge(%{inbox: nil})
66+
changeset = %TaskList{} |> TaskList.create_changeset(attrs)
67+
refute changeset.valid?
68+
assert changeset |> Map.get(:errors) |> Keyword.get(:inbox)
69+
end
70+
71+
test "requires pull_requests" do
72+
attrs = @valid_attrs |> Map.merge(%{pull_requests: nil})
73+
changeset = %TaskList{} |> TaskList.create_changeset(attrs)
74+
refute changeset.valid?
75+
assert changeset |> Map.get(:errors) |> Keyword.get(:pull_requests)
76+
end
77+
78+
test "ensures a unique 'done' task list per project" do
79+
%{id: project_id} = insert(:project)
80+
attrs = @valid_attrs |> Map.merge(%{done: true})
81+
82+
{:ok, _task_list} =
83+
%TaskList{}
84+
|> TaskList.create_changeset(attrs)
85+
|> Changeset.put_change(:project_id, project_id)
86+
|> Repo.insert
87+
88+
{:error, changeset} =
89+
%TaskList{}
90+
|> TaskList.create_changeset(attrs)
91+
|> Changeset.put_change(:project_id, project_id)
92+
|> Repo.insert
93+
94+
refute changeset.valid?
95+
assert changeset |> Map.get(:errors) |> Keyword.get(:done)
96+
end
97+
98+
test "ensures a unique 'inbox' task list per project" do
99+
%{id: project_id} = insert(:project)
100+
attrs = @valid_attrs |> Map.merge(%{inbox: true})
101+
102+
{:ok, _task_list} =
103+
%TaskList{}
104+
|> TaskList.create_changeset(attrs)
105+
|> Changeset.put_change(:project_id, project_id)
106+
|> Repo.insert
107+
108+
{:error, changeset} =
109+
%TaskList{}
110+
|> TaskList.create_changeset(attrs)
111+
|> Changeset.put_change(:project_id, project_id)
112+
|> Repo.insert
113+
114+
refute changeset.valid?
115+
assert changeset |> Map.get(:errors) |> Keyword.get(:inbox)
116+
end
117+
118+
test "ensures a unique 'pull_requests' task list per project" do
119+
%{id: project_id} = insert(:project)
120+
attrs = @valid_attrs |> Map.merge(%{pull_requests: true})
121+
122+
{:ok, _task_list} =
123+
%TaskList{}
124+
|> TaskList.create_changeset(attrs)
125+
|> Changeset.put_change(:project_id, project_id)
126+
|> Repo.insert
127+
128+
{:error, changeset} =
129+
%TaskList{}
130+
|> TaskList.create_changeset(attrs)
131+
|> Changeset.put_change(:project_id, project_id)
132+
|> Repo.insert
24133

25-
refute record.inbox
134+
refute changeset.valid?
135+
assert changeset |> Map.get(:errors) |> Keyword.get(:pull_requests)
136+
end
26137
end
27138
end

test/lib/code_corps_web/views/task_list_view_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ defmodule CodeCorpsWeb.TaskListViewTest do
1212
expected_json = %{
1313
"data" => %{
1414
"attributes" => %{
15+
"done" => task_list.done,
1516
"inbox" => task_list.inbox,
1617
"name" => task_list.name,
1718
"order" => 1000,
19+
"pull-requests" => task_list.pull_requests,
1820
"inserted-at" => task_list.inserted_at,
1921
"updated-at" => task_list.updated_at,
2022
},

0 commit comments

Comments
 (0)