Class: ActionMailer::InStyle::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/action_mailer/in_style/processor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Processor

Initialize Accepts one argument:

  • ActionMailer message object



16
17
18
19
20
21
# File 'lib/action_mailer/in_style/processor.rb', line 16

def initialize(message)
  @message = message

  @existing_html_part = message.html_part || (message.content_type =~ /text\/html/ && message)
  @premailer = ActionMailer::InStyle::Premailer.new(html_part.body.to_s, :with_html_string => true, :remove_classes => true)
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



11
12
13
# File 'lib/action_mailer/in_style/processor.rb', line 11

def message
  @message
end

#premailerObject (readonly)

Returns the value of attribute premailer.



11
12
13
# File 'lib/action_mailer/in_style/processor.rb', line 11

def premailer
  @premailer
end

Class Method Details

.inline!(message) ⇒ Object



7
8
9
# File 'lib/action_mailer/in_style/processor.rb', line 7

def self.inline!(message)
  new(message).inline!
end

Instance Method Details

#add_attachments!Object

Re-add any attachments



62
63
64
# File 'lib/action_mailer/in_style/processor.rb', line 62

def add_attachments!
  @existing_attachments.each {|a| message.body << a }
end

#add_html_part!Object

Add an HTML part with CSS inlined.



46
47
48
49
50
51
# File 'lib/action_mailer/in_style/processor.rb', line 46

def add_html_part!
  part = Mail::Part.new
  part.body = premailer.to_inline_css
  part.content_type = "text/html; charset=#{@msg_charset}"
  message.html_part = part
end

#add_text_part!Object

Add a text part with either the pre-existing text part, or one generated with premailer.



54
55
56
57
58
59
# File 'lib/action_mailer/in_style/processor.rb', line 54

def add_text_part!
  part = Mail::Part.new
  part.body = @existing_text_part || premailer.to_plain_text
  part.content_type = "text/plain; charset=#{@msg_charset}"
  message.text_part = part
end

#capture_existing_message_partsObject



66
67
68
69
70
# File 'lib/action_mailer/in_style/processor.rb', line 66

def capture_existing_message_parts
  @existing_text_part    = message.text_part && message.text_part.body.to_s
  @existing_attachments  = message.attachments
  @msg_charset           = message.charset
end

#html_partObject



23
24
25
# File 'lib/action_mailer/in_style/processor.rb', line 23

def html_part
  @existing_html_part
end

#inline!Object

InStyle! Processes the message object, replacing the html part with an inlined html version.

If the message contains attachments or text parts, these parts are preserved. If the message does not contain a text part, one will be constructed from the text content available in the html part.



34
35
36
37
38
39
40
41
42
43
# File 'lib/action_mailer/in_style/processor.rb', line 34

def inline!
  capture_existing_message_parts
  reset_message_body!

  add_text_part!
  add_html_part!
  add_attachments!

  message
end

#original_message_partsObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/action_mailer/in_style/processor.rb', line 72

def original_message_parts
  capture_existing_message_parts

  {
    :html_part    => html_part,
    :text_part    => @existing_text_part,
    :attachments  => @existing_attachments,
    :charset      => @msg_charset
  }
end

#reset_message_body!Object



83
84
85
86
# File 'lib/action_mailer/in_style/processor.rb', line 83

def reset_message_body!
  message.body = nil
  message.body.instance_variable_set("@parts", Mail::PartsList.new)
end