ja_resource v0.3.1 JaResource.Create behaviour
Defines a behaviour for creating a resource and the function to execute it.
It relies on (and uses):
- JaResource.Repo
- JaResource.Model
- JaResource.Attributes
When used JaResource.Create defines the following overrideable callbacks:
- handle_create/2
- handle_invalid_create/2
- render_create/2
- JaResource.Attributes.permitted_attributes/3
- JaResource.Repo.repo/1
Summary
Functions
Creates a resource given a module using Create and a connection
Callbacks
Returns an unpersisted changeset or persisted model of the newly created object
Returns a Plug.Conn
in response to errors during create
Returns a Plug.Conn
in response to successful create
Functions
Callbacks
Specs
handle_create(Plug.Conn.t, JaResource.attributes) ::
Plug.Conn.t |
Ecto.Changeset.t |
JaResource.record |
{:ok, JaResource.record} |
{:error, JaResource.validation_errors}
Returns an unpersisted changeset or persisted model of the newly created object.
Default implementation returns the results of calling
Model.changeset(%Model{}, attrs)
where Model is the model defined by the
JaResource.Model.model/0
callback.
The attributes argument is the result of the permitted_attributes
function.
handle_create/2
can return an %Ecto.Changeset, an Ecto.Schema struct,
a list of errors ({:error, [email: "is not valid"]}
or a conn with
any response/body.
Example custom implementation:
def handle_create(_conn, attributes) do
Post.changeset(%Post{}, attributes, :create_and_publish)
end
Specs
handle_invalid_create(Plug.Conn.t, Ecto.Changeset.t) :: Plug.Conn.t
Returns a Plug.Conn
in response to errors during create.
Default implementation sets the status to :unprocessable_entity
and renders
the error messages provided.
Specs
render_create(Plug.Conn.t, JaResource.record) :: Plug.Conn.t
Returns a Plug.Conn
in response to successful create.
Default implementation sets the status to :created
and renders the view.