actionmailer_inline_css 1.5.2 → 1.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
+ .rvmrc
2
+ .idea
1
3
  Gemfile.lock
2
4
  *.swp
3
5
  *.swo
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "actionmailer_inline_css"
3
- s.version = "1.5.2"
3
+ s.version = "1.5.3"
4
4
  s.date = Time.now.strftime('%Y-%m-%d')
5
5
  s.summary = "Always send HTML e-mails with inline CSS, using the 'premailer' gem"
6
6
  s.email = "nathan.f77@gmail.com"
@@ -4,38 +4,40 @@
4
4
  module ActionMailer
5
5
  class InlineCssHook
6
6
  def self.delivering_email(message)
7
+
7
8
  if html_part = (message.html_part || (message.content_type =~ /text\/html/ && message))
8
- # Generate an email with all CSS inlined (access CSS a FS path)
9
- premailer = ::Premailer.new(html_part.body.to_s,
10
- :with_html_string => true)
11
-
12
- # Prepend host to remaning URIs.
13
- # Two-phase conversion to avoid request deadlock from dev. server (Issue #4)
14
- premailer = ::Premailer.new(premailer.to_inline_css,
15
- :with_html_string => true,
16
- :base_url => message.header[:host].to_s)
17
- existing_text_part = message.text_part && message.text_part.body.to_s
18
- existing_attachments = message.attachments
9
+ host = ActionMailerInlineCss.base_url || message.header[:host].to_s
10
+
11
+ # Generate an email with all CSS inlined (access CSS a FS path), and URIs
12
+ premailer = ::Premailer.new(html_part.body.to_s, :with_html_string => true, :base_url => host)
13
+
19
14
  msg_charset = message.charset
20
15
 
21
- # Reset the body
22
- message.body = nil
23
- message.body.instance_variable_set("@parts", Mail::PartsList.new)
16
+ if message.text_part && message.text_part.body.to_s
17
+ html_part.content_type "text/html; charset=#{msg_charset}"
18
+ html_part.body premailer.to_inline_css
19
+ else
20
+ existing_attachments = message.attachments
24
21
 
25
- # Add an HTML part with CSS inlined.
26
- message.html_part do
27
- content_type "text/html; charset=#{msg_charset}"
28
- body premailer.to_inline_css
29
- end
22
+ # Clear body to make a multipart email
23
+ message.body = nil
30
24
 
31
- # Add a text part with either the pre-existing text part, or one generated with premailer.
32
- message.text_part do
33
- content_type "text/plain; charset=#{msg_charset}"
34
- body existing_text_part || premailer.to_plain_text
35
- end
25
+ # IMPORTANT: Plain text part must be generated before CSS is inlined.
26
+ # Not doing so results in CSS declarations (<style>) visible in the plain text part.
27
+ message.text_part = Mail::Part.new do
28
+ content_type "text/plain; charset=#{msg_charset}"
29
+ body premailer.to_plain_text
30
+ end
31
+
32
+ message.html_part = Mail::Part.new do
33
+ content_type "text/html; charset=#{msg_charset}"
34
+ body premailer.to_inline_css
35
+ end
36
36
 
37
- # Re-add any attachments
38
- existing_attachments.each {|a| message.body << a }
37
+ message.content_type 'multipart/mixed' if ! existing_attachments.empty?
38
+
39
+ existing_attachments.each {|a| message.body << a }
40
+ end
39
41
 
40
42
  message
41
43
  end
@@ -6,3 +6,7 @@ require 'overrides/premailer/adapter/nokogiri'
6
6
 
7
7
  ActionMailer::Base.register_interceptor ActionMailer::InlineCssHook
8
8
 
9
+ module ActionMailerInlineCss
10
+ mattr_accessor :base_url
11
+ end
12
+
@@ -6,9 +6,13 @@ Premailer.class_eval do
6
6
  # a leading slash and a cache buster (e.g. ?12412422).
7
7
  # This override handles these cases, while falling back to the default implementation.
8
8
  def load_css_from_local_file_with_rails_path!(path)
