diff --git a/README.md b/README.md index 87d39c7..5029dab 100644 --- a/README.md +++ b/README.md @@ -256,18 +256,13 @@ end ## Known issues ## -Roadie will not be able to find your stylesheets if you have an `asset_host` configured and will ignore those lines when inlining. - -A workaround for this is to not use `asset_host` in your mailers: +If you need to set an `asset_host` in your mailer, it must be done before `Roadie::Rails::Automatic` is `include`d. For example: ```ruby -config.action_controller.asset_host = # ... -config.action_mailer.asset_host = nil - -# or - class MyMailer < ActionMailer::Base - self.asset_host = nil + self.asset_host = 'http://mail-cdn.mysite.com' + + include Roadie::Rails::Automatic end ``` diff --git a/lib/roadie/rails.rb b/lib/roadie/rails.rb index ea87204..38cb464 100644 --- a/lib/roadie/rails.rb +++ b/lib/roadie/rails.rb @@ -3,6 +3,8 @@ module Rails end end +require "active_support/concern" + require "roadie" require "roadie/rails/version" @@ -12,6 +14,7 @@ module Rails require "roadie/rails/mail_inliner" require "roadie/rails/asset_pipeline_provider" +require "roadie/rails/css_localizer" require "roadie/rails/mailer" diff --git a/lib/roadie/rails/automatic.rb b/lib/roadie/rails/automatic.rb index b386838..ee1490a 100644 --- a/lib/roadie/rails/automatic.rb +++ b/lib/roadie/rails/automatic.rb @@ -1,6 +1,12 @@ module Roadie module Rails module Automatic + extend ActiveSupport::Concern + + included do + include CssLocalizer + end + def mail(*args, &block) super.tap do |email| email.extend InlineOnDelivery diff --git a/lib/roadie/rails/css_localizer.rb b/lib/roadie/rails/css_localizer.rb new file mode 100644 index 0000000..db185ee --- /dev/null +++ b/lib/roadie/rails/css_localizer.rb @@ -0,0 +1,36 @@ +module Roadie + module Rails + module CssLocalizer + extend ActiveSupport::Concern + + included do + old_asset_host = self.asset_host + if old_asset_host + # don't include an asset_host for CSS files, so that they are picked up by Roadie + self.asset_host = Proc.new { |source, request| + uri = URI::parse(source) + if uri.path.end_with?('.css') + nil + else + # logic copied from Rails + # http://git.io/vhYx7Q + if old_asset_host.respond_to?(:call) + # the original asset_host was a Proc + arity = old_asset_host.respond_to?(:arity) ? old_asset_host.arity : old_asset_host.method(:call).arity + args = [source] + args << request if request && (arity > 1 || arity < 0) + old_asset_host.call(*args) + elsif old_asset_host =~ /%d/ + # the original asset_host was a string value that needs the filename checksum added + old_asset_host % (Zlib.crc32(source) % 4) + else + # the original asset_host was a simple string value + old_asset_host + end + end + } + end + end + end + end +end diff --git a/lib/roadie/rails/mailer.rb b/lib/roadie/rails/mailer.rb index 449137c..02bc296 100644 --- a/lib/roadie/rails/mailer.rb +++ b/lib/roadie/rails/mailer.rb @@ -1,6 +1,12 @@ module Roadie module Rails module Mailer + extend ActiveSupport::Concern + + included do + include CssLocalizer + end + def roadie_mail(options = {}, &block) email = mail(options, &block) MailInliner.new(email, roadie_options).execute diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index 6ebdf42..4a63bfb 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -76,6 +76,29 @@ def parse_html_in_email(mail) email.deliver end + it "sets the proper host for images with automatic mailer" do + email = app.read_delivered_email(:asset_email) + + html = email.html_part.body.decoded + expect(html).to include ' + +
+ + <%= stylesheet_link_tag "email" %> + + +