Class: ActionMailer::InStyle::Premailer

Inherits:
Premailer
  • Object
show all
Defined in:
lib/action_mailer/in_style/premailer.rb

Instance Method Summary collapse

Instance Method Details

#load_css_from_asset_pipeline!Object

Uses sprockets and the Rails ~>3.1 asset pipeline to load the compiled CSS from stylesheet links within the header of the email template.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/action_mailer/in_style/premailer.rb', line 14

def load_css_from_asset_pipeline!
  if tags = @doc.search("link[@rel='stylesheet'], style")
    tags.each do |tag|
      if tag.to_s.strip =~ /^\<link/i && tag.attributes['href'] && media_type_ok?(tag.attributes['media'])

        # If the stylesheet link begins with /assets we must be using the asset pipeline
        # to generate the css. In this case we should be able to retrieve the css directly
        # from sprockets
        if tag.attributes['href'].to_s =~ /\/assets\//
          link_uri = process_stylesheet_url(tag.attributes['href'])
          asset = Rails.application.assets.find_asset(link_uri)
          @css_parser.add_block!(asset.body) if asset
        end
      end
    end
  end
end

#load_css_from_html!Object



6
7
8
9
# File 'lib/action_mailer/in_style/premailer.rb', line 6

def load_css_from_html!
  load_css_from_asset_pipeline!
  super
end

#process_stylesheet_url(href) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/action_mailer/in_style/premailer.rb', line 32

def process_stylesheet_url(href)
  # Example stylesheet url: email-1bd4488901bfc2f2ccf4f044fc9154a6.css
  href = href.to_s.sub(/\?[0-9a-zA-Z]+$/, '').sub(/^\/assets\//, '')

  if href =~ /(\w+)\-(?:[0-9a-zA-Z]{32})\.([a-z]{3})/
    href = href.gsub(/(\w+)\-(?:[0-9a-zA-Z]{32})\.([a-z]{3})/, [$1,$2].join('.'))
  end

  return href
end