Skip to content

Commit

Permalink
Fix: Cannot return multi in handle create (#51)
Browse files Browse the repository at this point in the history
- Multis returned from handle_create/2 were being caught by the guard on line 129. Teal cannot preload the resource because it doesn’t know what key is associated with the resource update within the multi. A 500 error was being thrown when Teal attempted to preload the full map returned from the multi.
- Updates guard clauses so changeset models are caught on line 129 and successful multi responses are caught on 134 and passed to the application’s resource file’s render_create override
- Updates the function definitions with _cs and _multi to better describe the flow
  • Loading branch information
alexandreaco committed Feb 3, 2021
1 parent 1dee610 commit 7aaabf1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/ex_teal/resource/create.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ defmodule ExTeal.Resource.Create do
@doc false
def respond(%Plug.Conn{} = conn, _old_conn, _), do: conn

def respond({:error, errors}, conn, resource),
def respond({:error, errors} = _cs, conn, resource),
do: resource.handle_invalid_create(conn, errors)

def respond({:error, _name, errors, _changes}, conn, resource),
def respond({:error, _name, errors, _changes} = _multi, conn, resource),
do: resource.handle_invalid_create(conn, errors)

def respond({:ok, model}, conn, resource) do
def respond({:ok, %{id: _id} = model} = _cs, conn, resource) do
model = resource.repo().preload(model, resource.with())
resource.render_create(conn, model)
end

def respond(model, conn, resource), do: resource.render_create(conn, model)
def respond({:ok, model} = _multi, conn, resource), do: resource.render_create(conn, model)
end
2 changes: 2 additions & 0 deletions test/ex_teal/resource/create_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ defmodule ExTeal.Resource.CreateTest do

def handle_create(_, params) do
changeset = Post.changeset(%Post{}, params)
other_cs = Post.changeset(%Post{}, %{name: "Other"})

Multi.new()
|> Multi.insert(:post, changeset)
|> Multi.insert(:other_post, other_cs)
end

def render_create(conn, %{post: post}) do
Expand Down

0 comments on commit 7aaabf1

Please sign in to comment.