Move all render and layout pieces required in ActionMailer from ActionController to AbstractController.
This commit is contained in:
parent
684c2dc208
commit
43d5504f0a
|
@ -376,13 +376,19 @@ module ActionMailer #:nodoc:
|
|||
# The mail object instance referenced by this mailer.
|
||||
attr_reader :mail
|
||||
attr_reader :template_name, :default_template_name, :action_name
|
||||
attr_internal :response_body
|
||||
|
||||
def controller_path
|
||||
self.class.controller_path
|
||||
end
|
||||
|
||||
def formats
|
||||
@template.formats
|
||||
[:"*/*"]
|
||||
end
|
||||
|
||||
# Refactor out all mailer_name
|
||||
def _prefix
|
||||
mailer_name
|
||||
end
|
||||
|
||||
class << self
|
||||
|
@ -468,16 +474,20 @@ module ActionMailer #:nodoc:
|
|||
# Initialize the mailer via the given +method_name+. The body will be
|
||||
# rendered and a new TMail::Mail object created.
|
||||
def create!(method_name, *parameters) #:nodoc:
|
||||
ActiveSupport::Notifications.instrument(:create_mail, :name => method_name) do
|
||||
initialize_defaults(method_name)
|
||||
__send__(method_name, *parameters)
|
||||
|
||||
# Check if render was called.
|
||||
@body = self.response_body if @body.is_a?(Hash) && @body.empty?
|
||||
|
||||
# If an explicit, textual body has not been set, we check assumptions.
|
||||
unless String === @body
|
||||
# TODO Hax
|
||||
# TODO Fix me. Deprecate assigns to be given as a :body hash
|
||||
if @body.is_a?(Hash)
|
||||
@body.each do |k, v|
|
||||
instance_variable_set(:"@#{k}", v)
|
||||
end
|
||||
end
|
||||
|
||||
# First, we look to see if there are any likely templates that match,
|
||||
# which include the content-type in their file name (i.e.,
|
||||
|
@ -489,7 +499,7 @@ module ActionMailer #:nodoc:
|
|||
:content_type => template.mime_type ? template.mime_type.to_s : "text/plain",
|
||||
:disposition => "inline",
|
||||
:charset => charset,
|
||||
:body => render_to_string(:_template => template)
|
||||
:body => render_to_body(:_template => template)
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -528,16 +538,13 @@ module ActionMailer #:nodoc:
|
|||
@mail = create_mail
|
||||
end
|
||||
|
||||
@mail
|
||||
end
|
||||
|
||||
# Delivers a TMail::Mail object. By default, it delivers the cached mail
|
||||
# object (from the <tt>create!</tt> method). If no cached mail object exists, and
|
||||
# no alternate has been given as the parameter, this will fail.
|
||||
def deliver!(mail = @mail)
|
||||
raise "no mail object available for delivery!" unless mail
|
||||
|
||||
unless logger.nil?
|
||||
if logger
|
||||
logger.info "Sent mail to #{Array(recipients).join(', ')}"
|
||||
logger.debug "\n#{mail.encoded}"
|
||||
end
|
||||
|
@ -571,45 +578,16 @@ module ActionMailer #:nodoc:
|
|||
@sent_on ||= Time.now
|
||||
end
|
||||
|
||||
def _determine_template(options)
|
||||
def render(*args)
|
||||
# TODO Fix me. Deprecate assigns to be given as a :body hash
|
||||
options = args.last.is_a?(Hash) ? args.last : {}
|
||||
if options[:body]
|
||||
options.delete(:body).each do |k, v|
|
||||
instance_variable_set(:"@#{k}", v)
|
||||
end
|
||||
end
|
||||
|
||||
super
|
||||
layout = options.key?(:layout) ? options[:layout] : :default
|
||||
options[:_layout] = _layout_for_option(layout, options[:_template].details)
|
||||
end
|
||||
|
||||
def render_message(method_name, body)
|
||||
render :file => method_name, :body => body
|
||||
ensure
|
||||
@current_template_content_type = nil
|
||||
end
|
||||
|
||||
def render(opts)
|
||||
file = opts[:file]
|
||||
opts[:locals] ||= {}
|
||||
|
||||
@template = initialize_template_class(opts.delete(:body))
|
||||
|
||||
if file
|
||||
prefix = mailer_name unless file =~ /\//
|
||||
template = view_paths.find(file, {:formats => formats}, prefix)
|
||||
end
|
||||
|
||||
layout = opts.key?(:layout) ? opts.delete(:layout) : :default
|
||||
layout = _layout_for_option(layout, :formats => formats)
|
||||
|
||||
if template
|
||||
@template._render_template(template, layout, opts)
|
||||
elsif inline = opts[:inline]
|
||||
@template._render_inline(inline, layout, opts)
|
||||
end
|
||||
end
|
||||
|
||||
def default_template_format
|
||||
if @current_template_content_type
|
||||
Mime::Type.lookup(@current_template_content_type).to_sym
|
||||
else
|
||||
:html
|
||||
end
|
||||
end
|
||||
|
||||
def template_root
|
||||
|
@ -624,12 +602,6 @@ module ActionMailer #:nodoc:
|
|||
"#{mailer_name}"
|
||||
end
|
||||
|
||||
def initialize_template_class(assigns)
|
||||
template = ActionView::Base.new(self.class.view_paths, assigns, self)
|
||||
template.formats = [default_template_format]
|
||||
template
|
||||
end
|
||||
|
||||
def sort_parts(parts, order = [])
|
||||
order = order.collect { |s| s.downcase }
|
||||
|
||||
|
|
|
@ -12,7 +12,15 @@ class RenderMailer < ActionMailer::Base
|
|||
recipients recipient
|
||||
subject "using helpers"
|
||||
from "tester@example.com"
|
||||
body render(:file => "signed_up", :body => { :recipient => recipient })
|
||||
body render(:file => "templates/signed_up", :body => { :recipient => recipient })
|
||||
end
|
||||
|
||||
def implicit_body(recipient)
|
||||
recipients recipient
|
||||
subject "using helpers"
|
||||
from "tester@example.com"
|
||||
|
||||
render(:template => "templates/signed_up", :body => { :recipient => recipient })
|
||||
end
|
||||
|
||||
def rxml_template(recipient)
|
||||
|
@ -69,6 +77,11 @@ class RenderHelperTest < Test::Unit::TestCase
|
|||
restore_delivery_method
|
||||
end
|
||||
|
||||
def test_implicit_body
|
||||
mail = RenderMailer.create_implicit_body(@recipient)
|
||||
assert_equal "Hello there, \n\nMr. test@localhost", mail.body.strip
|
||||
end
|
||||
|
||||
def test_inline_template
|
||||
mail = RenderMailer.create_inline_template(@recipient)
|
||||
assert_equal "Hello, Earth", mail.body.strip
|
||||
|
|
|
@ -157,6 +157,7 @@ module AbstractController
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
# This will be overwritten by _write_layout_method
|
||||
def _layout(details) end
|
||||
|
||||
|
@ -191,6 +192,14 @@ module AbstractController
|
|||
end
|
||||
end
|
||||
|
||||
def _determine_template(options)
|
||||
super
|
||||
|
||||
return if (options.key?(:text) || options.key?(:inline) || options.key?(:partial)) && !options.key?(:layout)
|
||||
layout = options.key?(:layout) ? options[:layout] : :default
|
||||
options[:_layout] = _layout_for_option(layout, options[:_template].details)
|
||||
end
|
||||
|
||||
# Take in the name and details and find a Template.
|
||||
#
|
||||
# ==== Parameters
|
||||
|
|
|
@ -109,6 +109,21 @@ module AbstractController
|
|||
# to a directory.
|
||||
# _partial<TrueClass, FalseClass>:: Whether or not the file to look up is a partial
|
||||
def _determine_template(options)
|
||||
if options.key?(:text)
|
||||
options[:_template] = ActionView::TextTemplate.new(options[:text], formats.first)
|
||||
elsif options.key?(:inline)
|
||||
handler = ActionView::Template.handler_class_for_extension(options[:type] || "erb")
|
||||
template = ActionView::Template.new(options[:inline], "inline #{options[:inline].inspect}", handler, {})
|
||||
options[:_template] = template
|
||||
elsif options.key?(:template)
|
||||
options[:_template_name] = options[:template]
|
||||
elsif options.key?(:file)
|
||||
options[:_template_name] = options[:file]
|
||||
elsif !options.key?(:partial)
|
||||
options[:_template_name] ||= options[:action]
|
||||
options[:_prefix] = _prefix
|
||||
end
|
||||
|
||||
name = (options[:_template_name] || action_name).to_s
|
||||
|
||||
options[:_template] ||= with_template_cache(name) do
|
||||
|
@ -124,6 +139,10 @@ module AbstractController
|
|||
view_paths.exists?(name, details, options[:_prefix], options[:_partial])
|
||||
end
|
||||
|
||||
def _prefix
|
||||
self.class.name.underscore
|
||||
end
|
||||
|
||||
def with_template_cache(name)
|
||||
yield
|
||||
end
|
||||
|
|
|
@ -167,15 +167,5 @@ module ActionController
|
|||
controller_path
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def _determine_template(options)
|
||||
super
|
||||
|
||||
return if (options.key?(:text) || options.key?(:inline) || options.key?(:partial)) && !options.key?(:layout)
|
||||
layout = options.key?(:layout) ? options[:layout] : :default
|
||||
options[:_layout] = _layout_for_option(layout, options[:_template].details)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -38,11 +38,6 @@ module ActionController
|
|||
|
||||
def process_action(*)
|
||||
self.formats = request.formats.map {|x| x.to_sym}
|
||||
|
||||
super
|
||||
end
|
||||
|
||||
def _determine_template(*)
|
||||
super
|
||||
end
|
||||
|
||||
|
@ -74,25 +69,6 @@ module ActionController
|
|||
self.class.template_cache[Thread.current[:format_locale_key]][name] ||= super
|
||||
end
|
||||
|
||||
def _determine_template(options)
|
||||
if options.key?(:text)
|
||||
options[:_template] = ActionView::TextTemplate.new(options[:text], formats.first)
|
||||
elsif options.key?(:inline)
|
||||
handler = ActionView::Template.handler_class_for_extension(options[:type] || "erb")
|
||||
template = ActionView::Template.new(options[:inline], "inline #{options[:inline].inspect}", handler, {})
|
||||
options[:_template] = template
|
||||
elsif options.key?(:template)
|
||||
options[:_template_name] = options[:template]
|
||||
elsif options.key?(:file)
|
||||
options[:_template_name] = options[:file]
|
||||
elsif !options.key?(:partial)
|
||||
options[:_template_name] = (options[:action] || action_name).to_s
|
||||
options[:_prefix] = _prefix
|
||||
end
|
||||
|
||||
super
|
||||
end
|
||||
|
||||
def _process_options(options)
|
||||
status, content_type, location = options.values_at(:status, :content_type, :location)
|
||||
self.status = status if status
|
||||
|
|
|
@ -7,6 +7,8 @@ module AbstractController
|
|||
include AbstractController::RenderingController
|
||||
include Helpers
|
||||
|
||||
def _prefix() end
|
||||
|
||||
def render(string)
|
||||
super(:_template_name => string)
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue