From e9a8e0053be3b293ab89fb584f1d660063f107aa Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 31 Aug 2008 19:11:15 +0100 Subject: Add layout functionality to mailers. Mailer layouts behaves just like controller layouts, except layout names need to have '_mailer' postfix for them to be automatically picked up. --- actionmailer/lib/action_mailer/base.rb | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'actionmailer/lib/action_mailer/base.rb') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 5b3c560390..96e514e0db 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -246,7 +246,10 @@ module ActionMailer #:nodoc: # +implicit_parts_order+. class Base include AdvAttrAccessor, PartContainer - include ActionController::UrlWriter if Object.const_defined?(:ActionController) + if Object.const_defined?(:ActionController) + include ActionController::UrlWriter + include ActionController::Layout + end private_class_method :new #:nodoc: @@ -362,6 +365,7 @@ module ActionMailer #:nodoc: # The mail object instance referenced by this mailer. attr_reader :mail + attr_reader :template_name, :default_template_name, :action_name class << self attr_writer :mailer_name @@ -530,6 +534,7 @@ module ActionMailer #:nodoc: @content_type ||= @@default_content_type.dup @implicit_parts_order ||= @@default_implicit_parts_order.dup @template ||= method_name + @default_template_name = @action_name = @template @mailer_name ||= self.class.name.underscore @parts ||= [] @headers ||= {} @@ -546,7 +551,22 @@ module ActionMailer #:nodoc: if opts[:file] && (opts[:file] !~ /\// && !opts[:file].respond_to?(:render)) opts[:file] = "#{mailer_name}/#{opts[:file]}" end - initialize_template_class(body).render(opts) + + begin + old_template, @template = @template, initialize_template_class(body) + layout = respond_to?(:pick_layout, true) ? pick_layout(opts) : false + @template.render(opts.merge(:layout => layout)) + ensure + @template = old_template + end + end + + def default_template_format + :html + end + + def candidate_for_layout?(options) + !@template.send(:_exempt_from_layout?, default_template_name) end def template_root -- cgit v1.2.3