Sha256: b77e9e26d3a4ba8db9e2861b65e9d4c08fe799f233f6ac623ad0f3825bc90888

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

require 'uri'
require 'nokogiri'
require 'css_parser'

module Roadie
  # This module adds the Roadie functionality to ActionMailer 3 when included in ActionMailer::Base.
  #
  # If you want to add Roadie to any other mail framework, take a look at how this module is implemented.
  module ActionMailerExtensions
    def self.included(base)
      base.class_eval do
        alias_method_chain :collect_responses_and_parts_order, :inline_styles
        alias_method_chain :mail, :inline_styles
      end
    end

    protected
      def mail_with_inline_styles(headers = {}, &block)
        if headers.has_key?(:css)
          @targets = headers[:css]
        else
          @targets = default_css_targets
        end

        mail_without_inline_styles(headers, &block).tap do |email|
          email.header.fields.delete_if { |field| field.name == 'css' }
        end
      end

      def collect_responses_and_parts_order_with_inline_styles(headers, &block)
        responses, order = collect_responses_and_parts_order_without_inline_styles(headers, &block)
        [responses.map { |response| inline_style_response(response) }, order]
      end

    private
      def default_css_targets
        self.class.default[:css]
      end

      def inline_style_response(response)
        url_options = Rails.application.config.action_mailer.default_url_options
        if response[:content_type] == 'text/html'
          response.merge :body => Roadie.inline_css(Roadie.current_provider, css_targets, response[:body], url_options)
        else
          response
        end
      end

      def css_targets
        return [] unless @targets
        Array.wrap(@targets || []).map { |target| target.to_s }
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
roadie-2.1.0 lib/roadie/action_mailer_extensions.rb
roadie-2.1.0.pre2 lib/roadie/action_mailer_extensions.rb