9
- rails_path = Rails.root.join("public", path.sub(/\?[0-9a-zA-Z]+$/, '').sub(/^\//, '')).to_s
9
+ # Remove query string and the path
10
+ clean_path = path.sub(/\?.*$/, '').sub(%r(^https?://[^/]*/), '')
11
+ rails_path = Rails.root.join('public', clean_path)
10
12
  if File.exist?(rails_path)
11
13
  load_css_from_string(File.read(rails_path))
14
+ elsif (asset = Rails.application.assets.find_asset(clean_path.sub("#{Rails.configuration.assets.prefix}/", '')))
15
+ load_css_from_string(asset.source)
12
16
  else
13
17
  load_css_from_local_file_without_rails_path!(path)
14
18
  end
@@ -58,8 +58,7 @@ class HelperMailer < ActionMailer::Base
58
58
 
59
59
  def use_inline_css_hook_with_utf_8
60
60
  mail_with_defaults do |format|
61
- charset "utf8"
62
- format.html { render(:inline => TEST_HTML_UTF8) }
61
+ format.html(:charset => "utf8") { render(:inline => TEST_HTML_UTF8) }
63
62
  end
64
63
  end
65
64
 
@@ -130,4 +129,15 @@ class InlineCssHookTest < ActionMailer::TestCase
130
129
  assert_equal original_hello_attachment_url, mail.attachments["hello"].url
131
130
  end
132
131
 
132
+ def test_alternative_content_type
133
+ mail = HelperMailer.use_inline_css_hook_with_text_and_html_parts.deliver
134
+ assert_match /multipart\/alternative/, mail.content_type
135
+ end
136
+
137
+ def test_mixed_content_type
138
+ File.stubs(:read).returns("world")
139
+ mail = HelperMailer.with_attachment
140
+ m = ActionMailer::InlineCssHook.delivering_email(mail.deliver)
141
+ assert_equal "multipart/mixed", m.content_type
142
+ end
133
143
  end
@@ -30,12 +30,12 @@ end
30
30
 
31
31
  class PremailerStylesheetLinkTagTest < ActionMailer::TestCase
32
32
  def test_premailer_stylesheet_link_tag
33
- css_file = "div.test { color: #119911; }"
34
33
  File.stubs(:exist?).returns(true)
35
- File.stubs(:read).returns(css_file)
34
+ File.stubs(:exists?).returns(true)
35
+ File.stubs(:read).returns("div.test { color: #119911; }")
36
36
 
37
37
  mail = HelperMailer.use_stylesheet_link_tag.deliver
38
- assert_match "<div class=\"test\" style=\"color: #119911;\">", mail.html_part.body.encoded
38
+ assert_match %Q{<div class="test" style="color: #119911;">}, mail.html_part.body.encoded
39
39
  end
40
40
  end
41
41
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actionmailer_inline_css
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-08 00:00:00.000000000Z
12
+ date: 2013-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionmailer
16
- requirement: &19144080 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *19144080
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: premailer
27
- requirement: &19143300 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: 1.7.1
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *19143300
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.7.1
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: nokogiri
38
- requirement: &19142740 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: 1.4.4
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *19142740
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.4.4
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: mocha
49
- requirement: &19142140 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: 0.10.0
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *19142140
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 0.10.0
58
78
  description: Module for ActionMailer to improve the rendering of HTML emails by using
59
79
  the 'premailer' gem, which inlines CSS and makes relative links absolute.
60
80
  email: nathan.f77@gmail.com
@@ -65,6 +85,7 @@ files:
65
85
  - .gitignore
66
86
  - .travis.yml
67
87
  - Gemfile
88
+ - LICENSE
68
89
  - README.md
69
90
  - Rakefile
70
91
  - actionmailer_inline_css.gemspec
@@ -95,8 +116,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
116
  version: '0'
96
117
  requirements: []
97
118
  rubyforge_project:
98
- rubygems_version: 1.8.8
119
+ rubygems_version: 1.8.24
99
120
  signing_key:
100
121
  specification_version: 3
101
122
  summary: Always send HTML e-mails with inline CSS, using the 'premailer' gem
102
123
  test_files: []
124
+ has_rdoc: false