module Roadie::ActionMailerExtensions

def self.included(base)

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

def collect_responses_and_parts_order_with_inline_styles(headers, &block)

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

def css_targets

def css_targets
  Array.wrap(@targets || []).map { |target| resolve_target(target) }.compact.map(&:to_s)
end

def default_css_targets

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

def inline_style_response(response)

def inline_style_response(response)
  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 mail_with_inline_styles(headers = {}, &block)

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 resolve_target(target)

def resolve_target(target)
  if target.respond_to? :call
    target.call
  else
    target
  end
end