LoadResource v0.4.0 LoadResource.Plug View Source
This plug allows you to specify resources that your app should load and (optionally) validate as part of a request.
Examples
Load a Book resource using the id
param on the incoming request:
plug LoadResource.Plug, [model: Book, handler: &MyErrorHandler.not_found/1]
Use the book_id
param instead of id
(useful when composing multiple resources):
plug LoadResource.Plug, [model: Book, id_key: "book_id", handler: &MyErrorHandler.not_found/1]
Load a Quote that matches to a previously loaded Book:
plug LoadResource.Plug, [model: Quote, scopes: [:book], handler: &MyErrorHandler.not_found/1]
(See LoadResource.Scope
for more information on scopes.)
Accepted Options
model
: an Ecto model representing the resource you want to load (required)handler
: a function/1 that gets called if the record can’t be found andrequired: true
(required)id_key
: what param in the incoming request represents the ID of the record (optional, default: “id”)required
: whether to halt the plug pipeline and return an error response if the record can’t be found (optional, default: true)resource_name
: under what value to store the resource inconn.assigns
(optional, default: derived from the model)scopes
: an list of atoms and/orLoadResource.Scope
structs (optional, default: [])
Link to this section Summary
Functions
Load a resource for a given request based on the previously-provided options
Initialize the plug with any options provided in the controller or pipeline, including calculating the resource_name (which key will written to in conn.assigns
) from the model
Link to this section Functions
Load a resource for a given request based on the previously-provided options.
Initialize the plug with any options provided in the controller or pipeline, including calculating the resource_name (which key will written to in conn.assigns
) from the model.