Skip to content

Commit

Permalink
Removed 'embedded_style_tag' helper, in favour of 'stylesheet_link_tag'
Browse files Browse the repository at this point in the history
  • Loading branch information
ndbroadbent committed Sep 13, 2011
1 parent 3fe744b commit 23b48ae
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 74 deletions.
29 changes: 9 additions & 20 deletions README.md
Expand Up @@ -54,29 +54,18 @@ To use this in your Rails app, simply add `gem "actionmailer_inline_css"` to you
(Having said that, it is recommended that you write your text templates by hand.)


### Additional Helpers
### Including CSS in Mail Templates

Rails does not provide any helpers for adding 'embedded' CSS to your layouts (and rightly so!),
but the following helper is now available for your mail layouts:
You can use the `stylesheet_link_tag` helper to add stylesheets to your mailer layouts.
<tt>actionmailer_inline_css</tt> contains a <tt>premailer</tt> override that properly handles
these CSS URIs.

<tt>embedded_style_tag(file = mailer.mailer_name)</tt>
#### Example

* Embeds CSS loaded from a file, in a <tt>style</tt> tag.
* File can be given with or without .css extension
* Default path for mailer css files is <tt>public/stylesheets/mailers/#{mailer_name}.css</tt>
Add the following line to the `<head>` section of <tt>app/views/layouts/build_mailer.html.erb</tt>:

#### Examples
<%= stylesheet_link_tag 'mailers/build_mailer' %>

Given a mailer called <tt>BuildMailer</tt>, you can add the following
line to the `<head>` section of <tt>app/views/layouts/build_mailer.html.erb</tt>:

<%= embedded_style_tag %>

This will embed the CSS found at <tt>public/stylesheets/mailers/build_mailer.css</tt>.

--------------------------

If you use Sass on Heroku with [hassle](https://github.com/Papipo/hassle):

<%= embedded_style_tag 'tmp/hassle/stylesheets/mailers/build_mailer.css' %>
This will add a stylesheet link for <tt>/stylesheets/mailers/build_mailer.css</tt>.
Premailer will then inline the CSS from that file, and remove the link tag.

20 changes: 0 additions & 20 deletions lib/action_mailer/inline_css_helper.rb

This file was deleted.

3 changes: 1 addition & 2 deletions lib/actionmailer_inline_css.rb
@@ -1,8 +1,7 @@
require 'premailer'
require 'nokogiri'
require 'action_mailer/inline_css_hook'
require 'action_mailer/inline_css_helper'
require 'overrides/premailer/premailer'

ActionMailer::Base.register_interceptor ActionMailer::InlineCssHook
ActionMailer::Base.send :helper, ActionMailer::InlineCssHelper

19 changes: 19 additions & 0 deletions lib/overrides/premailer/premailer.rb
@@ -0,0 +1,19 @@
require 'premailer'

Premailer.class_eval do
protected
# When using the 'stylesheet_link_tag' helper in Rails, css URIs are given with
# 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
if File.exist?(rails_path)
load_css_from_string(File.read(rails_path))
else
load_css_from_local_file_without_rails_path!(path)
end
end
alias_method_chain :load_css_from_local_file!, :rails_path

end

2 changes: 1 addition & 1 deletion test/abstract_unit.rb
Expand Up @@ -83,7 +83,7 @@ class Rails
class << self
def root; self; end
def join(*args); self; end
def to_s; "path"; end
def to_s; "PATH"; end
end
end

Expand Down
29 changes: 0 additions & 29 deletions test/inline_css_helper_test.rb

This file was deleted.

2 changes: 0 additions & 2 deletions test/inline_css_hook_test.rb
Expand Up @@ -13,8 +13,6 @@
</html>}

class HelperMailer < ActionMailer::Base
helper ActionMailer::InlineCssHelper

def use_inline_css_hook_with_only_html_part
mail_with_defaults do |format|
format.html { render(:inline => TEST_HTML) }
Expand Down
39 changes: 39 additions & 0 deletions test/premailer_stylesheet_link_tag_test.rb
@@ -0,0 +1,39 @@
require 'abstract_unit'

ENV["RAILS_ASSET_ID"] = "123456"

class HelperMailer < ActionMailer::Base
def use_stylesheet_link_tag
mail_with_defaults do |format|
format.html { render(:inline => %Q{
<html>
<head>
<%= stylesheet_link_tag 'mailers/mailer' %>
</head>
<body>
<div class="test">Test</div>
</body>
</html>
}) }
end
end

protected

def mail_with_defaults(&block)
mail(:to => "test@localhost", :from => "tester@example.com",
:subject => "using helpers", &block)
end
end

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)

mail = HelperMailer.use_stylesheet_link_tag.deliver
assert_match "<div class=\"test\" style=\"color: #119911;\">", mail.html_part.body.encoded
end
end

0 comments on commit 23b48ae

Please sign in to comment.