Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mikel/rails
Browse files Browse the repository at this point in the history
Conflicts:
	actionmailer/lib/action_mailer/base.rb
  • Loading branch information
mikel committed Jan 26, 2010
2 parents 21dcc20 + 8fabcb2 commit 1133757
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 28 deletions.
25 changes: 6 additions & 19 deletions actionmailer/lib/action_mailer/base.rb
Expand Up @@ -270,18 +270,6 @@ class Base < AbstractController::Base
:parts_order => [ "text/plain", "text/enriched", "text/html" ] :parts_order => [ "text/plain", "text/enriched", "text/html" ]
} }


extlib_inheritable_accessor :default_charset
self.default_charset = self.default_params[:charset]

extlib_inheritable_accessor :default_content_type
self.default_content_type = self.default_params[:content_type]

extlib_inheritable_accessor :default_mime_version
self.default_mime_version = self.default_params[:mime_version]

extlib_inheritable_accessor :default_implicit_parts_order
self.default_implicit_parts_order = self.default_params[:parts_order]

class << self class << self


def mailer_name def mailer_name
Expand Down Expand Up @@ -505,7 +493,7 @@ def mail(headers={}, &block)
quote_fields!(headers, charset) quote_fields!(headers, charset)


# Render the templates and blocks # Render the templates and blocks
responses, explicit_order = collect_responses_and_sort_order(headers, &block) responses, explicit_order = collect_responses_and_parts_order(headers, &block)
create_parts_from_responses(m, responses, charset) create_parts_from_responses(m, responses, charset)


# Finally setup content type and parts order # Finally setup content type and parts order
Expand Down Expand Up @@ -561,18 +549,18 @@ def quote_fields!(headers, charset) #:nodoc:
m.reply_to ||= quote_address_if_necessary(headers[:reply_to], charset) if headers[:reply_to] m.reply_to ||= quote_address_if_necessary(headers[:reply_to], charset) if headers[:reply_to]
end end


def collect_responses_and_sort_order(headers) #:nodoc: def collect_responses_and_parts_order(headers) #:nodoc:
responses, sort_order = [], nil responses, parts_order = [], nil


if block_given? if block_given?
collector = ActionMailer::Collector.new(self) { render(action_name) } collector = ActionMailer::Collector.new(self) { render(action_name) }
yield(collector) yield(collector)
sort_order = collector.responses.map { |r| r[:content_type] } parts_order = collector.responses.map { |r| r[:content_type] }
responses = collector.responses responses = collector.responses
elsif headers[:body] elsif headers[:body]
responses << { responses << {
:body => headers[:body], :body => headers[:body],
:content_type => self.class.default_content_type.dup :content_type => self.class.defaults[:content_type] || "text/plain"
} }
else else
each_template do |template| each_template do |template|
Expand All @@ -583,7 +571,7 @@ def collect_responses_and_sort_order(headers) #:nodoc:
end end
end end


[responses, sort_order] [responses, parts_order]
end end


def each_template(&block) #:nodoc: def each_template(&block) #:nodoc:
Expand All @@ -601,7 +589,6 @@ def each_template(&block) #:nodoc:
def create_parts_from_responses(m, responses, charset) #:nodoc: def create_parts_from_responses(m, responses, charset) #:nodoc:
if responses.size == 1 && !m.has_attachments? if responses.size == 1 && !m.has_attachments?
responses[0].each { |k,v| m[k] = v } responses[0].each { |k,v| m[k] = v }
return responses[0][:content_type]
elsif responses.size > 1 && m.has_attachments? elsif responses.size > 1 && m.has_attachments?
container = Mail::Part.new container = Mail::Part.new
container.content_type = "multipart/alternative" container.content_type = "multipart/alternative"
Expand Down
5 changes: 4 additions & 1 deletion actionmailer/lib/action_mailer/delivery_methods.rb
Expand Up @@ -65,7 +65,10 @@ def wrap_delivery_behavior(mail, method=nil) #:nodoc:
method ||= self.delivery_method method ||= self.delivery_method
mail.delivery_handler = self mail.delivery_handler = self


