Skip to content

Commit

Permalink
mailer previews for NullMail instances. Closes #19849.
Browse files Browse the repository at this point in the history
  • Loading branch information
senny committed Apr 27, 2015
1 parent 503a80a commit 95c2734
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
7 changes: 7 additions & 0 deletions actionmailer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
* Mailer previews no longer crash when the `mail` method wasn't called
(`NullMail`).

Fixes #19849.

*Yves Senn*

* Make sure labels and values line up in mailer previews.

*Yves Senn*
Expand Down
1 change: 1 addition & 0 deletions actionmailer/lib/action_mailer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ def process(method_name, *args) #:nodoc:

class NullMail #:nodoc:
def body; '' end
def header; {} end

def respond_to?(string, include_all=false)
true
Expand Down
10 changes: 5 additions & 5 deletions railties/lib/rails/mailers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def preview
@page_title = "Mailer Previews for #{@preview.preview_name}"
render action: 'mailer'
else
email = File.basename(params[:path])
@email_action = File.basename(params[:path])

if @preview.email_exists?(email)
@email = @preview.call(email)
if @preview.email_exists?(@email_action)
@email = @preview.call(@email_action)

if params[:part]
part_type = Mime::Type.lookup(params[:part])
Expand All @@ -28,14 +28,14 @@ def preview
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}"
raise AbstractController::ActionNotFound, "Email part '#{part_type}' not found in #{@preview.name}##{@email_action}"
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}"
raise AbstractController::ActionNotFound, "Email '#{@email_action}' not found in #{@preview.name}"
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion railties/lib/rails/templates/rails/mailers/email.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,16 @@
</dl>
</header>

<iframe seamless name="messageBody" src="?part=<%= Rack::Utils.escape(@part.mime_type) %>"></iframe>
<% if @part.mime_type %>
<iframe seamless name="messageBody" src="?part=<%= Rack::Utils.escape(@part.mime_type) %>"></iframe>
<% else %>
<p>
You are trying to preview an email without content.
Make sure to call the <em>mail</em> method in <em><%= @preview.preview_name %>#<%= @email_action %></em>.
</p>
<strong>Message (debug):</strong>
<%= debug @email %>
<% end %>

</body>
</html>
27 changes: 27 additions & 0 deletions railties/test/application/mailer_previews_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,33 @@ def foo
assert_match "Email &#39;bar&#39; not found in NotifierPreview", last_response.body
end

test "mailer preview NullMail" do
mailer 'notifier', <<-RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo
# does not call +mail+
end
end
RUBY

mailer_preview 'notifier', <<-RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
end
RUBY

app('development')

get "/rails/mailers/notifier/foo"
assert_match "You are trying to preview an email without content.", last_response.body
assert_match "notifier#foo", last_response.body
assert_match "ActionMailer::Base::NullMail", last_response.body
end

test "mailer preview email part not found" do
mailer 'notifier', <<-RUBY
class Notifier < ActionMailer::Base
Expand Down

0 comments on commit 95c2734

Please sign in to comment.