From 98a340849c198fad49fedb965812f139494676d3 Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Sun, 11 Sep 2011 00:50:35 +0800 Subject: [PATCH] Reorganized Actionmailer inline CSS code. Use sensible default path for mailer stylesheets. Module is small enough to live in initializer. --- config/initializers/actionmailer_inline_css.rb | 18 ++++++++++++++++++ config/initializers/inline_css.rb | 2 -- lib/inline_css.rb | 18 ------------------ public/stylesheets/email.css | 55 ------------------------------------------------------- public/stylesheets/mailers/mailer.css | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ spec/mailers/mailer_spec.rb | 10 +++++++++- 6 files changed, 82 insertions(+), 76 deletions(-) create mode 100644 config/initializers/actionmailer_inline_css.rb delete mode 100644 config/initializers/inline_css.rb delete mode 100644 lib/inline_css.rb delete mode 100644 public/stylesheets/email.css create mode 100644 public/stylesheets/mailers/mailer.css diff --git a/config/initializers/actionmailer_inline_css.rb b/config/initializers/actionmailer_inline_css.rb new file mode 100644 index 0000000..8263568 --- /dev/null +++ b/config/initializers/actionmailer_inline_css.rb @@ -0,0 +1,18 @@ +# +# ActionMailer module for inline css in html emails +# +module ActionMailer::InlineCss + def render(*args) + if (template = args.first[:template]) && template.mime_type.html? + premailer = Premailer.new(super, :with_html_string => true, + :css => default_params[:css] || + ["#{Rails.root}/public/stylesheets/mailers/#{mailer_name}.css"]) + premailer.to_inline_css + else + super + end + end +end + +ActionMailer::Base.send :include, ActionMailer::InlineCss + diff --git a/config/initializers/inline_css.rb b/config/initializers/inline_css.rb deleted file mode 100644 index 61a80e4..0000000 --- a/config/initializers/inline_css.rb +++ /dev/null @@ -1,2 +0,0 @@ -require 'inline_css' - diff --git a/lib/inline_css.rb b/lib/inline_css.rb deleted file mode 100644 index ff0d5b5..0000000 --- a/lib/inline_css.rb +++ /dev/null @@ -1,18 +0,0 @@ -# -# module for ActionMailer to inline css in html emails -# -module InlineCss - def render(*args) - if (template = args.first[:template]) && template.mime_type.html? - premailer = Premailer.new(super, - :with_html_string => true, - :css => [Rails.root.join("public/stylesheets/email.css").to_s]) - premailer.to_inline_css - else - super - end - end -end - -ActionMailer::Base.send :include, InlineCss - diff --git a/public/stylesheets/email.css b/public/stylesheets/email.css deleted file mode 100644 index d2afbb2..0000000 --- a/public/stylesheets/email.css +++ /dev/null @@ -1,55 +0,0 @@ -td.header { - padding: 10px 20px 8px 20px; - height: 70px; - background-color: #11112f; - text-align: left; - border-bottom: 1px solid #ccccee; -} - td.header a { - display: block; - height: 31px; - width: 88px; - margin-top: 4px; - } - /* Style 'Errbit' logo alt text if image cannot be loaded. */ - td.header a img { - border: none; - color: #E3E3E3; - font-family: helvetica; - font-size: 30px; - font-weight: bold; - min-height: 31px; - text-shadow: 0 1px 0 #EEEEFF; - } - -td.section, td.content, td.footer { - font-family: Helvetica,Arial,sans-serif; - font-size: 14px; - background-color: #ffffff; - text-align: left; -} -td.section { - padding: 0; - border-bottom: 1px solid #dddddd; -} -td.content { - padding: 20px 20px 10px 20px; - line-height: 1.3em; -} -td.footer { - padding: 10px 20px 20px 20px; - font-size: 11px; - font-weight: bold; - color: #666666; -} - -a.bold, span.bold { font-weight: bold; } - -p { margin: 0 0 15px 0; } -p.heading { - color: #6a6a6a; - margin-bottom: 4px; -} -p.monospace, p.backtrace { font-family: monospace; } -p.backtrace { margin-bottom: 2px; } - diff --git a/public/stylesheets/mailers/mailer.css b/public/stylesheets/mailers/mailer.css new file mode 100644 index 0000000..d2afbb2 --- /dev/null +++ b/public/stylesheets/mailers/mailer.css @@ -0,0 +1,55 @@ +td.header { + padding: 10px 20px 8px 20px; + height: 70px; + background-color: #11112f; + text-align: left; + border-bottom: 1px solid #ccccee; +} + td.header a { + display: block; + height: 31px; + width: 88px; + margin-top: 4px; + } + /* Style 'Errbit' logo alt text if image cannot be loaded. */ + td.header a img { + border: none; + color: #E3E3E3; + font-family: helvetica; + font-size: 30px; + font-weight: bold; + min-height: 31px; + text-shadow: 0 1px 0 #EEEEFF; + } + +td.section, td.content, td.footer { + font-family: Helvetica,Arial,sans-serif; + font-size: 14px; + background-color: #ffffff; + text-align: left; +} +td.section { + padding: 0; + border-bottom: 1px solid #dddddd; +} +td.content { + padding: 20px 20px 10px 20px; + line-height: 1.3em; +} +td.footer { + padding: 10px 20px 20px 20px; + font-size: 11px; + font-weight: bold; + color: #666666; +} + +a.bold, span.bold { font-weight: bold; } + +p { margin: 0 0 15px 0; } +p.heading { + color: #6a6a6a; + margin-bottom: 4px; +} +p.monospace, p.backtrace { font-family: monospace; } +p.backtrace { margin-bottom: 2px; } + diff --git a/spec/mailers/mailer_spec.rb b/spec/mailers/mailer_spec.rb index 296504b..8777ea3 100644 --- a/spec/mailers/mailer_spec.rb +++ b/spec/mailers/mailer_spec.rb @@ -5,11 +5,19 @@ describe Mailer do include EmailSpec::Helpers include EmailSpec::Matchers - it "should html-escape the notice's message for the html part" do + before do @notice = Factory(:notice, :message => "class < ActionController::Base") @email = Mailer.err_notification(@notice) + end + + it "should html-escape the notice's message for the html part" do @email.should have_body_text("class < ActionController::Base") end + + it "should inline css" do + @email.should have_body_text '