Skip to content

Commit

Permalink
updating since tag
Browse files Browse the repository at this point in the history
  • Loading branch information
yordis committed Mar 20, 2018
1 parent 0c8af24 commit e6c2711
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/straw_hat_mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule StrawHat.Mailer do
It use `StrawHat.Mailer.deliver/1` inside `Task.start/1`.
"""
@since "1.0.0"
@spec deliver_later(Swoosh.Email.t(), keyword) :: {:ok, pid}
def deliver_later(email, config \\ []) do
Task.start(fn -> deliver(email, config) end)
Expand Down
1 change: 1 addition & 0 deletions lib/straw_hat_mailer/emails.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ defmodule StrawHat.Mailer.Emails do

def with_template(email, template_name, data) do
add_template = fn template -> with_template(email, template, data) end

template_name
|> Templates.get_template_by_name()
|> StrawHat.Response.and_then(add_template)
Expand Down
9 changes: 9 additions & 0 deletions lib/straw_hat_mailer/partials/partials.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ defmodule StrawHat.Mailer.Partials do
@doc """
Returns the list of partials.
"""
@since "1.0.0"
@spec get_partials(Scrivener.Config.t()) :: Scrivener.Page.t()
def get_partials(pagination \\ []), do: Repo.paginate(Partial, pagination)

@doc """
Creates a partial.
"""
@since "1.0.0"
@spec create_partial(Partial.partial_attrs()) ::
{:ok, Partial.t()} | {:error, Ecto.Changeset.t()}
def create_partial(partial_attrs) do
Expand All @@ -29,6 +31,7 @@ defmodule StrawHat.Mailer.Partials do
@doc """
Updates a partial.
"""
@since "1.0.0"
@spec update_partial(Partial.t(), Partial.partial_attrs()) ::
{:ok, Partial.t()} | {:error, Ecto.Changeset.t()}
def update_partial(%Partial{} = partial, partial_attrs) do
Expand All @@ -40,12 +43,14 @@ defmodule StrawHat.Mailer.Partials do
@doc """
Destroys a partial.
"""
@since "1.0.0"
@spec destroy_partial(Partial.t()) :: {:ok, Partial.t()} | {:error, Ecto.Changeset.t()}
def destroy_partial(%Partial{} = partial), do: Repo.delete(partial)

@doc """
Get a partial by `id`.
"""
@since "1.0.0"
@spec find_partial(String.t()) :: {:ok, Partial.t()} | {:error, Error.t()}
def find_partial(partial_id) do
partial_id
Expand All @@ -58,12 +63,14 @@ defmodule StrawHat.Mailer.Partials do
@doc """
Get a partial by `id`.
"""
@since "1.0.0"
@spec get_partial(String.t()) :: Partial.t() | nil | no_return
def get_partial(partial_id), do: Repo.get(Partial, partial_id)

@doc """
Returns an `%Ecto.Changeset{}` for tracking partial changes.
"""
@since "1.0.0"
@spec change_partial(Partial.t()) :: Ecto.Changeset.t()
def change_partial(%Partial{} = partial) do
Partial.changeset(partial, %{})
Expand All @@ -72,13 +79,15 @@ defmodule StrawHat.Mailer.Partials do
@doc """
Returns a list of partials that belongs to the `owner_id`.
"""
@since "1.0.0"
@spec get_owner_partials(String.t(), Scrivener.Config.t()) :: Scrivener.Page.t()
def get_owner_partials(owner_id, pagination \\ []) do
owner_id
|> partials_by_owner()
|> Repo.paginate(pagination)
end

@since "1.0.0"
@spec partials_by_owner(String.t()) :: Ecto.Query.t()
defp partials_by_owner(owner_id) do
from(partial in Partial, where: partial.owner_id == ^owner_id)
Expand Down
5 changes: 5 additions & 0 deletions lib/straw_hat_mailer/templates/template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ defmodule StrawHat.Mailer.Template do
@doc """
Validate the attributes and return a Ecto.Changeset for the current Template.
"""
@since "1.0.0"
@spec changeset(t, template_attrs) :: Ecto.Changeset.t()
def changeset(template, template_attrs) do
template
Expand All @@ -86,13 +87,17 @@ defmodule StrawHat.Mailer.Template do
|> validate_name()
end

@since "1.0.0"
@spec validate_name(Ecto.Changeset.t()) :: Ecto.Changeset.t()
defp validate_name(changeset) do
changeset
|> update_change(:name, &cleanup_name/1)
|> validate_format(:name, @name_regex)
|> unique_constraint(:name, name: :templates_owner_id_name_index)
end

@since "1.0.0"
@spec cleanup_name(String.t()) :: String.t()
defp cleanup_name(name) do
name
|> String.trim()
Expand Down
6 changes: 6 additions & 0 deletions lib/straw_hat_mailer/templates/template_engine.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
defmodule StrawHat.Mailer.TemplateEngine do
@moduledoc false

alias StrawHat.Mailer.Template

@callback render(String.t(), data :: map()) :: String.t()

@since "1.0.0"
@spec render(%Template{}, any) :: String.t()
def render(template, data) do
template_engine().render(template, data)
end

@since "1.0.0"
@spec template_engine :: any
defp template_engine do
Application.get_env(
:straw_hat_mailer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule StrawHat.Mailer.TemplateEngine.BBMustache do

@behaviour StrawHat.Mailer.TemplateEngine

@since "1.0.0"
@spec render(String.t(), map()) :: String.t()
def render(template, data) do
:bbmustache.render(template, data, key_type: :atom)
Expand Down
1 change: 1 addition & 0 deletions lib/straw_hat_mailer/templates/template_partial.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ defmodule StrawHat.Mailer.TemplatePartial do
@doc """
Validate the attributes and return a Ecto.Changeset for the current Template Partial.
"""
@since "1.0.0"
@spec changeset(t, Template.t(), Partial.t(), map()) :: Ecto.Changeset.t()
def changeset(template_partial, template, partial, params \\ %{}) do
template_partial
Expand Down
13 changes: 13 additions & 0 deletions lib/straw_hat_mailer/templates/templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule StrawHat.Mailer.Templates do
@doc """
Returns the list of templates.
"""
@since "1.0.0"
@spec get_templates(Scrivener.Config.t()) :: Scrivener.Page.t()
def get_templates(pagination \\ []) do
templates_with_partials() |> Repo.paginate(pagination)
Expand All @@ -17,6 +18,7 @@ defmodule StrawHat.Mailer.Templates do
@doc """
Creates a template.
"""
@since "1.0.0"
@spec create_template(Template.template_attrs()) ::
{:ok, Template.t()} | {:error, Ecto.Changeset.t()}
def create_template(template_attrs) do
Expand All @@ -28,6 +30,7 @@ defmodule StrawHat.Mailer.Templates do
@doc """
Updates a template.
"""
@since "1.0.0"
@spec update_template(Template.t(), Template.template_attrs()) ::
{:ok, Template.t()} | {:error, Ecto.Changeset.t()}
def update_template(%Template{} = template, template_attrs) do
Expand All @@ -39,12 +42,14 @@ defmodule StrawHat.Mailer.Templates do
@doc """
Destroys a template.
"""
@since "1.0.0"
@spec destroy_template(Template.t()) :: {:ok, Template.t()} | {:error, Ecto.Changeset.t()}
def destroy_template(%Template{} = template), do: Repo.delete(template)

@doc """
Returns an `%Ecto.Changeset{}` for tracking template changes.
"""
@since "1.0.0"
@spec change_template(Template.t()) :: Ecto.Changeset.t()
def change_template(%Template{} = template) do
Template.changeset(template, %{})
Expand All @@ -53,6 +58,7 @@ defmodule StrawHat.Mailer.Templates do
@doc """
Get a template by `id`.
"""
@since "1.0.0"
@spec find_template(String.t()) :: {:ok, Template.t()} | {:error, Error.t()}
def find_template(template_id) do
template_id
Expand All @@ -65,6 +71,7 @@ defmodule StrawHat.Mailer.Templates do
@doc """
Get a template by `id`.
"""
@since "1.0.0"
@spec get_template(String.t()) :: Ecto.Schema.t() | nil | no_return
def get_template(template_id) do
Template
Expand All @@ -75,6 +82,7 @@ defmodule StrawHat.Mailer.Templates do
@doc """
Get a template by `name`.
"""
@since "1.0.0"
@spec get_template_by_name(String.t()) :: {:ok, Template.t()} | {:error, Error.t()}
def get_template_by_name(template_name) do
template_name
Expand All @@ -91,6 +99,7 @@ defmodule StrawHat.Mailer.Templates do
@doc """
Add partials to template.
"""
@since "1.0.0"
@spec add_partials(Template.t(), [Partial.t()]) :: [
{:ok, Template.t()} | {:error, Ecto.Changeset.t()}
]
Expand All @@ -103,6 +112,7 @@ defmodule StrawHat.Mailer.Templates do
@doc """
Add a partial to the template.
"""
@since "1.0.0"
@spec add_partial(Template.t(), Partial.t()) ::
{:ok, TemplatePartial.t()} | {:error, Ecto.Changeset.t()}
def add_partial(template, partial) do
Expand All @@ -114,6 +124,7 @@ defmodule StrawHat.Mailer.Templates do
@doc """
Remove a partial from the template.
"""
@since "1.0.0"
@spec remove_partial(Template.t(), Partial.t()) ::
{:ok, TemplatePartial.t()} | {:error, Ecto.Changeset.t() | Error.t()}
def remove_partial(%Template{id: template_id} = _template, %Partial{id: partial_id} = _partial) do
Expand All @@ -127,6 +138,7 @@ defmodule StrawHat.Mailer.Templates do
|> StrawHat.Response.map(&Repo.delete/1)
end

@since "1.0.0"
@spec templates_by_name(String.t()) :: Ecto.Query.t()
defp templates_by_name(name) do
from(
Expand All @@ -136,6 +148,7 @@ defmodule StrawHat.Mailer.Templates do
)
end

@since "1.0.0"
@spec templates_with_partials :: Ecto.Query.t()
defp templates_with_partials do
from(_template in Template, preload: [:partials])
Expand Down

0 comments on commit e6c2711

Please sign in to comment.