View Source ExTeal.Resource.Update behaviour (ExTeal v0.27.0)
Defines a behaviour for updating a resource and the function to execute it.
It relies on (and uses):
- ExTeal.Resource.Record
- ExTeal.Resource.Attributes
When used ExTeal.Resource.Update defines the update/2
action suitable for
handling json-api requests.
To customize the behaviour of the update action the following callbacks can be implemented:
- ExTeal.Resource.Record.record/2
- ExTeal.Resource.Records.records/1
- handle_update/3
- handle_invalid_update/2
- render_update/2
- ExTeal.Resource.Attributes.permitted_attributes/3
Summary
Callbacks
Returns a Plug.Conn
in response to errors during update.
Returns an unpersisted changeset or persisted model representing the newly updated model.
Returns a Plug.Conn
in response to successful update.
Functions
Execute the update action on a given module implementing Update behaviour and conn.
Callbacks
@callback handle_invalid_update(Plug.Conn.t(), Ecto.Changeset.t()) :: Plug.Conn.t()
Returns a Plug.Conn
in response to errors during update.
Default implementation sets the status to :unprocessable_entity
and renders
the error messages provided.
@callback handle_update( Plug.Conn.t(), ExTeal.Resource.record(), ExTeal.Resource.attributes() ) :: Plug.Conn.t() | ExTeal.Resource.record() | nil
Returns an unpersisted changeset or persisted model representing the newly updated model.
Receives the conn, the model as found by record/2
, and the attributes
argument from the permitted_attributes
function.
Default implementation returns the results of calling
Model.changeset(model, attrs)
.
handle_update/3
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_update(conn, post, attributes) do
current_user_id = conn.assigns[:current_user].id
case post.author_id do
^current_user_id -> {:error, author_id: "you can only edit your own posts"}
_ -> Post.changeset(post, attributes, :update)
end
end
@callback render_update(Plug.Conn.t(), ExTeal.Resource.record()) :: Plug.Conn.t()
Returns a Plug.Conn
in response to successful update.
Default implementation renders the view.
Functions
Execute the update action on a given module implementing Update behaviour and conn.