Skip to content

Commit

Permalink
Make it work with normal responses too...
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Nov 14, 2016
1 parent 7cf0520 commit aac8cf2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/ja_resource/create.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ defmodule JaResource.Create do
def respond(%Plug.Conn{} = conn, _old_conn), do: conn
def respond({:error, _name, errors, _changes}, conn), do: invalid(conn, errors)
def respond({:error, errors}, conn), do: invalid(conn, errors)
def respond({:ok, %{} = map}, conn), do: created(conn, Map.fetch(map, controller.atom()))
def respond({:ok, model}, conn), do: created(conn, model)
def respond(model, conn), do: created(conn, model)

Expand Down
1 change: 1 addition & 0 deletions lib/ja_resource/delete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ defmodule JaResource.Delete do
@doc false
def respond(nil, conn), do: not_found(conn)
def respond(%Plug.Conn{} = conn, _old_conn), do: conn
def respond({:ok, %{} = map}, conn), do: created(conn, Map.fetch(map, controller.atom()))
def respond({:ok, _model}, conn), do: deleted(conn)
def respond({:error, _name, errors, _changes}, conn), do: invalid(conn, errors)
def respond({:errors, errors}, conn), do: invalid(conn, errors)
Expand Down
9 changes: 9 additions & 0 deletions lib/ja_resource/model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ defmodule JaResource.Model do
@inferred_model JaResource.Model.model_from_controller(__MODULE__)
def model(), do: @inferred_model

def atom() do
model()
|> Atom.to_string
|> String.split(".")
|> List.last
|> String.downcase
|> String.to_atom
end

defoverridable [model: 0]
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/ja_resource/update.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ defmodule JaResource.Update do
def respond(nil, conn), do: send_resp(conn, :not_found, "")
def respond({:error, _name, errors, _changes}, conn), do: invalid(conn, errors)
def respond({:error, errors}, conn), do: invalid(conn, errors)
def respond({:ok, %{} = map}, conn), do: created(conn, Map.fetch(map, controller.atom()))
def respond({:ok, model}, conn), do: updated(conn, model)
def respond(model, conn), do: updated(conn, model)

Expand Down

0 comments on commit aac8cf2

Please sign in to comment.