View Source ExTeal.Resource.Create behaviour (ExTeal v0.27.0)
Defines a behaviour for creating a resource and the function to execute it.
It relies on (and uses):
- ExTeal.Resource.Repo
- ExTeal.Resource.Model
- ExTeal.Resource.Attributes
When used ExTeal.Resource.Create defines the following overrideable callbacks:
- handle_create/2
- handle_invalid_create/2
- render_create/2
- ExTeal.Resource.Attributes.permitted_attributes/3
- ExTeal.Resource.Repo.repo/1
Summary
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
Creates a resource given a module using Create and a connection.
Callbacks
@callback handle_create(Plug.Conn.t(), ExTeal.Resource.attributes()) :: Plug.Conn.t() | Ecto.Changeset.t() | ExTeal.Resource.record() | {:ok, ExTeal.Resource.record()} | {:error, ExTeal.Resource.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
ExTeal.Resource.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.
ou
Example custom implementation:
def handle_create(_conn, attributes) do
Post.changeset(%Post{}, attributes, :create_and_publish)
end
@callback 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.
@callback render_create(Plug.Conn.t(), ExTeal.Resource.record()) :: Plug.Conn.t()
Returns a Plug.Conn
in response to successful create.
Default implementation sets the status to :created
and renders the view.
Functions
Creates a resource given a module using Create and a connection.
Create.call(ArticleResource, conn)