Make ActionMailer::Base inherit from AbstractController::Base

Signed-off-by: Yehuda Katz <wycats@Yehuda-Katz.local>
This commit is contained in:
José Valim 2009-12-22 20:17:27 +01:00 committed by Yehuda Katz
parent 8e48a5ef0c
commit 4964d3b02c
8 changed files with 63 additions and 81 deletions

View File

@ -250,31 +250,21 @@ module ActionMailer #:nodoc:
# <tt>["text/html", "text/enriched", "text/plain"]</tt>. Items that appear first in the array have higher priority in the mail client
# and appear last in the mime encoded message. You can also pick a different order from inside a method with
# +implicit_parts_order+.
class Base
class Base < AbstractController::Base
include AdvAttrAccessor, PartContainer, Quoting, Utils
include AbstractController::Rendering
include AbstractController::LocalizedCache
include AbstractController::Layouts
include AbstractController::Helpers
helper ActionMailer::MailHelper
if Object.const_defined?(:ActionController)
include ActionController::UrlWriter
end
include ActionController::UrlWriter
include ActionMailer::DeprecatedBody
private_class_method :new #:nodoc:
class_inheritable_accessor :view_paths
self.view_paths = []
attr_internal :formats
cattr_accessor :logger
@@raise_delivery_errors = true
cattr_accessor :raise_delivery_errors
@ -346,24 +336,13 @@ module ActionMailer #:nodoc:
# have multiple mailer methods share the same template.
adv_attr_accessor :template
# The mail and action_name instances referenced by this mailer.
attr_reader :mail, :action_name
# Where the response body is stored.
attr_internal :response_body
# Override the mailer name, which defaults to an inflected version of the
# mailer's class name. If you want to use a template in a non-standard
# location, you can use this to specify that location.
attr_writer :mailer_name
adv_attr_accessor :mailer_name
def mailer_name(value = nil)
if value
@mailer_name = value
else
@mailer_name || self.class.mailer_name
end
end
# Expose the internal mail
attr_reader :mail
# Alias controller_path to mailer_name so render :partial in views work.
alias :controller_path :mailer_name
@ -453,18 +432,16 @@ module ActionMailer #:nodoc:
# will be initialized according to the named method. If not, the mailer will
# remain uninitialized (useful when you only need to invoke the "receive"
# method, for instance).
def initialize(method_name=nil, *parameters) #:nodoc:
@_formats = []
@_response_body = nil
def initialize(method_name=nil, *args) #:nodoc:
super()
create!(method_name, *parameters) if method_name
process(method_name, *args) if method_name
end
# Initialize the mailer via the given +method_name+. The body will be
# Process 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:
def process(method_name, *args) #:nodoc:
initialize_defaults(method_name)
__send__(method_name, *parameters)
super
# Create e-mail parts
create_parts
@ -473,7 +450,7 @@ module ActionMailer #:nodoc:
@subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name],
:default => method_name.humanize)
# build the mail object itself
# Build the mail object itself
@mail = create_mail
end
@ -488,7 +465,7 @@ module ActionMailer #:nodoc:
logger.debug "\n#{mail.encoded}"
end
ActiveSupport::Notifications.instrument(:deliver_mail, :mail => @mail) do
ActiveSupport::Notifications.instrument(:deliver_mail, :mail => mail) do
begin
self.delivery_method.perform_delivery(mail) if perform_deliveries
rescue Exception => e # Net::SMTP errors or sendmail pipe errors
@ -510,23 +487,18 @@ module ActionMailer #:nodoc:
@implicit_parts_order ||= @@default_implicit_parts_order.dup
@mime_version ||= @@default_mime_version.dup if @@default_mime_version
@mailer_name ||= self.class.mailer_name
@mailer_name ||= self.class.mailer_name.dup
@template ||= method_name
@action_name = @template
@parts ||= []
@headers ||= {}
@sent_on ||= Time.now
ActiveSupport::Deprecation.silence do
super # Run deprecation hooks
end
super # Run deprecation hooks
end
def create_parts
ActiveSupport::Deprecation.silence do
super # Run deprecation hooks
end
super # Run deprecation hooks
if String === response_body
@parts.unshift Part.new(

View File

@ -34,13 +34,13 @@ class TestMailer < ActionMailer::Base
def from_with_name
from "System <system@loudthinking.com>"
recipients "root@loudthinking.com"
body "Nothing to see here."
render :text => "Nothing to see here."
end
def from_without_name
from "system@loudthinking.com"
recipients "root@loudthinking.com"
body "Nothing to see here."
render :text => "Nothing to see here."
end
def cc_bcc(recipient)
@ -301,6 +301,7 @@ class TestMailer < ActionMailer::Base
render :text => "testing"
end
# This tests body calls accepeting a hash, which is deprecated.
def body_ivar(recipient)
recipients recipient
subject "Body as a local variable"
@ -1043,7 +1044,8 @@ EOF
end
def test_body_is_stored_as_an_ivar
mail = TestMailer.create_body_ivar(@recipient)
mail = nil
ActiveSupport::Deprecation.silence { mail = TestMailer.create_body_ivar(@recipient) }
assert_equal "body: foo\nbar: baz", mail.body
end

View File

@ -12,8 +12,8 @@ class TestMailer < ActionMailer::Base
@from = "system@loudthinking.com"
@sent_on = Time.local(2004, 12, 12)
@body["recipient"] = recipient
@body["welcome_url"] = url_for :host => "example.com", :controller => "welcome", :action => "greeting"
@recipient = recipient
@welcome_url = url_for :host => "example.com", :controller => "welcome", :action => "greeting"
end
class <<self

View File

@ -84,7 +84,7 @@ module AbstractController
#
# ==== Returns
# self
def process(action)
def process(action, *args)
@_action_name = action_name = action.to_s
unless action_name = method_for_action(action_name)
@ -93,7 +93,7 @@ module AbstractController
@_response_body = nil
process_action(action_name)
process_action(action_name, *args)
end
private
@ -113,8 +113,8 @@ module AbstractController
# Call the action. Override this in a subclass to modify the
# behavior around processing an action. This, and not #process,
# is the intended way to override action dispatching.
def process_action(method_name)
send_action(method_name)
def process_action(method_name, *args)
send_action(method_name, *args)
end
# Actually call the method associated with the action. Override

View File

@ -29,33 +29,5 @@ module AbstractController
@str.send(*args, &block)
end
end
# Override process_action in the AbstractController::Base
# to log details about the method.
def process_action(action)
result = ActiveSupport::Notifications.instrument(:process_action,
:controller => self, :action => action) do
super
end
if logger
log = DelayedLog.new do
"\n\nProcessing #{self.class.name}\##{action_name} " \
"to #{request.formats} (for #{request_origin}) " \
"[#{request.method.to_s.upcase}]"
end
logger.info(log)
end
result
end
private
# Returns the request origin with the IP and time. This needs to be cached,
# otherwise we would get different results for each time it calls.
def request_origin
@request_origin ||= "#{request.remote_ip} at #{Time.now.to_s(:db)}"
end
end
end

View File

@ -21,6 +21,7 @@ module ActionController
autoload :Helpers
autoload :HideActions
autoload :Layouts
autoload :Logger
autoload :MimeResponds
autoload :RackDelegation
autoload :Compatibility

View File

@ -14,6 +14,7 @@ module ActionController
include ActionController::Layouts
include ActionController::ConditionalGet
include ActionController::RackDelegation
include ActionController::Logger
include ActionController::Benchmarking
include ActionController::Configuration

View File

@ -0,0 +1,34 @@
require 'abstract_controller/logger'
module ActionController
module Logger
# Override process_action in the AbstractController::Base
# to log details about the method.
def process_action(action)
result = ActiveSupport::Notifications.instrument(:process_action,
:controller => self, :action => action) do
super
end
if logger
log = AbstractController::Logger::DelayedLog.new do
"\n\nProcessing #{self.class.name}\##{action_name} " \
"to #{request.formats} (for #{request_origin}) " \
"[#{request.method.to_s.upcase}]"
end
logger.info(log)
end
result
end
private
# Returns the request origin with the IP and time. This needs to be cached,
# otherwise we would get different results for each time it calls.
def request_origin
@request_origin ||= "#{request.remote_ip} at #{Time.now.to_s(:db)}"
end
end
end