MyApp v0.1.1 Bubbles.Url.Schema View Source
Provides a set of functions for creating, updating and deleting schemas which have associated URLs.
For example, if we have a Foo.Article
schema and in our domain each article has
an associated URL, represented by the Foo.Url
schema, then Foo.Article
is
the schema this module is intended to work with. If a function expects to
receive a schema_module
parameter, it expects Foo.Article
to be passed to
it. If a function expects to receive a schema
parameter, it expects an
actual %Foo.Article{}
struct.
Bubbles.Url.Schema
is meant to be used where schemas are being updated. If
we’re working with Phoenix and our domain is organized into contexts, we’d
modify the context methods for creating, updating and deleting schemas. Also,
for brefity, we can wrap Bubbles.Url.Schema
functions in helper functions.
See the following example of a modified context:
alias Foo.{Article, Url, Repo}
def create_article(%{uri: uri} = attrs) do
with {:ok, %{schema: article, url: url}} <-
create_article_with_url(uri, fn url ->
attrs = Map.put(attrs, :url_id, url.id)
%Article{}
|> Article.changeset(attrs)
|> Repo.insert()
end) do
{:ok, Map.put(article, :url, url)}
else
error -> error
end
end
defp create_article_with_url(uri, schema_create_fn) do
Schema.create_with_url(uri, Repo, Url, schema_create_fn)
end
Link to this section Summary
Functions
Creates a schema record including an associated Url record
Creates an Ecto.Multi
struct for creating a schema record including an
associated Url record
Deleted a schema record including deleting the associated Url record(s)
Creates an Ecto.Multi
struct for deleting a schema record including deleting
the associated Url record
Fetches a struct of schema_module
type associated with the URL that has the
matching uri
attribute
Updates a schema record including updating the associated Url record if provided uri has changed
Creates an Ecto.Multi
struct for updating a schema record including updating
an associated Url record
Link to this section Functions
Creates a schema record including an associated Url record.
The function expects a schema_create_fn
function which receives a single
parameter, the Url
struct, and is expected to return a struct of created
schema record.
For example, if we have a Foo.Article and Foo.Url structs and each article has a URL, we’d call the function as:
Bubbles.Url.Schema.create_with_url(uri, Foo.Repo, Foo.Url, fn url ->
attrs = Map.put(attrs, :url_id, url.id)
%Foo.Article{}
|> Foo.Article.changeset(attrs)
|> Foo.Repo.insert()
end)
Creates an Ecto.Multi
struct for creating a schema record including an
associated Url record.
This function is used in create_with_url/5
and immediately executed in a
transaction. On its own, it can be easily used as part of a larger
Ecto.Multi
operations pipeline.
Deleted a schema record including deleting the associated Url record(s).
The function expects a schema_delete_fn
function which is expected to delete
the schema record.
For example, if we have a Foo.Article and Foo.Url structs and each article has a URL, we’d call the function as:
Bubbles.Url.Schema.delete_with_url(article, Foo.Repo, Foo.Url, fn ->
Foo.Repo.delete(article)
end)
Creates an Ecto.Multi
struct for deleting a schema record including deleting
the associated Url record.
This function is used in delete_with_url/4
and immediately executed in a
transaction. On its own, it can be easily used as part of a larger
Ecto.Multi
operations pipeline.
Fetches a struct of schema_module
type associated with the URL that has the
matching uri
attribute.
For brevity, this function can be wrapped in a function with a shorter signature, for example:
get_article_by_uri!(uri) do
Bubbles.Url.Schema.get_by_uri!(uri, Foo.Repo, Foo.Url, Foo.Article)
end
Updates a schema record including updating the associated Url record if provided uri has changed.
The function expects a schema_update_fn
function which receives a single
parameter, the Url
struct, and is expected to return a struct of updated
schema record.
For example, if we have a Foo.Article and Foo.Url structs and each article has a URL, we’d call the function as:
Bubbles.Url.Schema.update_with_url(uri, Foo.Repo, Foo.Url, fn url ->
attrs = Map.put(attrs, :url_id, url.id)
%Foo.Article{}
|> Foo.Article.changeset(attrs)
|> Foo.Repo.update()
end)
Creates an Ecto.Multi
struct for updating a schema record including updating
an associated Url record.
This function is used in update_with_url/5
and immediately executed in a
transaction. On its own, it can be easily used as part of a larger
Ecto.Multi
operations pipeline.