better handle varying number of parts for mailer

in the rails2 shim. refs CNVS-9184

test-plan:
 - configure canvas to send outgoing email
 - configure a user with a real eamil address and set notifications for
   this user to asap
 - have a different user initiate a conversation with the first user
 - check for a notification email from the conversation
   - entire email should have content-type multipart/alternative with
     both text and html parts
   - text part should have content-type text/plain
   - html part should have content-type text/html
   - both parts should have charset=utf-8 in their content-type
   - html format should still be displayed by default

Change-Id: I0dd1cf7aad6d51e808438946bce172d256ffceb7
Reviewed-on: https://gerrit.instructure.com/26295
Reviewed-by: Jon Willesen <jonw@instructure.com>
QA-Review: Jon Willesen <jonw@instructure.com>
Tested-by: Jenkins <jenkins@instructure.com>
Product-Review: Jacob Fugal <jacob@instructure.com>
This commit is contained in:
Jacob Fugal 2013-11-13 15:37:26 -07:00
parent 58df738069
commit b0e3177647
1 changed files with 26 additions and 14 deletions

View File

@ -26,6 +26,7 @@ class Mailer < ActionMailer::Base
class Formatter
def initialize(target)
@target = target
@parts = []
end
# e.g. the "render text: ..." in format.html{ render text: ... }
@ -33,21 +34,30 @@ class Mailer < ActionMailer::Base
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
# e.g. format.text{ render text: ... }
def text(&block)
@parts << {
content_type: 'text/plain; charset=utf-8',
body: instance_eval(&block)
}
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
# e.g. format.html{ render text: ... }
def html(&block)
@parts << {
content_type: 'text/html; charset=utf-8',
body: instance_eval(&block)
}
end
def commit
if @parts.size > 1
@target.content_type 'multipart/alternative'
@parts.each{ |part| @target.part part }
elsif @parts.size == 1
part = @parts.first
@target.content_type part[:content_type]
@target.body part[:body]
end
end
end
@ -61,7 +71,9 @@ class Mailer < ActionMailer::Base
from params[:from]
reply_to params[:reply_to]
subject params[:subject]
yield Formatter.new(self)
formatter = Formatter.new(self)
yield formatter
formatter.commit
self
end