Skip to content

Commit

Permalink
Add get_name function to the BaseEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
zacck authored and joshsmith committed Dec 21, 2017
1 parent 15d84db commit ffc2a1b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion emails/receipt.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<img src="{{high_five_image_url}}" width="100" height="100" />
</p>
<p class="donation_text center">
<strong>{{user_first_name}}</strong>, thanks for your monthly donation to <a href="{{project_url}}">{{project_title}}</a>.
Hey {{name}}, thanks for your monthly donation to <a href="{{project_url}}">{{project_title}}</a>.
</p>
<tr>
<td class="donation_goal">
Expand Down
5 changes: 5 additions & 0 deletions lib/code_corps/emails/base_email.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
defmodule CodeCorps.Emails.BaseEmail do
import Bamboo.Email, only: [from: 2, new_email: 0]
alias CodeCorps.User

@spec create :: Bamboo.Email.t
def create do
new_email()
|> from("Code Corps<team@codecorps.org>")
end

@spec get_name(User.t) :: String.t
def get_name(%User{first_name: nil}), do: "there"
def get_name(%User{first_name: name}), do: name
end
6 changes: 3 additions & 3 deletions lib/code_corps/emails/receipt_email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule CodeCorps.Emails.ReceiptEmail do
alias CodeCorps.Emails.BaseEmail
alias CodeCorps.{DonationGoal, Project, Repo, StripeConnectCharge, StripeConnectSubscription, WebClient}

@spec create(StripeConnectCharge.t, Stripe.Invoice.t) :: Bamboo.Email.t
@spec create(StripeConnectCharge.t, Stripe.Invoice.t) :: Bamboo.Email.t
def create(%StripeConnectCharge{} = charge, %Stripe.Invoice{} = invoice) do
with %StripeConnectCharge{} = charge <- Repo.preload(charge, :user),
%Project{} = project <- get_project(invoice.subscription),
Expand Down Expand Up @@ -51,11 +51,11 @@ defmodule CodeCorps.Emails.ReceiptEmail do
charge_amount: charge.amount |> format_amount(),
charge_statement_descriptor: charge.statement_descriptor,
high_five_image_url: high_five_image_url(),
name: BaseEmail.get_name(charge.user),
project_current_donation_goal_description: current_donation_goal.description,
project_title: project.title,
project_url: project |> url(),
subject: project |> build_subject_line(),
user_first_name: charge.user.first_name
subject: project |> build_subject_line()
}
end

Expand Down
17 changes: 17 additions & 0 deletions test/lib/code_corps/emails/base_email_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule CodeCorps.Emails.BaseEmailTest do
use CodeCorps.ModelCase
use Bamboo.Test
alias CodeCorps.Emails.BaseEmail

describe "get_name/1" do
test "get_name returns there on nil name" do
user = %CodeCorps.User{}
assert BaseEmail.get_name(user) == "there"
end

test "get_name returns first_name of user" do
user = %CodeCorps.User{first_name: "Zacck"}
assert BaseEmail.get_name(user) == "Zacck"
end
end
end
4 changes: 2 additions & 2 deletions test/lib/code_corps/emails/receipt_email_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ defmodule CodeCorps.Emails.ReceiptEmailTest do
assert template_model == %{
charge_amount: "$5.00",
charge_statement_descriptor: "Test descriptor",
name: "Jimmy",
project_title: "Code Corps",
project_url: "http://localhost:4200/#{project.organization.slug}/#{project.slug}",
project_current_donation_goal_description: "Test goal",
subject: "Your monthly donation to Code Corps",
user_first_name: "Jimmy"
subject: "Your monthly donation to Code Corps"
}
assert high_five_image_url
end
Expand Down

0 comments on commit ffc2a1b

Please sign in to comment.