actionmailer_inline_css 1.0.3 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -30
- data/actionmailer_inline_css.gemspec +1 -1
- data/lib/actionmailer_inline_css.rb +1 -2
- data/lib/overrides/premailer/premailer.rb +19 -0
- data/test/abstract_unit.rb +5 -1
- data/test/inline_css_hook_test.rb +26 -11
- data/test/premailer_stylesheet_link_tag_test.rb +39 -0
- metadata +13 -13
- data/lib/action_mailer/inline_css_helper.rb +0 -20
- data/test/inline_css_helper_test.rb +0 -29
data/README.md
CHANGED
@@ -54,40 +54,18 @@ To use this in your Rails app, simply add `gem "actionmailer_inline_css"` to you
|
|
54
54
|
(Having said that, it is recommended that you write your text templates by hand.)
|
55
55
|
|
56
56
|
|
57
|
-
###
|
57
|
+
### Including CSS in Mail Templates
|
58
58
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
<tt>embedded_style_tag(file = mailer.mailer_name)</tt>
|
63
|
-
|
64
|
-
* Embeds CSS loaded from a file, in a <tt>style</tt> tag.
|
65
|
-
* File can be given with or without .css extension
|
66
|
-
* Default path for mailer css files is <tt>public/stylesheets/mailers/#{mailer_name}.css</tt>
|
59
|
+
You can use the `stylesheet_link_tag` helper to add stylesheets to your mailer layouts.
|
60
|
+
<tt>actionmailer_inline_css</tt> contains a <tt>premailer</tt> override that properly handles
|
61
|
+
these CSS URIs.
|
67
62
|
|
68
63
|
#### Example
|
69
64
|
|
70
|
-
|
71
|
-
line to the `<head>` section of <tt>app/views/layouts/build_mailer.html.erb</tt>:
|
72
|
-
|
73
|
-
<%= embedded_style_tag %>
|
74
|
-
|
75
|
-
This will embed the CSS found at <tt>public/stylesheets/mailers/build_mailer.css</tt>.
|
76
|
-
|
77
|
-
--------------------------
|
78
|
-
|
79
|
-
If you wanted to include different CSS for each mail template, you could do something like this:
|
80
|
-
|
81
|
-
<% content_for 'head' do %>
|
82
|
-
<%= embedded_style_tag 'error_notification' %>
|
83
|
-
<% end %>
|
84
|
-
|
65
|
+
Add the following line to the `<head>` section of <tt>app/views/layouts/build_mailer.html.erb</tt>:
|
85
66
|
|
86
|
-
|
67
|
+
<%= stylesheet_link_tag 'mailers/build_mailer' %>
|
87
68
|
|
88
|
-
|
89
|
-
|
90
|
-
ERB templates contain HTML tags in plain text, so the output must be parsed.
|
91
|
-
On-the-fly CSS inlining could theoretically be built for Haml templates,
|
92
|
-
but it would be far too much effort for such a small use-case.
|
69
|
+
This will add a stylesheet link for <tt>/stylesheets/mailers/build_mailer.css</tt>.
|
70
|
+
Premailer will then inline the CSS from that file, and remove the link tag.
|
93
71
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "actionmailer_inline_css"
|
3
|
-
s.version = "1.0
|
3
|
+
s.version = "1.1.0"
|
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"
|
@@ -1,8 +1,7 @@
|
|
1
1
|
require 'premailer'
|
2
2
|
require 'nokogiri'
|
3
3
|
require 'action_mailer/inline_css_hook'
|
4
|
-
require '
|
4
|
+
require 'overrides/premailer/premailer'
|
5
5
|
|
6
6
|
ActionMailer::Base.register_interceptor ActionMailer::InlineCssHook
|
7
|
-
ActionMailer::Base.send :helper, ActionMailer::InlineCssHelper
|
8
7
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'premailer'
|
2
|
+
|
3
|
+
Premailer.class_eval do
|
4
|
+
protected
|
5
|
+
# When using the 'stylesheet_link_tag' helper in Rails, css URIs are given with
|
6
|
+
# a leading slash and a cache buster (e.g. ?12412422).
|
7
|
+
# This override handles these cases, while falling back to the default implementation.
|
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
|
10
|
+
if File.exist?(rails_path)
|
11
|
+
load_css_from_string(File.read(rails_path))
|
12
|
+
else
|
13
|
+
load_css_from_local_file_without_rails_path!(path)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
alias_method_chain :load_css_from_local_file!, :rails_path
|
17
|
+
|
18
|
+
end
|
19
|
+
|
data/test/abstract_unit.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
#
|
2
|
+
# Copied from rails/actionmailer/test/abstract_unit.rb
|
3
|
+
# --------------------------------------------------------------
|
4
|
+
|
1
5
|
# Pathname has a warning, so require it first while silencing
|
2
6
|
# warnings to shut it up.
|
3
7
|
#
|
@@ -79,7 +83,7 @@ class Rails
|
|
79
83
|
class << self
|
80
84
|
def root; self; end
|
81
85
|
def join(*args); self; end
|
82
|
-
def to_s; "
|
86
|
+
def to_s; "PATH"; end
|
83
87
|
end
|
84
88
|
end
|
85
89
|
|
@@ -1,11 +1,6 @@
|
|
1
1
|
require 'abstract_unit'
|
2
2
|
|
3
|
-
|
4
|
-
helper ActionMailer::InlineCssHelper
|
5
|
-
|
6
|
-
def use_inline_css_hook
|
7
|
-
mail_with_defaults do |format|
|
8
|
-
format.html { render(:inline => %Q{
|
3
|
+
TEST_HTML = %Q{
|
9
4
|
<html>
|
10
5
|
<head>
|
11
6
|
<style>
|
@@ -15,9 +10,19 @@ class HelperMailer < ActionMailer::Base
|
|
15
10
|
<body>
|
16
11
|
<div id="test">Test</div>
|
17
12
|
</body>
|
18
|
-
</html>
|
19
|
-
|
20
|
-
|
13
|
+
</html>}
|
14
|
+
|
15
|
+
class HelperMailer < ActionMailer::Base
|
16
|
+
def use_inline_css_hook_with_only_html_part
|
17
|
+
mail_with_defaults do |format|
|
18
|
+
format.html { render(:inline => TEST_HTML) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def use_inline_css_hook_with_text_and_html_parts
|
23
|
+
mail_with_defaults do |format|
|
24
|
+
format.html { render(:inline => TEST_HTML) }
|
25
|
+
format.text { render(:inline => "Different Text Part") }
|
21
26
|
end
|
22
27
|
end
|
23
28
|
|
@@ -29,10 +34,20 @@ class HelperMailer < ActionMailer::Base
|
|
29
34
|
end
|
30
35
|
end
|
31
36
|
|
37
|
+
|
32
38
|
class InlineCssHookTest < ActionMailer::TestCase
|
33
|
-
def
|
34
|
-
mail = HelperMailer.
|
39
|
+
def test_inline_css_hook_with_only_html_part
|
40
|
+
mail = HelperMailer.use_inline_css_hook_with_only_html_part.deliver
|
41
|
+
assert_match '<div id="test" style="color: #123456;">Test</div>', mail.html_part.body.encoded
|
42
|
+
# Test generated text part
|
43
|
+
assert_match 'Test', mail.text_part.body.encoded
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_inline_css_hook_with_text_and_html_parts
|
47
|
+
mail = HelperMailer.use_inline_css_hook_with_text_and_html_parts.deliver
|
35
48
|
assert_match '<div id="test" style="color: #123456;">Test</div>', mail.html_part.body.encoded
|
49
|
+
# Test specified text part
|
50
|
+
assert_match 'Different Text Part', mail.text_part.body.encoded
|
36
51
|
end
|
37
52
|
end
|
38
53
|
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'abstract_unit'
|
2
|
+
|
3
|
+
ENV["RAILS_ASSET_ID"] = "123456"
|
4
|
+
|
5
|
+
class HelperMailer < ActionMailer::Base
|
6
|
+
def use_stylesheet_link_tag
|
7
|
+
mail_with_defaults do |format|
|
8
|
+
format.html { render(:inline => %Q{
|
9
|
+
<html>
|
10
|
+
<head>
|
11
|
+
<%= stylesheet_link_tag 'mailers/mailer' %>
|
12
|
+
</head>
|
13
|
+
<body>
|
14
|
+
<div class="test">Test</div>
|
15
|
+
</body>
|
16
|
+
</html>
|
17
|
+
}) }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
protected
|
22
|
+
|
23
|
+
def mail_with_defaults(&block)
|
24
|
+
mail(:to => "test@localhost", :from => "tester@example.com",
|
25
|
+
:subject => "using helpers", &block)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class PremailerStylesheetLinkTagTest < ActionMailer::TestCase
|
30
|
+
def test_premailer_stylesheet_link_tag
|
31
|
+
css_file = "div.test { color: #119911; }"
|
32
|
+
File.stubs(:exist?).returns(true)
|
33
|
+
File.stubs(:read).returns(css_file)
|
34
|
+
|
35
|
+
mail = HelperMailer.use_stylesheet_link_tag.deliver
|
36
|
+
assert_match "<div class=\"test\" style=\"color: #119911;\">", mail.html_part.body.encoded
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
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.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-09-13 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionmailer
|
16
|
-
requirement: &
|
16
|
+
requirement: &23025380 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *23025380
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: premailer
|
27
|
-
requirement: &
|
27
|
+
requirement: &23024840 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 1.7.1
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *23024840
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: nokogiri
|
38
|
-
requirement: &
|
38
|
+
requirement: &23024260 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.4.4
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *23024260
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: mocha
|
49
|
-
requirement: &
|
49
|
+
requirement: &23023760 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: 0.10.0
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *23023760
|
58
58
|
description: Module for ActionMailer to improve the rendering of HTML emails by using
|
59
59
|
the 'premailer' gem, which inlines CSS and makes relative links absolute.
|
60
60
|
email: nathan.f77@gmail.com
|
@@ -67,12 +67,12 @@ files:
|
|
67
67
|
- README.md
|
68
68
|
- Rakefile
|
69
69
|
- actionmailer_inline_css.gemspec
|
70
|
-
- lib/action_mailer/inline_css_helper.rb
|
71
70
|
- lib/action_mailer/inline_css_hook.rb
|
72
71
|
- lib/actionmailer_inline_css.rb
|
72
|
+
- lib/overrides/premailer/premailer.rb
|
73
73
|
- test/abstract_unit.rb
|
74
|
-
- test/inline_css_helper_test.rb
|
75
74
|
- test/inline_css_hook_test.rb
|
75
|
+
- test/premailer_stylesheet_link_tag_test.rb
|
76
76
|
homepage: http://premailer.dialect.ca/
|
77
77
|
licenses: []
|
78
78
|
post_install_message:
|
@@ -87,7 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
87
|
version: '0'
|
88
88
|
segments:
|
89
89
|
- 0
|
90
|
-
hash:
|
90
|
+
hash: 4032464049642912323
|
91
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
92
|
none: false
|
93
93
|
requirements:
|
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
96
|
version: '0'
|
97
97
|
segments:
|
98
98
|
- 0
|
99
|
-
hash:
|
99
|
+
hash: 4032464049642912323
|
100
100
|
requirements: []
|
101
101
|
rubyforge_project:
|
102
102
|
rubygems_version: 1.8.8
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module ActionMailer
|
2
|
-
module InlineCssHelper
|
3
|
-
# Embed CSS loaded from a file in a 'style' tag.
|
4
|
-
# CSS file can be given with or without .css extension,
|
5
|
-
# and will be searched for in "#{Rails.root}/public/stylesheets/mailers" by default.
|
6
|
-
def embedded_style_tag(file = mailer.mailer_name)
|
7
|
-
['.css', ''].each do |ext|
|
8
|
-
[Rails.root.join("public", "stylesheets", "mailers"), Rails.root].each do |parent_path|
|
9
|
-
guessed_path = parent_path.join(file+ext).to_s
|
10
|
-
if File.exist?(guessed_path)
|
11
|
-
return content_tag(:style, File.read(guessed_path), {:type => "text/css"}, false)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
nil
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'abstract_unit'
|
2
|
-
|
3
|
-
class HelperMailer < ActionMailer::Base
|
4
|
-
helper ActionMailer::InlineCssHelper
|
5
|
-
|
6
|
-
def use_embedded_style_tag
|
7
|
-
mail_with_defaults do |format|
|
8
|
-
format.html { render(:inline => "<%= embedded_style_tag %>") }
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
protected
|
13
|
-
|
14
|
-
def mail_with_defaults(&block)
|
15
|
-
mail(:to => "test@localhost", :from => "tester@example.com",
|
16
|
-
:subject => "using helpers", &block)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
class InlineCssHelperTest < ActionMailer::TestCase
|
21
|
-
def test_embedded_style_tag
|
22
|
-
css = "body { display: none; }"
|
23
|
-
File.stubs(:exist?).returns(true)
|
24
|
-
File.stubs(:read).returns(css)
|
25
|
-
mail = HelperMailer.use_embedded_style_tag
|
26
|
-
assert_match "<style type=\"text/css\">#{css}</style>", mail.body.encoded
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|