提交 576209b4 编写于 作者: Y Yoshiyuki Kinjo

Add `perform_deliveries` to a payload of `deliver.action_mailer` notification.

上级 383b8bc8
* Skip delivery notification when `perform_deliveries` is false.
* Add `perform_deliveries` to a payload of `deliver.action_mailer` notification.
*Yoshiyuki Kinjo*
* Change delivery logging message when `perform_deliveries` is false.
*Yoshiyuki Kinjo*
......
......@@ -579,7 +579,6 @@ def receive(raw_mail)
# calling +deliver_mail+ directly and passing a <tt>Mail::Message</tt> will do
# nothing except tell the logger you sent the email.
def deliver_mail(mail) #:nodoc:
return unless mail.perform_deliveries
ActiveSupport::Notifications.instrument("deliver.action_mailer") do |payload|
set_payload_for_mail(payload, mail)
yield # Let Mail do the delivery actions
......@@ -589,15 +588,16 @@ def deliver_mail(mail) #:nodoc:
private
def set_payload_for_mail(payload, mail)
payload[:mailer] = name
payload[:message_id] = mail.message_id
payload[:subject] = mail.subject
payload[:to] = mail.to
payload[:from] = mail.from
payload[:bcc] = mail.bcc if mail.bcc.present?
payload[:cc] = mail.cc if mail.cc.present?
payload[:date] = mail.date
payload[:mail] = mail.encoded
payload[:mailer] = name
payload[:message_id] = mail.message_id
payload[:subject] = mail.subject
payload[:to] = mail.to
payload[:from] = mail.from
payload[:bcc] = mail.bcc if mail.bcc.present?
payload[:cc] = mail.cc if mail.cc.present?
payload[:date] = mail.date
payload[:mail] = mail.encoded
payload[:perform_deliveries] = mail.perform_deliveries
end
def method_missing(method_name, *args)
......
......@@ -9,8 +9,13 @@ class LogSubscriber < ActiveSupport::LogSubscriber
# An email was delivered.
def deliver(event)
info do
perform_deliveries = event.payload[:perform_deliveries]
recipients = Array(event.payload[:to]).join(", ")
"Sent mail to #{recipients} (#{event.duration.round(1)}ms)"
if perform_deliveries
"Sent mail to #{recipients} (#{event.duration.round(1)}ms)"
else
"Skipped sending mail to #{recipients} as `perform_deliveries` is false"
end
end
debug { event.payload[:mail] }
......
......@@ -37,13 +37,16 @@ def test_deliver_is_notified
BaseMailer.deliveries.clear
end
def test_deliver_is_not_notified_when_perform_deliveries_is_false
def test_deliver_message_when_perform_deliveries_is_false
BaseMailer.welcome_without_deliveries.deliver_now
wait
assert_equal(0, @logger.logged(:info).size)
assert_equal(1, @logger.logged(:debug).size)
assert_equal(1, @logger.logged(:info).size)
assert_match("Skipped sending mail to system@test.lindsaar.net as `perform_deliveries` is false", @logger.logged(:info).first)
assert_equal(2, @logger.logged(:debug).size)
assert_match(/BaseMailer#welcome_without_deliveries: processed outbound mail in [\d.]+ms/, @logger.logged(:debug).first)
assert_match("Welcome", @logger.logged(:debug).second)
ensure
BaseMailer.deliveries.clear
end
......
......@@ -319,17 +319,18 @@ Action Mailer
### deliver.action_mailer
| Key | Value |
| ------------- | -------------------------------------------- |
| `:mailer` | Name of the mailer class |
| `:message_id` | ID of the message, generated by the Mail gem |
| `:subject` | Subject of the mail |
| `:to` | To address(es) of the mail |
| `:from` | From address of the mail |
| `:bcc` | BCC addresses of the mail |
| `:cc` | CC addresses of the mail |
| `:date` | Date of the mail |
| `:mail` | The encoded form of the mail |
| Key | Value |
| --------------------- | ---------------------------------------------------- |
| `:mailer` | Name of the mailer class |
| `:message_id` | ID of the message, generated by the Mail gem |
| `:subject` | Subject of the mail |
| `:to` | To address(es) of the mail |
| `:from` | From address of the mail |
| `:bcc` | BCC addresses of the mail |
| `:cc` | CC addresses of the mail |
| `:date` | Date of the mail |
| `:mail` | The encoded form of the mail |
| `:perform_deliveries` | Whether delivery of this message is performed or not |
```ruby
{
......@@ -339,7 +340,8 @@ Action Mailer
to: ["users@rails.com", "dhh@rails.com"],
from: ["me@rails.com"],
date: Sat, 10 Mar 2012 14:18:09 +0100,
mail: "..." # omitted for brevity
mail: "...", # omitted for brevity
perform_deliveries: true
}
```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册