Skip to content

Commit f1444dc

Browse files
committed
included with project and task
1 parent 12f6808 commit f1444dc

File tree

6 files changed

+130
-24
lines changed

6 files changed

+130
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule CodeCorpsWeb.ProjectJsonapiView do
2+
@moduledoc false
3+
alias CodeCorps.StripeService.Validators.ProjectCanEnableDonations
4+
alias CodeCorps.Presenters.ImagePresenter
5+
6+
use JSONAPI.View, type: "project"
7+
use CodeCorpsWeb, :view
8+
9+
def fields, do: [
10+
:approval_requested,
11+
:approved,
12+
:can_activate_donations,
13+
:cloudinary_public_id,
14+
:description,
15+
:donations_active,
16+
:icon_thumb_url,
17+
:icon_large_url,
18+
:inserted_at,
19+
:long_description_body,
20+
:long_description_markdown,
21+
:should_link_externally,
22+
:slug,
23+
:title,
24+
:total_monthly_donated,
25+
:updated_at,
26+
:website
27+
]
28+
29+
def can_activate_donations(project, _conn) do
30+
case ProjectCanEnableDonations.validate(project) do
31+
{:ok, _} -> true
32+
{:error, _} -> false
33+
end
34+
end
35+
36+
def donations_active(project, _conn) do
37+
Enum.any?(project.donation_goals) && project.stripe_connect_plan != nil
38+
end
39+
40+
def icon_large_url(project, _conn), do: ImagePresenter.large(project)
41+
42+
def icon_thumb_url(project, _conn), do: ImagePresenter.thumbnail(project)
43+
end

lib/code_corps_web/views/project_view.ex

-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ defmodule CodeCorpsWeb.ProjectView do
3636
has_many :project_skills, serializer: CodeCorpsWeb.ProjectSkillView, identifiers: :always
3737
has_many :project_users, serializer: CodeCorpsWeb.ProjectUserView, identifiers: :always
3838
has_many :skills, serializer: CodeCorpsWeb.SkillView, identifiers: :always
39-
has_many :tasks, serializer: CodeCorpsWeb.TaskView, identifiers: :always
40-
has_many :task_lists, serializer: CodeCorpsWeb.TaskListView, identifiers: :always
4139

4240
def can_activate_donations(project, _conn) do
4341
case ProjectCanEnableDonations.validate(project) do
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule CodeCorpsWeb.TaskJsonapiView do
2+
@moduledoc false
3+
use CodeCorpsWeb, :view
4+
use JSONAPI.View, type: "task"
5+
6+
def fields, do: [
7+
:archived, :body, :created_at, :created_from, :has_github_pull_request,
8+
:inserted_at, :markdown, :modified_at, :modified_from, :number, :order,
9+
:overall_status, :status, :title, :updated_at
10+
]
11+
12+
# def relationships do
13+
# [github_issue]
14+
# end
15+
16+
# has_one :github_issue, type: "github-issue", field: :github_issue_id
17+
# has_one :github_pull_request, serializer: CodeCorpsWeb.GithubPullRequestView, identifiers: :always
18+
# has_one :github_repo, type: "github-repo", field: :github_repo_id
19+
# has_one :project, type: "project", field: :project_id
20+
# has_one :task_list, type: "task-list", field: :task_list_id
21+
# has_one :user, type: "user", field: :user_id
22+
# has_one :user_task, serializer: CodeCorpsWeb.UserTaskView, identifiers: :always
23+
24+
# has_many :comments, serializer: CodeCorpsWeb.CommentView, identifiers: :always
25+
# has_many :task_skills, serializer: CodeCorpsWeb.TaskSkillView, identifiers: :always
26+
27+
def has_github_pull_request(%{
28+
github_pull_request: %CodeCorps.GithubPullRequest{}
29+
}), do: true
30+
def has_github_pull_request(%{github_pull_request: nil}), do: false
31+
32+
def overall_status(%{
33+
github_pull_request: %CodeCorps.GithubPullRequest{merged: merged, state: state}
34+
}, _conn) do
35+
case merged do
36+
true -> "merged"
37+
false -> state
38+
end
39+
end
40+
def overall_status(%{github_pull_request: nil, status: status}, _conn) do
41+
status
42+
end
43+
end

