Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
9dd65c36
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9dd65c36
编写于
1月 26, 2010
作者:
J
José Valim and Mikel Lindsaar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make defaults accept a hash.
上级
b8c82edc
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
84 addition
and
79 deletion
+84
-79
actionmailer/lib/action_mailer/base.rb
actionmailer/lib/action_mailer/base.rb
+41
-37
actionmailer/test/base_test.rb
actionmailer/test/base_test.rb
+43
-42
未找到文件。
actionmailer/lib/action_mailer/base.rb
浏览文件 @
9dd65c36
...
...
@@ -254,8 +254,13 @@ class Base < AbstractController::Base
private_class_method
:new
#:nodoc:
extlib_inheritable_accessor
:defaults
self
.
defaults
=
{}
extlib_inheritable_accessor
:default_params
self
.
default_params
=
{
:mime_version
=>
"1.0"
,
:charset
=>
"utf-8"
,
:content_type
=>
"text/plain"
,
:parts_order
=>
[
"text/plain"
,
"text/enriched"
,
"text/html"
]
}
extlib_inheritable_accessor
:default_charset
self
.
default_charset
=
"utf-8"
...
...
@@ -283,6 +288,11 @@ def mailer_name
attr_writer
:mailer_name
alias
:controller_path
:mailer_name
def
defaults
(
value
=
nil
)
self
.
default_params
.
merge!
(
value
)
if
value
self
.
default_params
end
# Receives a raw email, parses it into an email object, decodes it,
# instantiates a new mailer, and passes the email object to the mailer
# object's +receive+ method. If you want your mailer to be able to
...
...
@@ -425,10 +435,10 @@ def attachments
# You can set default values for any of the above headers (except :date) by using the <tt>defaults</tt>
# class method:
#
# class Notifier
# self.defaults
= {
:from => 'no-reply@test.lindsaar.net',
#
:bcc => 'email_logger@test.lindsaar.net',
#
:reply_to => 'bounces@test.lindsaar.net' }
# class Notifier
< ActionMailer::Base
# self.defaults :from => 'no-reply@test.lindsaar.net',
# :bcc => 'email_logger@test.lindsaar.net',
#
:reply_to => 'bounces@test.lindsaar.net'
# end
#
# If you need other headers not listed above, use the <tt>headers['name'] = value</tt> method.
...
...
@@ -475,40 +485,46 @@ def mail(headers={}, &block)
@mail_was_called
=
true
m
=
@_message
# Give preference to headers and fallback to the ones set in mail
content_type
=
headers
[
:content_type
]
||
m
.
content_type
charset
=
headers
[
:charset
]
||
m
.
charset
||
self
.
class
.
default_charset
.
dup
mime_version
=
headers
[
:mime_version
]
||
m
.
mime_version
||
self
.
class
.
default_mime_version
.
dup
# At the beginning, do not consider class default for parts order neither content_type
content_type
=
headers
[
:content_type
]
parts_order
=
headers
[
:parts_order
]
# Set fields quotings
headers
=
set_defaults
(
headers
)
# Merge defaults from class
headers
=
headers
.
reverse_merge
(
self
.
class
.
defaults
)
charset
=
headers
[
:charset
]
# Quote fields
headers
[
:subject
]
||=
default_i18n_subject
quote_fields!
(
headers
,
charset
)
# Render the templates and blocks
responses
,
sort_order
=
collect_responses_and_sort_order
(
headers
,
&
block
)
responses
,
explicit_order
=
collect_responses_and_sort_order
(
headers
,
&
block
)
create_parts_from_responses
(
m
,
responses
,
charset
)
#
Tidy up content type, charset, mime version and sort
order
m
.
content_type
=
set_content_type
(
m
,
content_type
)
#
Finally setup content type and parts
order
m
.
content_type
=
set_content_type
(
m
,
content_type
,
headers
[
:content_type
]
)
m
.
charset
=
charset
m
.
mime_version
=
mime_version
sort_order
=
headers
[
:parts_order
]
||
sort_order
||
self
.
class
.
default_implicit_parts_order
.
dup
if
m
.
multipart?
m
.
body
.
set_sort_order
(
sort_order
)
parts_order
||=
explicit_order
||
headers
[
:parts_order
]
m
.
body
.
set_sort_order
(
parts_order
)
m
.
body
.
sort_parts!
end
#
Finaly set delivery behavior configured in class
#
Set configure delivery behavior
wrap_delivery_behavior!
(
headers
[
:delivery_method
])
# Remove headers already treated and assign all others
headers
.
except!
(
:subject
,
:to
,
:from
,
:cc
,
:bcc
,
:reply_to
)
headers
.
except!
(
:body
,
:parts_order
,
:content_type
,
:charset
,
:delivery_method
)
headers
.
each
{
|
k
,
v
|
m
[
k
]
=
v
}
m
end
protected
def
set_content_type
(
m
,
user_content_type
)
def
set_content_type
(
m
,
user_content_type
,
class_default
)
params
=
m
.
content_type_parameters
||
{}
case
when
user_content_type
.
present?
...
...
@@ -518,23 +534,13 @@ def set_content_type(m, user_content_type)
when
m
.
multipart?
[
"multipart"
,
"alternative"
,
params
]
else
self
.
class
.
default_content_type
.
dup
class_default
end
end
def
set_defaults
(
headers
)
headers
[
:subject
]
||=
default_subject
headers
[
:to
]
||=
self
.
class
.
defaults
[
:to
].
to_s
.
dup
headers
[
:from
]
||=
self
.
class
.
defaults
[
:from
].
to_s
.
dup
headers
[
:cc
]
||=
self
.
class
.
defaults
[
:cc
].
to_s
.
dup
headers
[
:bcc
]
||=
self
.
class
.
defaults
[
:bcc
].
to_s
.
dup
headers
[
:reply_to
]
||=
self
.
class
.
defaults
[
:reply_to
].
to_s
.
dup
headers
end
def
default_subject
#:nodoc:
def
default_i18n_subject
#:nodoc:
mailer_scope
=
self
.
class
.
mailer_name
.
gsub
(
'/'
,
'.'
)
self
.
class
.
defaults
[
:subject
]
||
I18n
.
t
(
:subject
,
:scope
=>
[
:actionmailer
,
mailer_scope
,
action_name
],
:default
=>
action_name
.
humanize
)
I18n
.
t
(
:subject
,
:scope
=>
[
:actionmailer
,
mailer_scope
,
action_name
],
:default
=>
action_name
.
humanize
)
end
# TODO: Move this into Mail
...
...
@@ -546,7 +552,6 @@ def quote_fields!(headers, charset) #:nodoc:
m
.
cc
||=
quote_address_if_necessary
(
headers
[
:cc
],
charset
)
if
headers
[
:cc
]
m
.
bcc
||=
quote_address_if_necessary
(
headers
[
:bcc
],
charset
)
if
headers
[
:bcc
]
m
.
reply_to
||=
quote_address_if_necessary
(
headers
[
:reply_to
],
charset
)
if
headers
[
:reply_to
]
m
.
date
||=
headers
[
:date
]
if
headers
[
:date
]
end
def
collect_responses_and_sort_order
(
headers
)
#:nodoc:
...
...
@@ -588,8 +593,7 @@ def each_template(&block) #:nodoc:
def
create_parts_from_responses
(
m
,
responses
,
charset
)
#:nodoc:
if
responses
.
size
==
1
&&
!
m
.
has_attachments?
headers
=
responses
[
0
]
headers
.
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?
container
=
Mail
::
Part
.
new
...
...
actionmailer/test/base_test.rb
浏览文件 @
9dd65c36
...
...
@@ -2,72 +2,61 @@
require
'abstract_unit'
class
BaseTest
<
ActiveSupport
::
TestCase
DEFAULT_HEADERS
=
{
:to
=>
'mikel@test.lindsaar.net'
,
:subject
=>
'The first email on new API!'
}
class
BaseMailer
<
ActionMailer
::
Base
self
.
defaults
=
{
:to
=>
'system@test.lindsaar.net'
,
:from
=>
'jose@test.plataformatec.com'
,
:reply_to
=>
'mikel@test.lindsaar.net'
,
:subject
=>
'Default Subject!'
}
self
.
mailer_name
=
"base_mailer"
def
empty
(
hash
=
{})
mail
(
hash
)
end
self
.
defaults
:to
=>
'system@test.lindsaar.net'
,
:from
=>
'jose@test.plataformatec.com'
,
:reply_to
=>
'mikel@test.lindsaar.net'
def
welcome
(
hash
=
{})
headers
[
'X-SPAM'
]
=
"Not SPAM"
mail
(
DEFAULT_HEADERS
.
merge
(
hash
))
mail
(
{
:subject
=>
"The first email on new API!"
}.
merge!
(
hash
))
end
def
attachment_with_content
(
hash
=
{})
attachments
[
'invoice.pdf'
]
=
'This is test File content'
mail
(
DEFAULT_HEADERS
.
merge
(
hash
)
)
mail
(
hash
)
end
def
attachment_with_hash
attachments
[
'invoice.jpg'
]
=
{
:data
=>
"you smiling"
,
:mime_type
=>
"image/x-jpg"
,
:transfer_encoding
=>
"base64"
}
mail
(
DEFAULT_HEADERS
)
mail
end
def
implicit_multipart
(
hash
=
{})
attachments
[
'invoice.pdf'
]
=
'This is test File content'
if
hash
.
delete
(
:attachments
)
mail
(
DEFAULT_HEADERS
.
merge
(
hash
)
)
mail
(
hash
)
end
def
implicit_with_locale
(
hash
=
{})
mail
(
DEFAULT_HEADERS
.
merge
(
hash
)
)
mail
(
hash
)
end
def
explicit_multipart
(
hash
=
{})
attachments
[
'invoice.pdf'
]
=
'This is test File content'
if
hash
.
delete
(
:attachments
)
mail
(
DEFAULT_HEADERS
.
merge
(
hash
)
)
do
|
format
|
mail
(
hash
)
do
|
format
|
format
.
text
{
render
:text
=>
"TEXT Explicit Multipart"
}
format
.
html
{
render
:text
=>
"HTML Explicit Multipart"
}
end
end
def
explicit_multipart_templates
(
hash
=
{})
mail
(
DEFAULT_HEADERS
.
merge
(
hash
)
)
do
|
format
|
mail
(
hash
)
do
|
format
|
format
.
html
format
.
text
end
end
def
explicit_multipart_with_any
(
hash
=
{})
mail
(
DEFAULT_HEADERS
.
merge
(
hash
)
)
do
|
format
|
mail
(
hash
)
do
|
format
|
format
.
any
(
:text
,
:html
){
render
:text
=>
"Format with any!"
}
end
end
def
custom_block
(
include_html
=
false
)
mail
(
DEFAULT_HEADERS
)
do
|
format
|
mail
do
|
format
|
format
.
text
(
:content_transfer_encoding
=>
"base64"
){
render
"welcome"
}
format
.
html
{
render
"welcome"
}
if
include_html
end
...
...
@@ -81,19 +70,11 @@ def custom_block(include_html=false)
# Basic mail usage without block
test
"mail() should set the headers of the mail message"
do
email
=
BaseMailer
.
welcome
.
deliver
assert_equal
([
'
mikel
@test.lindsaar.net'
],
email
.
to
)
assert_equal
([
'
system
@test.lindsaar.net'
],
email
.
to
)
assert_equal
([
'jose@test.plataformatec.com'
],
email
.
from
)
assert_equal
(
'The first email on new API!'
,
email
.
subject
)
end
test
"mail() should pull the defaults from the class if nothing is specified"
do
email
=
BaseMailer
.
empty
.
deliver
assert_equal
([
'system@test.lindsaar.net'
],
email
.
to
)
assert_equal
([
'jose@test.plataformatec.com'
],
email
.
from
)
assert_equal
([
'mikel@test.lindsaar.net'
],
email
.
reply_to
)
assert_equal
(
'Default Subject!'
,
email
.
subject
)
end
test
"mail() with from overwrites the class level default"
do
email
=
BaseMailer
.
welcome
(
:from
=>
'someone@example.com'
,
:to
=>
'another@example.org'
).
deliver
...
...
@@ -184,7 +165,7 @@ def custom_block(include_html=false)
# Defaults values
test
"uses default charset from class"
do
swap
BaseMailer
,
:default_
charset
=>
"US-ASCII"
do
with_default
BaseMailer
,
:
charset
=>
"US-ASCII"
do
email
=
BaseMailer
.
welcome
.
deliver
assert_equal
(
"US-ASCII"
,
email
.
charset
)
...
...
@@ -194,7 +175,7 @@ def custom_block(include_html=false)
end
test
"uses default content type from class"
do
swap
BaseMailer
,
:default_
content_type
=>
"text/html"
do
with_default
BaseMailer
,
:
content_type
=>
"text/html"
do
email
=
BaseMailer
.
welcome
.
deliver
assert_equal
(
"text/html"
,
email
.
mime_type
)
...
...
@@ -204,7 +185,7 @@ def custom_block(include_html=false)
end
test
"uses default mime version from class"
do
swap
BaseMailer
,
:default_
mime_version
=>
"2.0"
do
with_default
BaseMailer
,
:
mime_version
=>
"2.0"
do
email
=
BaseMailer
.
welcome
.
deliver
assert_equal
(
"2.0"
,
email
.
mime_version
)
...
...
@@ -213,6 +194,13 @@ def custom_block(include_html=false)
end
end
test
"uses default headers from class"
do
with_default
BaseMailer
,
"X-SPAM"
=>
"Not spam"
do
email
=
BaseMailer
.
welcome
.
deliver
assert_equal
(
"Not spam"
,
email
[
"X-SPAM"
].
decoded
)
end
end
test
"subject gets default from I18n"
do
BaseMailer
.
defaults
[
:subject
]
=
nil
email
=
BaseMailer
.
welcome
(
:subject
=>
nil
).
deliver
...
...
@@ -236,7 +224,7 @@ def custom_block(include_html=false)
test
"implicit multipart with sort order"
do
order
=
[
"text/html"
,
"text/plain"
]
swap
BaseMailer
,
:default_implicit_
parts_order
=>
order
do
with_default
BaseMailer
,
:
parts_order
=>
order
do
email
=
BaseMailer
.
implicit_multipart
.
deliver
assert_equal
(
"text/html"
,
email
.
parts
[
0
].
mime_type
)
assert_equal
(
"text/plain"
,
email
.
parts
[
1
].
mime_type
)
...
...
@@ -259,7 +247,7 @@ def custom_block(include_html=false)
test
"implicit multipart with attachments and sort order"
do
order
=
[
"text/html"
,
"text/plain"
]
swap
BaseMailer
,
:default_implicit_
parts_order
=>
order
do
with_default
BaseMailer
,
:
parts_order
=>
order
do
email
=
BaseMailer
.
implicit_multipart
(
:attachments
=>
true
).
deliver
assert_equal
(
"application/pdf"
,
email
.
parts
[
0
].
mime_type
)
assert_equal
(
"multipart/alternative"
,
email
.
parts
[
1
].
mime_type
)
...
...
@@ -323,7 +311,7 @@ def custom_block(include_html=false)
test
"explicit multipart does not sort order"
do
order
=
[
"text/html"
,
"text/plain"
]
swap
BaseMailer
,
:default_implicit_
parts_order
=>
order
do
with_default
BaseMailer
,
:
parts_order
=>
order
do
email
=
BaseMailer
.
explicit_multipart
.
deliver
assert_equal
(
"text/plain"
,
email
.
parts
[
0
].
mime_type
)
assert_equal
(
"text/html"
,
email
.
parts
[
1
].
mime_type
)
...
...
@@ -424,17 +412,30 @@ def custom_block(include_html=false)
# Execute the block setting the given values and restoring old values after
# the block is executed.
def
swap
(
object
,
new_values
)
def
swap
(
klass
,
new_values
)
old_values
=
{}
new_values
.
each
do
|
key
,
value
|
old_values
[
key
]
=
object
.
send
key
object
.
send
:"
#{
key
}
="
,
value
old_values
[
key
]
=
klass
.
send
key
klass
.
send
:"
#{
key
}
="
,
value
end
yield
ensure
old_values
.
each
do
|
key
,
value
|
object
.
send
:"
#{
key
}
="
,
value
klass
.
send
:"
#{
key
}
="
,
value
end
end
def
with_default
(
klass
,
new_values
)
hash
=
klass
.
defaults
old_values
=
{}
new_values
.
each
do
|
key
,
value
|
old_values
[
key
]
=
hash
[
key
]
hash
[
key
]
=
value
end
yield
ensure
old_values
.
each
do
|
key
,
value
|
hash
[
key
]
=
value
end
end
end
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录