discourse/lib/email/renderer.rb
Sam Saffron 71ea4ad7fc PERF: reuse renderer when rendering email templates
Previous to this fix we were leaking methods on the internal action view
template class per render.

This caused email generation to be very low and a steady memory leak in the
application in sidekiq when sending out emails

The behavior change is new to Rails 6 so this fix does not need to be
backported into stable.
2019-10-06 23:57:03 -04:00

36 lines
825 B
Ruby

# frozen_string_literal: true
module Email
class Renderer
def initialize(message, opts = nil)
@message = message
@opts = opts || {}
end
def text
return @text if @text
@text = (+(@message.text_part ? @message.text_part : @message).body.to_s).force_encoding('UTF-8')
@text = CGI.unescapeHTML(@text)
end
def html
style = if @message.html_part
Email::Styles.new(@message.html_part.body.to_s, @opts)
else
unstyled = UserNotificationRenderer.instance.render(
template: 'layouts/email_template',
format: :html,
locals: { html_body: PrettyText.cook(text).html_safe }
)
Email::Styles.new(unstyled, @opts)
end
style.format_basic
style.format_html
style.to_html
end
end
end