lib/code_corps_web/views/task_list_view.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ defmodule CodeCorpsWeb.TaskListView do
33
use CodeCorpsWeb, :view
44
use JSONAPI.View, type: "task-list"
55

6-
alias CodeCorpsWeb.{ProjectView, TaskView}
6+
alias CodeCorpsWeb.{ProjectJsonapiView, TaskJsonapiView}
77

88
def fields, do: [:done, :inbox, :name, :order, :pull_requests, :inserted_at, :updated_at]
99

1010
def relationships do
11-
[project: ProjectView, tasks: TaskView]
11+
[project: {ProjectJsonapiView, :include}, tasks: {TaskJsonapiView, :include}]
1212
end
1313
end

test/lib/code_corps_web/views/project_view_test.exs

-18
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ defmodule CodeCorpsWeb.ProjectViewTest do
1212
project_skill = insert(:project_skill, project: project, skill: skill)
1313
project_user = insert(:project_user, project: project)
1414
stripe_connect_plan = insert(:stripe_connect_plan, project: project)
15-
task_list = insert(:task_list, project: project)
16-
task = insert(:task, task_list: task_list, project: project)
1715

1816
host = Application.get_env(:code_corps, :asset_host)
1917

@@ -102,22 +100,6 @@ defmodule CodeCorpsWeb.ProjectViewTest do
102100
"id" => stripe_connect_plan.id |> Integer.to_string,
103101
"type" => "stripe-connect-plan"
104102
}
105-
},
106-
"task-lists" => %{
107-
"data" => [
108-
%{
109-
"id" => task_list.id |> Integer.to_string,
110-
"type" => "task-list"
111-
}
112-
]
113-
},
114-
"tasks" => %{
115-
"data" => [
116-
%{
117-
"id" => task.id |> Integer.to_string,
118-
"type" => "task"
119-
}
120-
]
121103
}
122104
},
123105
"type" => "project",

test/lib/code_corps_web/views/task_list_view_test.exs

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
defmodule CodeCorpsWeb.TaskListViewTest do
22
use CodeCorpsWeb.ViewCase
33

4-
@tag :wip
54
test "renders all attributes and relationships properly" do
65
project = insert(:project)
76
task_list = insert(:task_list, order: 1000, project: project)
@@ -38,7 +37,48 @@ defmodule CodeCorpsWeb.TaskListViewTest do
3837
},
3938
type: "task-list",
4039
},
41-
:included => [],
40+
:included => [
41+
%{
42+
attributes: %{
43+
"approval-requested" => false,
44+
"approved" => project.approved,
45+
"cloudinary-public-id" => nil,
46+
"description" => project.description,
47+
"inserted-at" => project.inserted_at,
48+
"long-description-body" => project.long_description_body,
49+
"long-description-markdown" => project.long_description_markdown,
50+
"should-link-externally" => false,
51+
"slug" => project.slug,
52+
"title" => project.title,
53+
"total-monthly-donated" => 0,
54+
"updated-at" => project.updated_at,
55+
"website" => project.website
56+
},
57+
id: project.id |> Integer.to_string,
58+
relationships: %{},
59+
type: "project"
60+
},
61+
%{
62+
attributes: %{
63+
"archived" => false,
64+
"body" => nil,
65+
"created-at" => task.created_at,
66+
"created-from" => task.created_from,
67+
"inserted-at" => task.inserted_at,
68+
"markdown" => "A test task",
69+
"modified-at" => task.modified_at,
70+
"modified-from" => task.modified_from,
71+
"number" => task.number,
72+
"order" => task.order,
73+
"status" => task.status,
74+
"title" => task.title,
75+
"updated-at" => task.updated_at
76+
},
77+
id: task.id |> Integer.to_string,
78+
relationships: %{},
79+
type: "task"
80+
}
81+
],
4282
:links => %{}
4383
}
4484

0 commit comments

Comments
 (0)