Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include tasks in view #1302

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/code_corps_web/controllers/task_list_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule CodeCorpsWeb.TaskListController do
end
end

@preloads [:tasks]
@preloads [tasks: [:github_pull_request, :github_issue, :github_repo, :user_task, :user]]

def preload(data) do
Repo.preload(data, @preloads)
Expand Down
2 changes: 1 addition & 1 deletion lib/code_corps_web/views/task_list_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ defmodule CodeCorpsWeb.TaskListView do

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

has_many :tasks, serializer: CodeCorpsWeb.TaskView, identifiers: :always
has_many :tasks, serializer: CodeCorpsWeb.TaskIncludedView, identifiers: :always, include: true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe better to call this TaskSlimView.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not a bad idea. Ideally, I would love if we could just specify what to include and where.

Possibly even the Ember client would do it, so we don't have to worry about this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

kind of like graphql? I would love that. I worked on a project and eventually there was 5 different views for the same resource, each giving you a different subset of data. It was slightly hard to manage.

We implemented the request on the ember side, but eventually the logic got complex and the various views were easier to manage.

end
24 changes: 24 additions & 0 deletions lib/code_corps_web/views/task_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,27 @@ defmodule CodeCorpsWeb.TaskView do
status
end
end

defmodule CodeCorpsWeb.TaskIncludedView do
@moduledoc false
use CodeCorpsWeb, :view
use JaSerializer.PhoenixView

def type, do: "task"

attributes [
:archived, :body, :created_at, :created_from, :has_github_pull_request, :inserted_at,
:markdown, :modified_at, :modified_from, :number, :order, :status, :title, :updated_at
]

has_one :github_issue, type: "github-issue", field: :github_issue_id, include: true
has_one :github_pull_request, serializer: CodeCorpsWeb.GithubPullRequestView, include: true
has_one :github_repo, type: "github-repo", field: :github_repo_id
has_one :user, serializer: CodeCorpsWeb.UserIncludedView, include: true
has_one :user_task, serializer: CodeCorpsWeb.UserTaskView, include: true

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This task now includes all relevant relationships I believe that are needed for the task index route.

def has_github_pull_request(%{
github_pull_request: %CodeCorps.GithubPullRequest{}
}), do: true
def has_github_pull_request(%{github_pull_request: nil}), do: false
end
45 changes: 45 additions & 0 deletions lib/code_corps_web/views/user_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,48 @@ defmodule CodeCorpsWeb.UserView do
defp normalize_name(name) when name in ["", nil], do: nil
defp normalize_name(name), do: name
end

defmodule CodeCorpsWeb.UserIncludedView do
@moduledoc false
alias CodeCorps.Presenters.ImagePresenter

use CodeCorpsWeb, :view
use JaSerializer.PhoenixView

def type, do: "user"

attributes [
:admin, :biography, :cloudinary_public_id, :email, :first_name,
:github_avatar_url, :github_id, :github_username,
:inserted_at, :last_name, :name, :photo_large_url, :photo_thumb_url,
:sign_up_context, :state, :state_transition, :twitter, :username,
:website, :updated_at
]

def photo_large_url(user, _conn), do: ImagePresenter.large(user)

def photo_thumb_url(user, _conn), do: ImagePresenter.thumbnail(user)

@doc """
Returns the user email or an empty string, depending on the user
being rendered is the authenticated user, or some other user.

Users can only see their own emails. Everyone else's are private.
"""
def email(user, %Plug.Conn{assigns: %{current_user: current_user}}) do
if user.id == current_user.id, do: user.email, else: ""
end
def email(_user, _conn), do: ""

@doc """
Returns the user's full name when both first and last name are present.
Returns the only user's first name or last name when the other is missing,
otherwise returns nil.
"""
def name(%{first_name: first_name, last_name: last_name}, _conn) do
"#{first_name} #{last_name}" |> String.trim |> normalize_name
end

defp normalize_name(name) when name in ["", nil], do: nil
defp normalize_name(name), do: name
end
4 changes: 2 additions & 2 deletions priv/repo/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
-- PostgreSQL database dump
--

-- Dumped from database version 9.5.10
-- Dumped by pg_dump version 10.1
-- Dumped from database version 9.6.6
-- Dumped by pg_dump version 9.6.6

SET statement_timeout = 0;
SET lock_timeout = 0;
Expand Down
72 changes: 70 additions & 2 deletions test/lib/code_corps_web/views/task_list_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ defmodule CodeCorpsWeb.TaskListViewTest do
use CodeCorpsWeb.ViewCase

test "renders all attributes and relationships properly" do
user = insert(:user, first_name: "First", last_name: "Last", default_color: "blue")
host = Application.get_env(:code_corps, :asset_host)
project = insert(:project)
task_list = insert(:task_list, order: 1000, project: project)
task = insert(:task, order: 1000, task_list: task_list)
task = insert(:task, order: 1000, task_list: task_list, user: user)

task_list = CodeCorpsWeb.TaskListController.preload(task_list)
rendered_json = render(CodeCorpsWeb.TaskListView, "show.json-api", data: task_list)
Expand Down Expand Up @@ -39,7 +41,73 @@ defmodule CodeCorpsWeb.TaskListViewTest do
},
"jsonapi" => %{
"version" => "1.0"
}
},
"included" => [%{
"attributes" => %{
"archived" => task.archived,
"body" => task.body,
"created-at" => task.created_at,
"created-from" => task.created_from,
"has-github-pull-request" => false,
"inserted-at" => task.inserted_at,
"markdown" => task.markdown,
"modified-at" => task.modified_at,
"modified-from" => task.modified_from,
"number" => task.number,
"order" => task.order,
"status" => task.status,
"title" => task.title,
"updated-at" => task.updated_at
},
"relationships" => %{
"github-issue" => %{
"data" => nil
},
"github-repo" => %{
"data" => nil
},
"user-task" => %{
"data" => nil
},
"user" => %{
"data" => %{
"id" => task.user.id |> Integer.to_string,
"type" => "user"
}
},
"github-pull-request" => %{
"data" => nil
},
},
"id" => task.id |> Integer.to_string,
"type" => "task"
},
%{
"attributes" => %{
"admin" => false,
"biography" => user.biography,
"cloudinary-public-id" => nil,
"email" => "",
"first-name" => user.first_name,
"github-avatar-url" => nil,
"github-id" => nil,
"github-username" => nil,
"inserted-at" => user.inserted_at,
"last-name" => user.last_name,
"name" => "First Last",
"photo-large-url" => "#{host}/icons/user_default_large_blue.png",
"photo-thumb-url" => "#{host}/icons/user_default_thumb_blue.png",
"sign-up-context" => "default",
"state" => "signed_up",
"state-transition" => nil,
"twitter" => user.twitter,
"username" => user.username,
"updated-at" => user.updated_at,
"website" => user.website
},
"id" => user.id |> Integer.to_string,
"type" => "user"
}]
}

assert rendered_json == expected_json
Expand Down