beam-community/bamboo_phoenix

Shouldn't assigns have multiple signatures?

Opened this issue · 0 comments

Currently Bamboo.Phoenix only has the following assign/3 function and not the typical assign/2:

  def assign(%{assigns: assigns} = email, key, value) do
    %{email | assigns: Map.put(assigns, key, value)}
  end

This makes it a little inconvenient to assign multiple values. It forces you to have to either implement assign/2 yourself or do the following.

new_email()
|> assign(item_one: 1)
|> assign(item_two: 1)
|> assign(item_three: 1)

It definitely surprised me and I have to spend a little time figuring out why the following wasn't working:

new_email()
|> assign(item_one: 1, item_two: 2, item_three: 3)

Shouldn't we have the same functions available as Bamboo.Template?

  def assign(%{assigns: assigns} = email, attrs) when is_map(attrs) or is_list(attrs) do
    %{email | assigns: Enum.into(attrs, assigns)}
  end

  def assign(%{assigns: assigns} = email, key, value) do
    %{email | assigns: Map.put(assigns, key, value)}
  end

There is actually quite a bit of duplication of what's in Bamboo.Template, that could be cleaned up, along with adding assign/2 by doing the following in Bamboo.Phoenix:

import Bamboo.Email

I'm glad to do a pull request if you guys agree with that solution. We could also just re-implement assign/2, but I would prefer not to duplicate the code.

Let me know your thoughts, thanks!