1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Do not render views when mail() isn't called. (NullMail refactoring)

This commit is contained in:
Yves Senn 2012-09-29 22:29:29 +02:00
parent 2e44dda27a
commit b786f065d3
5 changed files with 28 additions and 1 deletions

View file

@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
* Do not render views when mail() isn't called.
Fix #7761
*Yves Senn*
* Support `Mailer.deliver_foo(*args)` as a synonym for
`Mailer.foo(*args).deliver`. This makes it easy to write e.g.
`Mailer.expects(:deliver_foo)` when testing code that calls

View file

@ -510,7 +510,19 @@ module ActionMailer
def process(*args) #:nodoc:
lookup_context.skip_default_locale!
super
generated_mail = super
unless generated_mail
@_message = NullMail.new
end
end
class NullMail #:nodoc:
def body; '' end
def method_missing(*args)
nil
end
end
def mailer_name

View file

@ -499,6 +499,12 @@ class BaseTest < ActiveSupport::TestCase
end
end
test 'the view is not rendered when mail was never called' do
mail = BaseMailer.without_mail_call
assert_equal('', mail.body.to_s.strip)
mail.deliver
end
# Before and After hooks
class MyObserver

View file

@ -0,0 +1 @@
<% raise 'the template should not be rendered' %>

View file

@ -115,4 +115,7 @@ class BaseMailer < ActionMailer::Base
def email_with_translations
mail body: render("email_with_translations", formats: [:html])
end
def without_mail_call
end
end