Permalink
Browse files

Using ActionMailer default host as Premailer base_url. Issue #2.

  • Loading branch information...
1 parent 4c92dc2 commit c54996ad94ecddde1a73db52834b45e21a70586d @guiocavalcanti guiocavalcanti committed Oct 10, 2011
View
@@ -1,2 +1,6 @@
Gemfile.lock
+*.swp
+*.swo
+.bundle
+bin
pkg/*
View
@@ -5,6 +5,8 @@ gem 'rake'
gemspec
group :development, :test do
+ gem "webmock"
+
unless ENV['TRAVIS']
gem 'ruby-debug', :platform => :mri_18
gem 'ruby-debug19', :platform => :mri_19, :require => 'ruby-debug'
@@ -5,7 +5,9 @@ module ActionMailer
class InlineCssHook
def self.delivering_email(message)
if html_part = (message.html_part || (message.content_type =~ /text\/html/ && message))
- premailer = ::Premailer.new(html_part.body.to_s, :with_html_string => true)
+ premailer = ::Premailer.new(html_part.body.to_s,
+ :with_html_string => true,
+ :base_url => message.header[:host].to_s)
existing_text_part = message.text_part && message.text_part.body.to_s
# Reset the body
message.body = nil
@@ -23,4 +25,3 @@ def self.delivering_email(message)
end
end
end
-
@@ -37,6 +37,7 @@
require 'action_mailer'
require 'action_mailer/test_case'
+require 'webmock/test_unit'
require 'actionmailer_inline_css'
silence_warnings do
@@ -26,7 +26,22 @@
</body>
</html>}
+TEST_HTML_WITH_HOST = %Q{
+<html>
+ <head>
+ <style>
+ #test { color: #123456; }
+ </style>
+ </head>
+ <body>
+ <img src="/images/test.png" />
+ </body>
+</html>
+}
+
class HelperMailer < ActionMailer::Base
+ default :host => "http://www.example.com/"
+
def use_inline_css_hook_with_only_html_part
mail_with_defaults do |format|
format.html { render(:inline => TEST_HTML) }
@@ -46,6 +61,12 @@ def use_inline_css_hook_with_utf_8
end
end
+ def inline_css_hook_with_base_url
+ mail_with_defaults do |format|
+ format.html { render(:inline => TEST_HTML_WITH_HOST) }
+ end
+ end
+
protected
def mail_with_defaults(&block)
@@ -75,5 +96,11 @@ def test_inline_css_hook_with_utf_8_characters
assert_match 'ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ', mail.html_part.body.encoded
assert_match 'ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ', mail.text_part.body.encoded
end
+
+ def test_inline_css_hook_with_base_url
+ mail = HelperMailer.inline_css_hook_with_base_url.deliver
+ assert_match '<img src="http://www.example.com/images/test.png">',
+ mail.html_part.body.encoded
+ end
end
@@ -3,6 +3,8 @@
ENV["RAILS_ASSET_ID"] = "123456"
class HelperMailer < ActionMailer::Base
+ default :host => "http://www.example.com/"
+
def use_stylesheet_link_tag
mail_with_defaults do |format|
format.html { render(:inline => %Q{
@@ -28,9 +30,10 @@ 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)
+ stub_request(:get, /example\.com\/*/).to_return do
+ css = "div.test { color: #119911; }"
+ { :status => 200, :body => css }
+ end
mail = HelperMailer.use_stylesheet_link_tag.deliver
assert_match "<div class=\"test\" style=\"color: #119911;\">", mail.html_part.body.encoded

0 comments on commit c54996a

Please sign in to comment.