Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #12 from glebm/master

Rails 3.2 support (+ assets)
  • Loading branch information...
commit 7d08b92d2bb3f5ebf052f9384bf8079a243fa9b6 2 parents a9c939f + 8a4e78a
Nathan Broadbent ndbroadbent authored
2  .gitignore
View
@@ -1,3 +1,5 @@
+.rvmrc
+.idea
Gemfile.lock
*.swp
*.swo
52 lib/action_mailer/inline_css_hook.rb
View
@@ -4,38 +4,38 @@
module ActionMailer
class InlineCssHook
def self.delivering_email(message)
+
if html_part = (message.html_part || (message.content_type =~ /text\/html/ && message))
- # Generate an email with all CSS inlined (access CSS a FS path)
- premailer = ::Premailer.new(html_part.body.to_s,
- :with_html_string => true)
-
- # Prepend host to remaning URIs.
- # Two-phase conversion to avoid request deadlock from dev. server (Issue #4)
- premailer = ::Premailer.new(premailer.to_inline_css,
- :with_html_string => true,
- :base_url => message.header[:host].to_s)
- existing_text_part = message.text_part && message.text_part.body.to_s
- existing_attachments = message.attachments
+ host = ActionMailerInlineCss.base_url || message.header[:host].to_s
+
+ # Generate an email with all CSS inlined (access CSS a FS path), and URIs
+ premailer = ::Premailer.new(html_part.body.to_s, :with_html_string => true, :base_url => host)
+
msg_charset = message.charset
- # Reset the body
- message.body = nil
- message.body.instance_variable_set("@parts", Mail::PartsList.new)
+ if message.text_part && message.text_part.body.to_s
+ html_part.content_type "text/html; charset=#{msg_charset}"
+ html_part.body premailer.to_inline_css
+ else
+ existing_attachments = message.attachments
- # Add an HTML part with CSS inlined.
- message.html_part do
- content_type "text/html; charset=#{msg_charset}"
- body premailer.to_inline_css
- end
+ # Clear body to make a multipart email
+ message.body = nil
- # Add a text part with either the pre-existing text part, or one generated with premailer.
- message.text_part do
- content_type "text/plain; charset=#{msg_charset}"
- body existing_text_part || premailer.to_plain_text
- end
+ # IMPORTANT: Plain text part must be generated before CSS is inlined.
+ # Not doing so results in CSS declarations (<style>) visible in the plain text part.
+ message.text_part = Mail::Part.new do
+ content_type "text/plain; charset=#{msg_charset}"
+ body premailer.to_plain_text
+ end
+
+ message.html_part = Mail::Part.new do
+ content_type "text/html; charset=#{msg_charset}"
+ body premailer.to_inline_css
+ end
- # Re-add any attachments
- existing_attachments.each {|a| message.body << a }
+ existing_attachments.each {|a| message.body << a }
+ end
message
end
4 lib/actionmailer_inline_css.rb
View
@@ -6,3 +6,7 @@
ActionMailer::Base.register_interceptor ActionMailer::InlineCssHook
+module ActionMailerInlineCss
+ mattr_accessor :base_url
+end
+
6 lib/overrides/premailer/premailer.rb
View
@@ -6,9 +6,13 @@
# a leading slash and a cache buster (e.g. ?12412422).
# This override handles these cases, while falling back to the default implementation.
def load_css_from_local_file_with_rails_path!(path)
- rails_path = Rails.root.join("public", path.sub(/\?[0-9a-zA-Z]+$/, '').sub(/^\//, '')).to_s
+ # Remove query string and the path
+ clean_path = path.sub(/\?.*$/, '').sub(%r(^https?://[^/]*/), '')
+ rails_path = Rails.root.join('public', clean_path)
if File.exist?(rails_path)
load_css_from_string(File.read(rails_path))
+ elsif (asset = Rails.application.assets.find_asset(clean_path.sub("#{Rails.configuration.assets.prefix}/", '')))
+ load_css_from_string(asset.source)
else
load_css_from_local_file_without_rails_path!(path)
end
3  test/inline_css_hook_test.rb
View
@@ -58,8 +58,7 @@ def use_inline_css_hook_with_text_and_html_parts
def use_inline_css_hook_with_utf_8
mail_with_defaults do |format|
- charset "utf8"
- format.html { render(:inline => TEST_HTML_UTF8) }
+ format.html(:charset => "utf8") { render(:inline => TEST_HTML_UTF8) }
end
end
6 test/premailer_stylesheet_link_tag_test.rb
View
@@ -30,12 +30,12 @@ def mail_with_defaults(&block)
class PremailerStylesheetLinkTagTest < ActionMailer::TestCase
def test_premailer_stylesheet_link_tag
- css_file = "div.test { color: #119911; }"
File.stubs(:exist?).returns(true)
- File.stubs(:read).returns(css_file)
+ File.stubs(:exists?).returns(true)
+ File.stubs(:read).returns("div.test { color: #119911; }")
mail = HelperMailer.use_stylesheet_link_tag.deliver
- assert_match "<div class=\"test\" style=\"color: #119911;\">", mail.html_part.body.encoded
+ assert_match %Q{<div class="test" style="color: #119911;">}, mail.html_part.body.encoded
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.