ja_resource v0.2.0 JaResource.Update behaviour
Defines a behaviour for updating a resource and the function to execute it.
It relies on (and uses):
- JaResource.Record
- JaResource.Attributes
When used JaResource.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:
- JaResource.Record.record/2
- JaResource.Records.records/1
- handle_update/3
- JaResource.Attributes.permitted_attributes/3
Summary
Functions
Execute the update action on a given module implementing Update behaviour and conn
Callbacks
Returns an unpersisted changeset or persisted model representing the newly updated model
Functions
Callbacks
Specs
handle_update(Plug.Conn.t, JaResource.record, JaResource.attributes) ::
Plug.Conn.t |
JaResource.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