convert app/models/mailer.rb for rails3

fixes CNVS-8732

makes Mailer#message act in the rails3 style, but then provides
scaffolding so that it produces the necessary rails2 behavior when
running under rails2

test-plan:
 * email delivery needs to be configured
 * set up an email communication channel for a student
 * configure to receive conversation alerts at that channel
 * have a teacher send a conversation message to that student
 * confirm receipt of the email at the student's address

Change-Id: I7f027a0a9a9dfd0231847017c888f1003b38d948
Reviewed-on: https://gerrit.instructure.com/25035
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Cody Cutrer <cody@instructure.com>
QA-Review: Jeremy Putnam <jeremyp@instructure.com>
Product-Review: Jacob Fugal <jacob@instructure.com>
This commit is contained in:
Jacob Fugal 2013-10-04 15:14:44 -06:00
parent b1d4019b65
commit 2bcb852a18
3 changed files with 82 additions and 19 deletions

View File

@ -192,7 +192,7 @@ class CommunicationChannel < ActiveRecord::Base
m = self.messages.new
m.to = self.path
m.body = t :body, "Your Canvas verification code is %{verification_code}", :verification_code => code
Mailer.deliver_message(m) rescue nil # omg! just ignore delivery failures
Mailer.message(m).deliver rescue nil # omg! just ignore delivery failures
end
# If you are creating a new communication_channel, do nothing, this just

View File

@ -20,24 +20,87 @@ class Mailer < ActionMailer::Base
attr_reader :email
def message(m)
headers({
# notifications have context, bounce replies don't.
'Auto-Submitted' => m.context ? 'auto-generated' : 'auto-replied'
})
recipients m.to
bcc m.bcc if m.bcc
cc m.cc if m.cc
from ("#{m.from_name || HostUrl.outgoing_email_default_name} <" + HostUrl.outgoing_email_address + ">")
reply_to ReplyToAddress.new(m).address
subject m.subject
if m.html_body
content_type 'multipart/alternative'
if CANVAS_RAILS2
# yielded to the block given to #mail as called from #message in order to
# perform the appropriate rails2-style ActionMailer actions on @target.
class Formatter
def initialize(target)
@target = target
end
part :content_type => 'text/plain; charset=utf-8', :body => m.body
part :content_type => 'text/html; charset=utf-8', :body => m.html_body
else
body m.body
# e.g. the "render text: ..." in format.html{ render text: ... }
def render(params)
params[:text]
end
# e.g. format.html{ render text: ... }
def html
body = yield self
@hasHTML = true
@target.content_type 'multipart/alternative'
@target.part content_type: 'text/html; charset=utf-8', body: body
end
# e.g. format.text{ render text: ... }
def text
body = yield self
if @hasHTML
@target.part content_type: 'text/plain; charset=utf-8', body: body
else
@target.body body
end
end
end
# now define #mail -- as called from #message -- to perform the appropriate
# rails2-style ActionMailer actions
def mail(params)
recipients params[:to]
bcc params[:bcc] if params[:bcc]
cc params[:cc] if params[:cc]
from params[:from]
reply_to params[:reply_to]
subject params[:subject]
yield Formatter.new(self)
self
end
# finally, define a proxy so that the rails3-style
# Mailer.message(m).deliver syntax works to invoke deliver_message(m) as
# expected by the rails2-implementation of ActionMailer
class Proxy
def initialize(message)
@message = message
end
def deliver
Mailer.deliver_message(@message)
end
end
def self.message(m)
Proxy.new(m)
end
end
# define in rails3-style
def message(m)
# notifications have context, bounce replies don't.
headers('Auto-Submitted' => m.context ? 'auto-generated' : 'auto-replied')
params = {
from: "#{m.from_name || HostUrl.outgoing_email_default_name} <" + HostUrl.outgoing_email_address + ">",
reply_to: ReplyToAddress.new(m).address,
to: m.to,
subject: m.subject
}
params[:cc] = m.cc if m.cc
params[:bcc] = m.bcc if m.bcc
mail(params) do |format|
format.html{ render text: m.html_body } if m.html_body
format.text{ render text: m.body }
end
end
end

View File

@ -603,7 +603,7 @@ class Message < ActiveRecord::Base
logger.info "Delivering mail: #{self.inspect}"
begin
res = Mailer.deliver_message(self)
res = Mailer.message(self).deliver
rescue Net::SMTPServerBusy => e
@exception = e
logger.error "Exception: #{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"