class Premailer::Rails::Hook
Attributes
message[R]
Public Class Methods
new(message)
click to toggle source
# File lib/premailer/rails/hook.rb, line 16 def initialize(message) @message = message end
perform(message)
click to toggle source
# File lib/premailer/rails/hook.rb, line 7 def perform(message) new(message).perform message end
Also aliased as: delivering_email, previewing_email
Public Instance Methods
perform()
click to toggle source
# File lib/premailer/rails/hook.rb, line 20 def perform if skip_premailer_header_present? remove_skip_premailer_header elsif message_contains_html? replace_html_part(generate_html_part_replacement) end end
Private Instance Methods
generate_alternative_part()
click to toggle source
# File lib/premailer/rails/hook.rb, line 60 def generate_alternative_part part = Mail::Part.new(content_type: 'multipart/alternative') part.add_part(generate_text_part) part.add_part(generate_html_part) part end
generate_html_part()
click to toggle source
# File lib/premailer/rails/hook.rb, line 68 def generate_html_part # Make sure that the text part is generated first. Otherwise the text # can end up containing CSS rules. generate_text_part if generate_text_part? part = html_part html = premailer.to_inline_css Mail::Part.new do content_type "text/html; charset=#{html.encoding}" body html end end
generate_html_part_replacement()
click to toggle source
# File lib/premailer/rails/hook.rb, line 48 def generate_html_part_replacement if generate_text_part? generate_alternative_part else generate_html_part end end
generate_text_part()
click to toggle source
# File lib/premailer/rails/hook.rb, line 81 def generate_text_part @text_part ||= begin part = html_part text = premailer.to_plain_text Mail::Part.new do content_type "text/plain; charset=#{text.encoding}" body text end end end
generate_text_part?()
click to toggle source
# File lib/premailer/rails/hook.rb, line 56 def generate_text_part? Rails.config[:generate_text_part] && !message.text_part end
html_part()
click to toggle source
# File lib/premailer/rails/hook.rb, line 96 def html_part if pure_html_message? message else message.html_part end end
message_contains_html?()
click to toggle source
# File lib/premailer/rails/hook.rb, line 38 def message_contains_html? html_part.present? end
premailer()
click to toggle source
# File lib/premailer/rails/hook.rb, line 92 def premailer @premailer ||= CustomizedPremailer.new(html_part.decoded) end
pure_html_message?()
click to toggle source
Returns true if the message itself has a content type of text/html, thus it does not contain other parts such as alternatives and attachments.
# File lib/premailer/rails/hook.rb, line 44 def pure_html_message? message.content_type && message.content_type.include?('text/html') end
remove_skip_premailer_header()
click to toggle source
# File lib/premailer/rails/hook.rb, line 34 def remove_skip_premailer_header message.header[:skip_premailer] = nil end
replace_html_part(new_part)
click to toggle source
# File lib/premailer/rails/hook.rb, line 104 def replace_html_part(new_part) if pure_html_message? replace_in_pure_html_message(new_part) else replace_part_in_list(message.parts, html_part, new_part) end end
replace_in_pure_html_message(new_part)
click to toggle source
If the new part is a pure text/html part, the body and its content type are used for the message. If the new part is
# File lib/premailer/rails/hook.rb, line 114 def replace_in_pure_html_message(new_part) if new_part.content_type.include?('text/html') message.body = new_part.decoded message.content_type = new_part.content_type else message.body = nil message.content_type = new_part.content_type new_part.parts.each do |part| message.add_part(part) end end end
replace_part_in_list(parts_list, old_part, new_part)
click to toggle source
# File lib/premailer/rails/hook.rb, line 127 def replace_part_in_list(parts_list, old_part, new_part) if (index = parts_list.index(old_part)) parts_list[index] = new_part else parts_list.any? do |part| if part.respond_to?(:parts) replace_part_in_list(part.parts, old_part, new_part) end end end end
skip_premailer_header_present?()
click to toggle source
# File lib/premailer/rails/hook.rb, line 30 def skip_premailer_header_present? message.header[:skip_premailer] end