Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
0b05acd4
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,发现更多精彩内容 >>
提交
0b05acd4
编写于
1月 26, 2010
作者:
M
Mikel Lindsaar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implementing class level :defaults hash, instead of delivers_from et al
上级
64f8c87b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
49 addition
and
16 deletion
+49
-16
actionmailer/lib/action_mailer/base.rb
actionmailer/lib/action_mailer/base.rb
+26
-13
actionmailer/test/base_test.rb
actionmailer/test/base_test.rb
+23
-3
未找到文件。
actionmailer/lib/action_mailer/base.rb
浏览文件 @
0b05acd4
...
...
@@ -254,8 +254,8 @@ class Base < AbstractController::Base
private_class_method
:new
#:nodoc:
extlib_inheritable_accessor
:default
_from
self
.
default
_from
=
nil
extlib_inheritable_accessor
:default
s
self
.
default
s
=
{}
extlib_inheritable_accessor
:default_charset
self
.
default_charset
=
"utf-8"
...
...
@@ -276,18 +276,13 @@ class Base < AbstractController::Base
self
.
default_implicit_parts_order
=
[
"text/plain"
,
"text/enriched"
,
"text/html"
]
class
<<
self
def
mailer_name
@mailer_name
||=
name
.
underscore
end
attr_writer
:mailer_name
alias
:controller_path
:mailer_name
# Sets who is the default sender for the e-mail
def
delivers_from
(
value
=
nil
)
self
.
default_from
=
value
if
value
self
.
default_from
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
...
...
@@ -419,8 +414,7 @@ def attachments
# humanized version of the <tt>action_name</tt>
# * <tt>:to</tt> - Who the message is destined for, can be a string of addresses, or an array
# of addresses.
# * <tt>:from</tt> - Who the message is from, if missing, will use the <tt>:delivers_from</tt>
# value in the class (if it exists)
# * <tt>:from</tt> - Who the message is from
# * <tt>:cc</tt> - Who you would like to Carbon-Copy on this email, can be a string of addresses,
# or an array of addresses.
# * <tt>:bcc</tt> - Who you would like to Blind-Carbon-Copy on this email, can be a string of
...
...
@@ -428,6 +422,15 @@ def attachments
# * <tt>:reply_to</tt> - Who to set the Reply-To header of the email to.
# * <tt>:date</tt> - The date to say the email was sent on.
#
# 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' }
# end
#
# If you need other headers not listed above, use the <tt>headers['name'] = value</tt> method.
#
# When a <tt>:return_path</tt> is specified as header, that value will be used as the 'envelope from'
...
...
@@ -478,8 +481,8 @@ def mail(headers={}, &block)
mime_version
=
headers
[
:mime_version
]
||
m
.
mime_version
||
self
.
class
.
default_mime_version
.
dup
# Set fields quotings
headers
[
:subject
]
||=
default_subject
headers
[
:from
]
||=
self
.
class
.
default_from
.
dup
headers
=
set_defaults
(
headers
)
quote_fields!
(
headers
,
charset
)
# Render the templates and blocks
...
...
@@ -519,9 +522,19 @@ def set_content_type(m, user_content_type)
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:
mailer_scope
=
self
.
class
.
mailer_name
.
gsub
(
'/'
,
'.'
)
I18n
.
t
(
:subject
,
:scope
=>
[
:actionmailer
,
mailer_scope
,
action_name
],
:default
=>
action_name
.
humanize
)
self
.
class
.
defaults
[
:subject
]
||
I18n
.
t
(
:subject
,
:scope
=>
[
:actionmailer
,
mailer_scope
,
action_name
],
:default
=>
action_name
.
humanize
)
end
# TODO: Move this into Mail
...
...
actionmailer/test/base_test.rb
浏览文件 @
0b05acd4
...
...
@@ -8,9 +8,18 @@ class BaseTest < ActiveSupport::TestCase
}
class
BaseMailer
<
ActionMailer
::
Base
delivers_from
'jose@test.plataformatec.com'
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
def
welcome
(
hash
=
{})
headers
[
'X-SPAM'
]
=
"Not SPAM"
mail
(
DEFAULT_HEADERS
.
merge
(
hash
))
...
...
@@ -77,9 +86,19 @@ def custom_block(include_html=false)
assert_equal
(
email
.
subject
,
'The first email on new API!'
)
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@else.com'
).
deliver
assert_equal
(
email
.
from
,
[
'someone@else.com'
])
email
=
BaseMailer
.
welcome
(
:from
=>
'someone@example.com'
,
:to
=>
'another@example.org'
).
deliver
assert_equal
([
'someone@example.com'
],
email
.
from
)
assert_equal
([
'another@example.org'
],
email
.
to
)
end
test
"mail() with bcc, cc, content_type, charset, mime_version, reply_to and date"
do
...
...
@@ -195,6 +214,7 @@ def custom_block(include_html=false)
end
test
"subject gets default from I18n"
do
BaseMailer
.
defaults
[
:subject
]
=
nil
email
=
BaseMailer
.
welcome
(
:subject
=>
nil
).
deliver
assert_equal
"Welcome"
,
email
.
subject
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录