-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c5710d
commit e274f55
Showing
10 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class InvoiceMailer < ApplicationMailer | ||
after_action -> { @invoice.sent! } | ||
|
||
def invoice | ||
@invoice = params[:invoice] | ||
recipients = params[:recipients] | ||
subject = params[:subject] | ||
|
||
mail(to: recipients, subject:) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module InvoiceSendable | ||
extend ActiveSupport::Concern | ||
|
||
def send_to_email(subject:, recipients:) | ||
InvoiceMailer.with(invoice: self, subject:, recipients:).invoice.deliver_later | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' /> | ||
</head> | ||
<body> | ||
<h1>Invoice: <%= @invoice.invoice_number %></h1> | ||
|
||
<p> | ||
Hi <%= @invoice.client_name %>, You have an invoice with amount <%= @invoice.amount %>. | ||
</p> | ||
|
||
<p> | ||
The due date is <%= @invoice.due_date %>. | ||
</p> | ||
|
||
<p>Thanks!</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Hi, <%= @invoice.client_name %> | ||
=============================================== | ||
|
||
You have an invoice - <%= @invoice.invoice_number %> with amount <%= @invoice.amount %> | ||
due date is: <%= @invoice.due_date %>. | ||
|
||
Thanks! | ||
Miru |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe InvoiceMailer, type: :mailer do | ||
describe "invoice" do | ||
let(:invoice) { create :invoice } | ||
let(:recipients) { [invoice.client.email, "miru@example.com"] } | ||
let(:subject) { "Invoice (#{invoice.invoice_number}) due on #{invoice.due_date}" } | ||
let(:mail) { InvoiceMailer.with(invoice:, subject:, recipients:).invoice } | ||
|
||
it "renders the headers" do | ||
expect(mail.subject).to eq(subject) | ||
expect(mail.to).to eq(recipients) | ||
expect(mail.from).to eq(["from@example.com"]) | ||
end | ||
|
||
it "renders the body" do | ||
expect(mail.body.encoded).to match("You have an invoice") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
# Preview all emails at http://localhost:3000/rails/mailers/invoice | ||
|
||
class InvoicePreview < ActionMailer::Preview | ||
def invoice | ||
invoice = Invoice.first | ||
recipients = [invoice.client.email, "miru@example.com"] | ||
subject = "Invoice (#{invoice.invoice_number}) due on #{invoice.due_date}" | ||
|
||
InvoiceMailer.with(invoice:, recipients:, subject:).invoice | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
# This patches rspec-rails to support rails 7.0 https://github.com/rspec/rspec-rails/issues/2531 | ||
module RSpec | ||
module Rails | ||
module Matchers | ||
class HaveEnqueuedMail | ||
def legacy_mail?(job) | ||
defined?(ActionMailer::DeliveryJob) && job[:job] <= ActionMailer::DeliveryJob | ||
end | ||
|
||
def parameterized_mail?(job) | ||
RSpec::Rails::FeatureCheck.has_action_mailer_parameterized? && job[:job] <= ActionMailer::MailDeliveryJob | ||
end | ||
|
||
def unified_mail?(job) | ||
RSpec::Rails::FeatureCheck.has_action_mailer_unified_delivery? && job[:job] <= ActionMailer::MailDeliveryJob | ||
end | ||
end | ||
end | ||
end | ||
end |