-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API: added helpers support within mailers
- Loading branch information
1 parent
2b2937f
commit a416a87
Showing
13 changed files
with
136 additions
and
2 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
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
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,6 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationMailer < ActionMailer::Base | ||
default from: '[email protected]' | ||
layout 'mailer' | ||
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,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class PersonMailer < ApplicationMailer | ||
def welcome | ||
@person = params[:person].decorated | ||
@family = @person.children | ||
@url = @person.url | ||
@urls = @person.class.urls | ||
|
||
mail subject: "#{@person.name} joined us" | ||
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
class Person < ApplicationRecord | ||
belongs_to :parent, class_name: 'Person' | ||
belongs_to :parent, class_name: 'Person', inverse_of: :children | ||
has_many :children, class_name: 'Person', inverse_of: :parent | ||
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 http-equiv="Content-Type" content="text/html; charset=utf-8"> | ||
<style> | ||
/* Email styles need to be inline */ | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<%= yield %> | ||
|
||
<footer> | ||
<nav> | ||
<%= yield :links %> | ||
</nav> | ||
</footer> | ||
</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 @@ | ||
<%= yield %> |
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,21 @@ | ||
<% content_for :links do %> | ||
<ul> | ||
<% for link in @person.class.links do %> | ||
<li><%= link %></li> | ||
<% end %> | ||
</ul> | ||
<% end %> | ||
|
||
<h1>Welcome <%= @person.link %>!</h1> | ||
|
||
<% if @family %> | ||
<h2>Family</h2> | ||
|
||
<ul> | ||
<% for person in @family do %> | ||
<li> | ||
<%= render person %> | ||
</li> | ||
<% end %> | ||
</ul> | ||
<% 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,12 @@ | ||
Welcome <%= @person.name %>! | ||
|
||
<% if @family %>Family: | ||
<% for person in @family do %> | ||
* <%= person.name %><% end %> | ||
<% end %> | ||
|
||
Details: <%= @url %> | ||
|
||
<% for name, url in @urls do %>See also: | ||
* <%= name.to_s.humanize %>: <%= url %> | ||
<% 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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe PersonMailer do | ||
subject { described_class.with person: john } | ||
|
||
let(:john) { Person.create! first_name: 'John', last_name: 'Smith' } | ||
let(:mathew) { Person.create! first_name: 'Mathew', last_name: 'Johnson', parent: john } | ||
|
||
describe 'welcome' do | ||
subject { super().welcome } | ||
|
||
before { mathew } # should exist | ||
|
||
it { is_expected.to have_attributes subject: 'John Smith joined us' } | ||
|
||
describe 'HTML' do | ||
include RSpecHtmlMatchers | ||
|
||
subject { super().html_part.body.to_s } | ||
|
||
it { is_expected.to have_tag 'h1', text: 'John Smith' } | ||
it { is_expected.to have_tag 'li .name', text: 'Mathew Johnson' } | ||
|
||
describe 'helpers within a view' do | ||
describe 'instance-level' do | ||
it { is_expected.to have_tag 'a', href: url_for(mathew), text: 'Mathew Johnson' } | ||
end | ||
|
||
describe 'class-level' do | ||
it { is_expected.to have_tag 'nav a', href: people_path, text: 'All' } | ||
end | ||
end | ||
end | ||
|
||
describe 'text' do | ||
subject { super().text_part.body.to_s } | ||
|
||
it { is_expected.to include 'Welcome John Smith!' } | ||
it { is_expected.to include '* Mathew Johnson' } | ||
|
||
describe 'helpers within a mailer action' do | ||
describe 'instance-level' do | ||
it { is_expected.to include "Details: #{url_for john}" } | ||
end | ||
|
||
describe 'class-level' do | ||
it { is_expected.to include "* All: #{people_url}" } | ||
end | ||
end | ||
end | ||
end | ||
end |