lib/roadie/action_mailer_extensions.rb in roadie-2.3.4 vs lib/roadie/action_mailer_extensions.rb in roadie-2.4.0
- old
+ new
@@ -7,11 +7,15 @@
#
# 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
+ if base.method_defined?(:collect_responses)
+ alias_method_chain :collect_responses, :inline_styles
+ else
+ alias_method_chain :collect_responses_and_parts_order, :inline_styles
+ end
alias_method_chain :mail, :inline_styles
end
end
protected
@@ -20,15 +24,32 @@
@targets = headers[:css]
else
@targets = default_css_targets
end
+ if headers.has_key?(:after_inlining)
+ @after_inlining_handler = headers[:after_inlining]
+ else
+ @after_inlining_handler = default_after_inlining || Roadie.after_inlining_handler
+ end
+
mail_without_inline_styles(headers, &block).tap do |email|
- email.header.fields.delete_if { |field| field.name == 'css' }
+ email.header.fields.delete_if { |field| %w(css after_inlining).include?(field.name) }
end
end
+ # Rails 4
+ def collect_responses_with_inline_styles(headers, &block)
+ responses = collect_responses_without_inline_styles(headers, &block)
+ if Roadie.enabled?
+ responses.map { |response| inline_style_response(response) }
+ else
+ responses
+ end
+ end
+
+ # Rails 3
def collect_responses_and_parts_order_with_inline_styles(headers, &block)
responses, order = collect_responses_and_parts_order_without_inline_styles(headers, &block)
if Roadie.enabled?
[responses.map { |response| inline_style_response(response) }, order]
else
@@ -39,24 +60,32 @@
private
def default_css_targets
self.class.default[:css]
end
+ def default_after_inlining
+ self.class.default[:after_inlining]
+ end
+
+ def after_inlining_handler
+ @after_inlining_handler
+ end
+
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)
+ response.merge :body => Roadie.inline_css(Roadie.current_provider, css_targets, response[:body], url_options, after_inlining_handler)
else
response
end
end
def css_targets
Array.wrap(@targets || []).map { |target| resolve_target(target) }.compact.map(&:to_s)
end
def resolve_target(target)
- if target.respond_to? :bind
- target.bind(self).call
+ if target.kind_of? Proc
+ instance_exec(&target)
elsif target.respond_to? :call
target.call
else
target
end