Skip to content

Commit

Permalink
Build mailer config during runtime (#170)
Browse files Browse the repository at this point in the history
* Build mailer config during runtime

* Add build_config
  • Loading branch information
smdern authored and paulcsmith committed Jun 13, 2016
1 parent 0b3be9e commit 584ec63
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions lib/bamboo/mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,23 @@ defmodule Bamboo.Mailer do

defmacro __using__(opts) do
quote bind_quoted: [opts: opts] do
config = Bamboo.Mailer.parse_opts(__MODULE__, opts)

@adapter config.adapter
@config config

@spec deliver_now(Bamboo.Email.t) :: Bamboo.Email.t
def deliver_now(email) do
Bamboo.Mailer.deliver_now(@adapter, email, @config)
config = build_config
Bamboo.Mailer.deliver_now(config.adapter, email, config)
end

@spec deliver_later(Bamboo.Email.t) :: Bamboo.Email.t
def deliver_later(email) do
Bamboo.Mailer.deliver_later(@adapter, email, @config)
config = build_config
Bamboo.Mailer.deliver_later(config.adapter, email, config)
end

otp_app = Keyword.fetch!(opts, :otp_app)

defp build_config, do: Bamboo.Mailer.build_config(__MODULE__, unquote(otp_app))

def deliver(_email) do
raise """
you called deliver/1, but it has been renamed to deliver_now/1 to add clarity.
Expand Down Expand Up @@ -185,10 +187,21 @@ defmodule Bamboo.Mailer do

@doc false
def parse_opts(mailer, opts) do
Logger.warn("#{__MODULE__}.parse_opts/2 has been deprecated. Use #{__MODULE__}.build_config/2")

otp_app = Keyword.fetch!(opts, :otp_app)
config = Application.fetch_env!(otp_app, mailer) |> Enum.into(%{})
build_config(mailer, otp_app)
end

def build_config(mailer, otp_app) do
otp_app
|> Application.fetch_env!(mailer)
|> Map.new
|> handle_adapter_config
end

config.adapter.handle_config(config)
defp handle_adapter_config(base_config = %{adapter: adapter}) do
adapter.handle_config(base_config)
|> Map.put_new(:deliver_later_strategy, Bamboo.TaskSupervisorStrategy)
end
end

0 comments on commit 584ec63

Please sign in to comment.