Skip to content

Commit

Permalink
Merge pull request #376 from code-corps/use-read-after-writes-for-tas…
Browse files Browse the repository at this point in the history
…k-number

Get rid of manual task reloading by using :read_after_writes option
  • Loading branch information
begedin committed Oct 19, 2016
2 parents d7e2ef3 + e3cf4a3 commit da5867b
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 10 deletions.
3 changes: 0 additions & 3 deletions test/controllers/task_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ defmodule CodeCorps.TaskControllerTest do
path = conn |> task_path(:show, task)

data = conn |> get(path) |> json_response(200) |> Map.get("data")
task = Task |> Repo.get(task.id)

assert data["id"] == "#{task.id}"
assert data["type"] == "task"
Expand All @@ -129,7 +128,6 @@ defmodule CodeCorps.TaskControllerTest do

test "shows task by number for project", %{conn: conn} do
task = insert(:task)
task = Task |> Repo.get(task.id)

path = conn |> project_task_path(:show, task.project_id, task.number)
data = conn |> get(path) |> json_response(200) |> Map.get("data")
Expand Down Expand Up @@ -158,7 +156,6 @@ defmodule CodeCorps.TaskControllerTest do
json = conn |> post(path, payload) |> json_response(201)

assert json["data"]["id"]
assert Repo.get_by(Task, @valid_attrs)

# ensure record is reloaded from database before serialized, since number is added
# on database level upon insert
Expand Down
2 changes: 0 additions & 2 deletions test/models/task_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ defmodule CodeCorps.TaskTest do
})
changeset = Task.create_changeset(%Task{}, changes)
{:ok, result} = Repo.insert(changeset)
result = Repo.get(Task, result.id)
assert result.number == 3

changes = Map.merge(@valid_attrs, %{
Expand All @@ -77,7 +76,6 @@ defmodule CodeCorps.TaskTest do
})
changeset = Task.create_changeset(%Task{}, changes)
{:ok, result} = Repo.insert(changeset)
result = Repo.get(Task, result.id)
assert result.number == 2
end

Expand Down
4 changes: 0 additions & 4 deletions web/controllers/task_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,9 @@ defmodule CodeCorps.TaskController do
%Task{}
|> Task.create_changeset(attributes)
|> Repo.insert
|> reload_task # need to reload to get generated number
|> CodeCorps.Analytics.Segment.track(:created, conn)
end

defp reload_task({:ok, new_task}), do: {:ok, Repo.get(Task, new_task.id)}
defp reload_task({:error, changeset}), do: {:error, changeset}

def handle_update(conn, task, attributes) do
task
|> Task.update_changeset(attributes)
Expand Down
2 changes: 1 addition & 1 deletion web/models/task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule CodeCorps.Task do
schema "tasks" do
field :body, :string
field :markdown, :string
field :number, :integer
field :number, :integer, read_after_writes: true
field :task_type, :string
field :state, :string
field :status, :string, default: "open"
Expand Down

0 comments on commit da5867b

Please sign in to comment.