if method.is_a?(Symbol) case method
when NilClass
raise "Delivery method cannot be nil"
when Symbol
if klass = delivery_methods[method.to_sym] if klass = delivery_methods[method.to_sym]
mail.delivery_method(klass, send(:"#{method}_settings")) mail.delivery_method(klass, send(:"#{method}_settings"))
else else
Expand Down
29 changes: 27 additions & 2 deletions actionmailer/lib/action_mailer/deprecated_api.rb
Expand Up @@ -5,8 +5,25 @@ module ActionMailer
module DeprecatedApi #:nodoc: module DeprecatedApi #:nodoc:
extend ActiveSupport::Concern extend ActiveSupport::Concern


module ClassMethods included do
[:charset, :content_type, :mime_version, :implicit_parts_order].each do |method|
class_eval <<-FILE, __FILE__, __LINE__ + 1
def self.default_#{method}
@@default_#{method}
end
def self.default_#{method}=(value)
ActiveSupport::Deprecation.warn "ActionMailer::Base.default_#{method}=value is deprecated, " <<
"use defaults :#{method} => value instead"
@@default_#{method} = value
end
@@default_#{method} = nil
FILE
end
end


module ClassMethods
# Deliver the given mail object directly. This can be used to deliver # Deliver the given mail object directly. This can be used to deliver
# a preconstructed mail object, like: # a preconstructed mail object, like:
# #
Expand Down Expand Up @@ -99,7 +116,15 @@ def render_message(*args)
end end


private private


def initialize_defaults(*)
@charset ||= self.class.default_charset.try(:dup)
@content_type ||= self.class.default_content_type.try(:dup)
@implicit_parts_order ||= self.class.default_implicit_parts_order.try(:dup)
@mime_version ||= self.class.default_mime_version.try(:dup)
super
end

def create_parts def create_parts
if @body.is_a?(Hash) && !@body.empty? if @body.is_a?(Hash) && !@body.empty?
ActiveSupport::Deprecation.warn "Giving a hash to body is deprecated, please use instance variables instead", caller[0,2] ActiveSupport::Deprecation.warn "Giving a hash to body is deprecated, please use instance variables instead", caller[0,2]
Expand Down
10 changes: 6 additions & 4 deletions actionmailer/lib/action_mailer/old_api.rb
@@ -1,3 +1,5 @@
require 'active_support/core_ext/object/try'

module ActionMailer module ActionMailer
module OldApi #:nodoc: module OldApi #:nodoc:
extend ActiveSupport::Concern extend ActiveSupport::Concern
Expand Down Expand Up @@ -185,10 +187,10 @@ def create_mail
# mailer. Subclasses may override this method to provide different # mailer. Subclasses may override this method to provide different
# defaults. # defaults.
def initialize_defaults(method_name) def initialize_defaults(method_name)
@charset ||= self.class.default_charset.dup @charset ||= self.class.defaults[:charset].try(:dup)
@content_type ||= self.class.default_content_type.dup @content_type ||= self.class.defaults[:content_type].try(:dup)
@implicit_parts_order ||= self.class.default_implicit_parts_order.dup @implicit_parts_order ||= self.class.defaults[:parts_order].try(:dup)
@mime_version ||= self.class.default_mime_version.dup if self.class.default_mime_version @mime_version ||= self.class.defaults[:mime_version].try(:dup)


@mailer_name ||= self.class.mailer_name.dup @mailer_name ||= self.class.mailer_name.dup
@template ||= method_name @template ||= method_name
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/generators/rails/mailer/templates/mailer.rb
@@ -1,5 +1,5 @@
class <%= class_name %> < ActionMailer::Base class <%= class_name %> < ActionMailer::Base
self.defaults = { :from => "from@example.com" } self.defaults :from => "from@example.com"
<% for action in actions -%> <% for action in actions -%>
# Subject can be set in your I18n file at config/locales/en.yml # Subject can be set in your I18n file at config/locales/en.yml
Expand Down
2 changes: 1 addition & 1 deletion railties/test/generators/mailer_generator_test.rb
Expand Up @@ -9,7 +9,7 @@ def test_mailer_skeleton_is_created
run_generator run_generator
assert_file "app/mailers/notifier.rb" do |mailer| assert_file "app/mailers/notifier.rb" do |mailer|
assert_match /class Notifier < ActionMailer::Base/, mailer assert_match /class Notifier < ActionMailer::Base/, mailer
assert_match /self\.defaults\ =\ \{\ :from\ =>\ "from@example\.com"\ \}/, mailer assert_match /self\.defaults :from => "from@example.com"/, mailer
end end
end end


Expand Down

0 comments on commit 1133757

Please sign in to comment.