Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add mailer previews feature based on mail_view gem
- Loading branch information
Showing
17 changed files
with
736 additions
and
6 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,67 @@ | ||
require 'active_support/descendants_tracker' | ||
|
||
module ActionMailer | ||
module Previews #:nodoc: | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
# Set the location of mailer previews through app configuration: | ||
# | ||
# config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews" | ||
# | ||
class_attribute :preview_path, instance_writer: false | ||
end | ||
end | ||
|
||
class Preview | ||
extend ActiveSupport::DescendantsTracker | ||
|
||
class << self | ||
# Returns all mailer preview classes | ||
def all | ||
load_previews if descendants.empty? | ||
descendants | ||
end | ||
|
||
# Returns the mail object for the given email name | ||
def call(email) | ||
preview = self.new | ||
preview.public_send(email) | ||
end | ||
|
||
# Returns all of the available email previews | ||
def emails | ||
public_instance_methods(false).map(&:to_s).sort | ||
end | ||
|
||
# Returns true if the email exists | ||
def email_exists?(email) | ||
emails.include?(email) | ||
end | ||
|
||
# Returns true if the preview exists | ||
def exists?(preview) | ||
all.any?{ |p| p.preview_name == preview } | ||
end | ||
|
||
# Find a mailer preview by its underscored class name | ||
def find(preview) | ||
all.find{ |p| p.preview_name == preview } | ||
end | ||
|
||
# Returns the underscored name of the mailer preview without the suffix | ||
def preview_name | ||
name.sub(/Preview$/, '').underscore | ||
end | ||
|
||
protected | ||
def load_previews #:nodoc: | ||
Dir["#{preview_path}/**/*_preview.rb"].each{ |file| require_dependency file } | ||
end | ||
|
||
def preview_path #:nodoc: | ||
Base.preview_path | ||
end | ||
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
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
11 changes: 11 additions & 0 deletions
11
railties/lib/rails/generators/test_unit/mailer/templates/preview.rb
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,11 @@ | ||
<% module_namespacing do -%> | ||
class <%= class_name %>Preview < ActionMailer::Preview | ||
<% actions.each do |action| -%> | ||
def <%= action %> | ||
<%= class_name %>.<%= action %> | ||
end | ||
<% 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,73 @@ | ||
require 'rails/application_controller' | ||
|
||
class Rails::MailersController < Rails::ApplicationController # :nodoc: | ||
prepend_view_path ActionDispatch::DebugExceptions::RESCUES_TEMPLATE_PATH | ||
|
||
before_filter :require_local! | ||
before_filter :find_preview, only: :preview | ||
|
||
def index | ||
@previews = ActionMailer::Preview.all | ||
@page_title = "Mailer Previews" | ||
end | ||
|
||
def preview | ||
if params[:path] == @preview.preview_name | ||
@page_title = "Mailer Previews for #{@preview.preview_name}" | ||
render action: 'mailer' | ||
else | ||
email = File.basename(params[:path]) | ||
|
||
if @preview.email_exists?(email) | ||
@email = @preview.call(email) | ||
|
||
if params[:part] | ||
part_type = Mime::Type.lookup(params[:part]) | ||
|
||
if part = find_part(part_type) | ||
response.content_type = part_type | ||
render text: part.respond_to?(:decoded) ? part.decoded : part | ||
else | ||
raise AbstractController::ActionNotFound, "Email part '#{part_type}' not found in #{@preview.name}##{email}" | ||
end | ||
else | ||
@part = find_preferred_part(request.format, Mime::HTML, Mime::TEXT) | ||
render action: 'email', layout: false, formats: %w[html] | ||
end | ||
else | ||
raise AbstractController::ActionNotFound, "Email '#{email}' not found in #{@preview.name}" | ||
end | ||
end | ||
end | ||
|
||
protected | ||
def find_preview | ||
candidates = [] | ||
params[:path].to_s.scan(%r{/|$}){ candidates << $` } | ||
preview = candidates.detect{ |candidate| ActionMailer::Preview.exists?(candidate) } | ||
|
||
if preview | ||
@preview = ActionMailer::Preview.find(preview) | ||
else | ||
raise AbstractController::ActionNotFound, "Mailer preview '#{params[:path]}' not found" | ||
end | ||
end | ||
|
||
def find_preferred_part(*formats) | ||
if @email.multipart? | ||
formats.each do |format| | ||
return find_part(format) if @email.parts.any?{ |p| p.mime_type == format } | ||
end | ||
else | ||
end | ||
end | ||
|
||
def find_part(format) | ||
if @email.multipart? | ||
@email.parts.find{ |p| p.mime_type == format } | ||
elsif @email.mime_type == format | ||
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
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,98 @@ | ||
<!DOCTYPE html> | ||
<html><head> | ||
<meta name="viewport" content="width=device-width" /> | ||
<style type="text/css"> | ||
header { | ||
width: 100%; | ||
padding: 10px 0 0 0; | ||
margin: 0; | ||
background: white; | ||
font: 12px "Lucida Grande", sans-serif; | ||
border-bottom: 1px solid #dedede; | ||
overflow: hidden; | ||
} | ||
|
||
dl { | ||
margin: 0 0 10px 0; | ||
padding: 0; | ||
} | ||
|
||
dt { | ||
width: 80px; | ||
padding: 1px; | ||
float: left; | ||
clear: left; | ||
text-align: right; | ||
color: #7f7f7f; | ||
} | ||
|
||
dd { | ||
margin-left: 90px; /* 80px + 10px */ | ||
padding: 1px; | ||
} | ||
|
||
iframe { | ||
border: 0; | ||
width: 100%; | ||
height: 800px; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<header> | ||
<dl> | ||
<% if @email.respond_to?(:smtp_envelope_from) && Array(@email.from) != Array(@email.smtp_envelope_from) %> | ||
<dt>SMTP-From:</dt> | ||
<dd><%= @email.smtp_envelope_from %></dd> | ||
<% end %> | ||
<% if @email.respond_to?(:smtp_envelope_to) && @email.to != @email.smtp_envelope_to %> | ||
<dt>SMTP-To:</dt> | ||
<dd><%= @email.smtp_envelope_to %></dd> | ||
<% end %> | ||
|
||
<dt>From:</dt> | ||
<dd><%= @email.header['from'] %></dd> | ||
|
||
<% if @email.reply_to %> | ||
<dt>Reply-To:</dt> | ||
<dd><%= @email.header['reply-to'] %></dd> | ||
<% end %> | ||
|
||
<dt>To:</dt> | ||
<dd><%= @email.header['to'] %></dd> | ||
|
||
<% if @email.cc %> | ||
<dt>CC:</dt> | ||
<dd><%= @email.header['cc'] %></dd> | ||
<% end %> | ||
|
||
<dt>Date:</dt> | ||
<dd><%= Time.current.rfc2822 %></dd> | ||
|
||
<dt>Subject:</dt> | ||
<dd><strong><%= @email.subject %></strong></dd> | ||
|
||
<% unless @email.attachments.nil? || @email.attachments.empty? %> | ||
<dt>Attachments:</dt> | ||
<dd> | ||
<%= @email.attachments.map { |a| a.respond_to?(:original_filename) ? a.original_filename : a.filename }.inspect %> | ||
</dd> | ||
<% end %> | ||
<% if @email.multipart? %> | ||
<dd> | ||
<select onchange="document.getElementsByName('messageBody')[0].src=this.options[this.selectedIndex].value;"> | ||
<option <%= request.format == Mime::HTML ? 'selected' : '' %> value="?part=text%2Fhtml">View as HTML email</option> | ||
<option <%= request.format == Mime::TEXT ? 'selected' : '' %> value="?part=text%2Fplain">View as plain-text email</option> | ||
</select> | ||
</dd> | ||
<% end %> | ||
</dl> | ||
</header> | ||
|
||
<iframe seamless name="messageBody" src="?part=<%= Rack::Utils.escape(@part.mime_type) %>"></iframe> | ||
|
||
</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 @@ | ||
<% @previews.each do |preview| %> | ||
<h3><%= link_to preview.preview_name.titleize, "/rails/mailers/#{preview.preview_name}" %></h3> | ||
<ul> | ||
<% preview.emails.each do |email| %> | ||
<li><%= link_to email, "/rails/mailers/#{preview.preview_name}/#{email}" %></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,6 @@ | ||
<h3><%= @preview.preview_name.titleize %></h3> | ||
<ul> | ||
<% @preview.emails.each do |email| %> | ||
<li><%= link_to email, "/rails/mailers/#{@preview.preview_name}/#{email}" %></li> | ||
<% end %> | ||
</ul> |
Oops, something went wrong.