From 37586ddf279c46c3d0c8c2553d0918aa82ceb020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sun, 28 Oct 2012 12:55:53 -0700 Subject: Merge pull request #8048 from senny/7761_dont_render_view_without_mail_call Do not render views when mail() isn't called. (NullMail refactoring) Conflicts: actionmailer/CHANGELOG.md --- actionmailer/CHANGELOG.md | 11 +++++++++++ actionmailer/lib/action_mailer/base.rb | 14 +++++++++++++- actionmailer/test/base_test.rb | 6 ++++++ .../test/fixtures/base_mailer/without_mail_call.erb | 1 + actionmailer/test/mailers/base_mailer.rb | 3 +++ 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 actionmailer/test/fixtures/base_mailer/without_mail_call.erb (limited to 'actionmailer') diff --git a/actionmailer/CHANGELOG.md b/actionmailer/CHANGELOG.md index d938ffbb4c..74aede2cb7 100644 --- a/actionmailer/CHANGELOG.md +++ b/actionmailer/CHANGELOG.md @@ -1,15 +1,26 @@ +## Rails 3.2.9 (unreleased) ## + +* Do not render views when mail() isn't called. + Fix #7761 + + *Yves Senn* + + ## Rails 3.2.8 (Aug 9, 2012) ## * No changes. + ## Rails 3.2.7 (Jul 26, 2012) ## * No changes. + ## Rails 3.2.6 (Jun 12, 2012) ## * No changes. + ## Rails 3.2.5 (Jun 1, 2012) ## * No changes. diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 2a11cb6ca7..a9fb49a303 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -454,7 +454,19 @@ module ActionMailer #:nodoc: 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 diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index d92fc01ef8..b69b26faf0 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -471,6 +471,12 @@ class BaseTest < ActiveSupport::TestCase assert_equal(%{Dummy}, mail.body.to_s.strip) 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 diff --git a/actionmailer/test/fixtures/base_mailer/without_mail_call.erb b/actionmailer/test/fixtures/base_mailer/without_mail_call.erb new file mode 100644 index 0000000000..290379d5fb --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/without_mail_call.erb @@ -0,0 +1 @@ +<% raise 'the template should not be rendered' %> \ No newline at end of file diff --git a/actionmailer/test/mailers/base_mailer.rb b/actionmailer/test/mailers/base_mailer.rb index e55d72fdb4..8c4430b046 100644 --- a/actionmailer/test/mailers/base_mailer.rb +++ b/actionmailer/test/mailers/base_mailer.rb @@ -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 -- cgit v1.2.3