Skip to content

Commit d6dec7f

Browse files
committed
Add mailer previews feature based on mail_view gem
1 parent 1602a70 commit d6dec7f

17 files changed

Lines changed: 736 additions & 6 deletions

File tree

actionmailer/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Add mailer previews feature based on 37 Signals mail_view gem
2+
3+
*Andrew White*
4+
15
* Calling `mail()` without arguments serves as getter for the current mail
26
message and keeps previously set headers.
37

actionmailer/lib/action_mailer.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ module ActionMailer
4141
autoload :Base
4242
autoload :DeliveryMethods
4343
autoload :MailHelper
44+
autoload :Preview
45+
autoload :Previews, 'action_mailer/preview'
4446
autoload :TestCase
4547
autoload :TestHelper
4648
end

actionmailer/lib/action_mailer/base.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,25 @@ module ActionMailer
308308
# Note that unless you have a specific reason to do so, you should prefer using before_action
309309
# rather than after_action in your ActionMailer classes so that headers are parsed properly.
310310
#
311+
# = Previewing emails
312+
#
313+
# You can preview your email templates visually by adding a mailer preview file to the
314+
# <tt>ActionMailer::Base.preview_path</tt>. Since most emails do something interesting
315+
# with database data, you'll need to write some scenarios to load messages with fake data:
316+
#
317+
# class NotifierPreview < ActionMailer::Preview
318+
# def welcome
319+
# Notifier.welcome(User.first)
320+
# end
321+
# end
322+
#
323+
# Methods must return a Mail::Message object which can be generated by calling the mailer
324+
# method without the additional <tt>deliver</tt>. The location of the mailer previews
325+
# directory can be configured using the <tt>preview_path</tt> option which has a default
326+
# of <tt>test/mailers/previews</tt>:
327+
#
328+
# config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews"
329+
#
311330
# = Configuration options
312331
#
313332
# These options are specified on the class level, like
@@ -362,6 +381,7 @@ module ActionMailer
362381
# <tt>delivery_method :test</tt>. Most useful for unit and functional testing.
363382
class Base < AbstractController::Base
364383
include DeliveryMethods
384+
include Previews
365385

366386
abstract!
367387

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'active_support/descendants_tracker'
2+
3+
module ActionMailer
4+
module Previews #:nodoc:
5+
extend ActiveSupport::Concern
6+
7+
included do
8+
# Set the location of mailer previews through app configuration:
9+
#
10+
# config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews"
11+
#
12+
class_attribute :preview_path, instance_writer: false
13+
end
14+
end
15+
16+
class Preview
17+
extend ActiveSupport::DescendantsTracker
18+
19+
class << self
20+
# Returns all mailer preview classes
21+
def all
22+
load_previews if descendants.empty?
23+
descendants
24+
end
25+
26+
# Returns the mail object for the given email name
27+
def call(email)
28+
preview = self.new
29+
preview.public_send(email)
30+
end
31+
32+
# Returns all of the available email previews
33+
def emails
34+
public_instance_methods(false).map(&:to_s).sort
35+
end
36+
37+
# Returns true if the email exists
38+
def email_exists?(email)
39+
emails.include?(email)
40+
end
41+
42+
# Returns true if the preview exists
43+
def exists?(preview)
44+
all.any?{ |p| p.preview_name == preview }
45+
end
46+
47+
# Find a mailer preview by its underscored class name
48+
def find(preview)
49+
all.find{ |p| p.preview_name == preview }
50+
end
51+
52+
# Returns the underscored name of the mailer preview without the suffix
53+
def preview_name
54+
name.sub(/Preview$/, '').underscore
55+
end
56+
57+
protected
58+
def load_previews #:nodoc:
59+
Dir["#{preview_path}/**/*_preview.rb"].each{ |file| require_dependency file }
60+
end
61+
62+
def preview_path #:nodoc:
63+
Base.preview_path
64+
end
65+
end
66+
end
67+
end

actionmailer/lib/action_mailer/railtie.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,13 @@ class Railtie < Rails::Railtie # :nodoc:
4040
config.compile_methods! if config.respond_to?(:compile_methods!)
4141
end
4242
end
43+
44+
initializer "action_mailer.configure_mailer_previews", before: :set_autoload_paths do |app|
45+
if Rails.env.development?
46+
options = app.config.action_mailer
47+
options.preview_path ||= defined?(Rails.root) ? "#{Rails.root}/test/mailers/previews" : nil
48+
app.config.autoload_paths << options.preview_path
49+
end
50+
end
4351
end
4452
end

actionpack/lib/action_dispatch/routing/inspector.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def action
6969
end
7070

7171
def internal?
72-
controller.to_s =~ %r{\Arails/(info|welcome)} || path =~ %r{\A#{Rails.application.config.assets.prefix}}
72+
controller.to_s =~ %r{\Arails/(info|mailers|welcome)} || path =~ %r{\A#{Rails.application.config.assets.prefix}}
7373
end
7474

7575
def engine?

railties/lib/rails.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module Rails
2525

2626
autoload :Info
2727
autoload :InfoController
28+
autoload :MailersController
2829
autoload :WelcomeController
2930

3031
class << self

railties/lib/rails/application/finisher.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ module Finisher
2222
initializer :add_builtin_route do |app|
2323
if Rails.env.development?
2424
app.routes.append do
25+
get '/rails/mailers' => "rails/mailers#index"
26+
get '/rails/mailers/*path' => "rails/mailers#preview"
2527
get '/rails/info/properties' => "rails/info#properties"
2628
get '/rails/info/routes' => "rails/info#routes"
2729
get '/rails/info' => "rails/info#index"

railties/lib/rails/generators/test_unit/mailer/mailer_generator.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ module TestUnit # :nodoc:
44
module Generators # :nodoc:
55
class MailerGenerator < Base # :nodoc:
66
argument :actions, type: :array, default: [], banner: "method method"
7-
check_class_collision suffix: "Test"
7+
8+
def check_class_collision
9+
class_collisions "#{class_name}Test", "#{class_name}Preview"
10+
end
811

912
def create_test_files
1013
template "functional_test.rb", File.join('test/mailers', class_path, "#{file_name}_test.rb")
1114
end
15+
16+
def create_preview_files
17+
template "preview.rb", File.join('test/mailers/previews', class_path, "#{file_name}_preview.rb")
18+
end
1219
end
1320
end
1421
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<% module_namespacing do -%>
2+
class <%= class_name %>Preview < ActionMailer::Preview
3+
<% actions.each do |action| -%>
4+
5+
def <%= action %>
6+
<%= class_name %>.<%= action %>
7+
end
8+
<% end -%>
9+
10+
end
11+
<% end -%>

0 commit comments

Comments
 (0)