ja_resource v0.2.0 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
  • 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

Functions

call(controller, conn)

Creates a resource given a module using Create and a connection.

Create.call(ArticleController, conn)

Dispatched by JaResource.Plug when phoenix action is create.

Callbacks

handle_create(arg0, arg1)

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