Skip to content

Commit

Permalink
Save point
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsmith committed Sep 29, 2017
1 parent 2d51ca0 commit 08ce09f
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 110 deletions.
4 changes: 4 additions & 0 deletions lib/code_corps/github/adapters/task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ defmodule CodeCorps.GitHub.Adapters.Task do
}

@mapping [
{:created_at, ["created_at"]},
{:github_issue_number, ["number"]},
{:markdown, ["body"]},
{:modified_at, ["updated_at"]},
{:status, ["state"]},
{:title, ["title"]}
]
Expand All @@ -26,6 +28,8 @@ defmodule CodeCorps.GitHub.Adapters.Task do
task
|> Map.from_struct
|> MapTransformer.transform_inverse(@mapping)
|> Map.delete("created_at")
|> Map.delete("number")
|> Map.delete("updated_at")
end
end
6 changes: 4 additions & 2 deletions lib/code_corps/github/event/issues/changeset_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ defmodule CodeCorps.GitHub.Event.Issues.ChangesetBuilder do
TaskList |> Repo.get_by(project_id: project_id, inbox: true)

task
|> Changeset.change(TaskAdapter.from_issue(issue_attrs))
|> Changeset.cast(TaskAdapter.from_issue(issue_attrs), [:created_at, :github_issue_number, :markdown, :modified_at, :status, :title])
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
|> Changeset.put_change(:created_on, "github")
|> Changeset.put_change(:github_repo_id, github_repo_id)
|> Changeset.put_change(:project_id, project_id)
|> Changeset.put_change(:task_list_id, task_list_id)
Expand All @@ -56,8 +57,9 @@ defmodule CodeCorps.GitHub.Event.Issues.ChangesetBuilder do

defp update_changeset(%Task{} = task, issue_attrs) do
task
|> Changeset.change(issue_attrs |> TaskAdapter.from_issue())
|> Changeset.cast(TaskAdapter.from_issue(issue_attrs), [:github_issue_number, :markdown, :modified_at, :status, :title])
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
|> Changeset.put_change(:updated_on, "github")
|> Changeset.validate_required([:project_id, :title, :user_id])
|> Changeset.assoc_constraint(:github_repo)
|> Changeset.assoc_constraint(:project)
Expand Down
23 changes: 20 additions & 3 deletions lib/code_corps/model/task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ defmodule CodeCorps.Task do
@type t :: %__MODULE__{}

schema "tasks" do
field :closed_at, :utc_datetime
field :body, :string
field :closed_at, :utc_datetime
field :created_at, :utc_datetime
field :created_from, :string, default: "code_corps"
field :markdown, :string
field :modified_at, :utc_datetime
field :modified_from, :string, default: "code_corps"
field :number, :integer, read_after_writes: true
field :order, :integer
field :status, :string, default: "open"
Expand Down Expand Up @@ -48,6 +52,7 @@ defmodule CodeCorps.Task do
struct
|> changeset(params)
|> cast(params, [:project_id, :user_id, :github_repo_id])
|> set_created_and_modified_at()
|> validate_required([:project_id, :user_id])
|> assoc_constraint(:project)
|> assoc_constraint(:user)
Expand All @@ -61,6 +66,7 @@ defmodule CodeCorps.Task do
|> cast(params, [:status])
|> validate_inclusion(:status, statuses())
|> set_closed_at()
|> update_modified_at()
end

def apply_position(changeset) do
Expand All @@ -79,10 +85,21 @@ defmodule CodeCorps.Task do
case changeset do
%Changeset{valid?: true, changes: %{status: "closed"}} ->
put_change(changeset, :closed_at, DateTime.utc_now)
%Changeset{valid?: true, changes: %{status: "open"}} ->
%Changeset{valid?: true, changes: %{status: "open"}} ->
put_change(changeset, :closed_at, nil)
_ ->
changeset
end
end
end

defp set_created_and_modified_at(changeset) do
now = DateTime.utc_now
changeset
|> put_change(:created_at, now)
|> put_change(:modified_at, now)
end

defp update_modified_at(changeset) do
put_change(changeset, :modified_at, DateTime.utc_now)
end
end
5 changes: 4 additions & 1 deletion lib/code_corps_web/views/task_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ defmodule CodeCorpsWeb.TaskView do
use CodeCorpsWeb, :view
use JaSerializer.PhoenixView

attributes ~w(body markdown number status title order inserted_at updated_at)a
attributes ~w(
body created_at inserted_at markdown modified_at number order status title
updated_at
)a

has_one :github_repo, serializer: CodeCorpsWeb.GithubRepoView
has_one :project, serializer: CodeCorpsWeb.ProjectView
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule CodeCorps.Repo.Migrations.AddCreatedAtAndModifiedAtToTasks do
use Ecto.Migration

def change do
alter table(:tasks) do
add :created_at, :utc_datetime
add :modified_at, :utc_datetime
add :created_from, :string, default: "code_corps"
add :modified_from, :string, default: "code_corps"
end
end
end
Loading

0 comments on commit 08ce09f

Please sign in to comment.