Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
d6dec7fc
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,发现更多精彩内容 >>
提交
d6dec7fc
编写于
12月 16, 2013
作者:
A
Andrew White
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add mailer previews feature based on mail_view gem
上级
1602a70f
变更
17
隐藏空白更改
内联
并排
Showing
17 changed file
with
736 addition
and
6 deletion
+736
-6
actionmailer/CHANGELOG.md
actionmailer/CHANGELOG.md
+4
-0
actionmailer/lib/action_mailer.rb
actionmailer/lib/action_mailer.rb
+2
-0
actionmailer/lib/action_mailer/base.rb
actionmailer/lib/action_mailer/base.rb
+20
-0
actionmailer/lib/action_mailer/preview.rb
actionmailer/lib/action_mailer/preview.rb
+67
-0
actionmailer/lib/action_mailer/railtie.rb
actionmailer/lib/action_mailer/railtie.rb
+8
-0
actionpack/lib/action_dispatch/routing/inspector.rb
actionpack/lib/action_dispatch/routing/inspector.rb
+1
-1
railties/lib/rails.rb
railties/lib/rails.rb
+1
-0
railties/lib/rails/application/finisher.rb
railties/lib/rails/application/finisher.rb
+2
-0
railties/lib/rails/generators/test_unit/mailer/mailer_generator.rb
...lib/rails/generators/test_unit/mailer/mailer_generator.rb
+8
-1
railties/lib/rails/generators/test_unit/mailer/templates/preview.rb
...ib/rails/generators/test_unit/mailer/templates/preview.rb
+11
-0
railties/lib/rails/mailers_controller.rb
railties/lib/rails/mailers_controller.rb
+73
-0
railties/lib/rails/templates/layouts/application.html.erb
railties/lib/rails/templates/layouts/application.html.erb
+6
-1
railties/lib/rails/templates/rails/mailers/email.html.erb
railties/lib/rails/templates/rails/mailers/email.html.erb
+98
-0
railties/lib/rails/templates/rails/mailers/index.html.erb
railties/lib/rails/templates/rails/mailers/index.html.erb
+8
-0
railties/lib/rails/templates/rails/mailers/mailer.html.erb
railties/lib/rails/templates/rails/mailers/mailer.html.erb
+6
-0
railties/test/application/mailer_previews_test.rb
railties/test/application/mailer_previews_test.rb
+388
-0
railties/test/generators/mailer_generator_test.rb
railties/test/generators/mailer_generator_test.rb
+33
-3
未找到文件。
actionmailer/CHANGELOG.md
浏览文件 @
d6dec7fc
*
Add mailer previews feature based on 37 Signals mail_view gem
*Andrew White*
*
Calling
`mail()`
without arguments serves as getter for the current mail
*
Calling
`mail()`
without arguments serves as getter for the current mail
message and keeps previously set headers.
message and keeps previously set headers.
...
...
actionmailer/lib/action_mailer.rb
浏览文件 @
d6dec7fc
...
@@ -41,6 +41,8 @@ module ActionMailer
...
@@ -41,6 +41,8 @@ module ActionMailer
autoload
:Base
autoload
:Base
autoload
:DeliveryMethods
autoload
:DeliveryMethods
autoload
:MailHelper
autoload
:MailHelper
autoload
:Preview
autoload
:Previews
,
'action_mailer/preview'
autoload
:TestCase
autoload
:TestCase
autoload
:TestHelper
autoload
:TestHelper
end
end
actionmailer/lib/action_mailer/base.rb
浏览文件 @
d6dec7fc
...
@@ -308,6 +308,25 @@ module ActionMailer
...
@@ -308,6 +308,25 @@ module ActionMailer
# Note that unless you have a specific reason to do so, you should prefer using before_action
# Note that unless you have a specific reason to do so, you should prefer using before_action
# rather than after_action in your ActionMailer classes so that headers are parsed properly.
# rather than after_action in your ActionMailer classes so that headers are parsed properly.
#
#
# = Previewing emails
#
# You can preview your email templates visually by adding a mailer preview file to the
# <tt>ActionMailer::Base.preview_path</tt>. Since most emails do something interesting
# with database data, you'll need to write some scenarios to load messages with fake data:
#
# class NotifierPreview < ActionMailer::Preview
# def welcome
# Notifier.welcome(User.first)
# end
# end
#
# Methods must return a Mail::Message object which can be generated by calling the mailer
# method without the additional <tt>deliver</tt>. The location of the mailer previews
# directory can be configured using the <tt>preview_path</tt> option which has a default
# of <tt>test/mailers/previews</tt>:
#
# config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews"
#
# = Configuration options
# = Configuration options
#
#
# These options are specified on the class level, like
# These options are specified on the class level, like
...
@@ -362,6 +381,7 @@ module ActionMailer
...
@@ -362,6 +381,7 @@ module ActionMailer
# <tt>delivery_method :test</tt>. Most useful for unit and functional testing.
# <tt>delivery_method :test</tt>. Most useful for unit and functional testing.
class
Base
<
AbstractController
::
Base
class
Base
<
AbstractController
::
Base
include
DeliveryMethods
include
DeliveryMethods
include
Previews
abstract!
abstract!
...
...
actionmailer/lib/action_mailer/preview.rb
0 → 100644
浏览文件 @
d6dec7fc
require
'active_support/descendants_tracker'
module
ActionMailer
module
Previews
#:nodoc:
extend
ActiveSupport
::
Concern
included
do
# Set the location of mailer previews through app configuration:
#
# config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews"
#
class_attribute
:preview_path
,
instance_writer:
false
end
end
class
Preview
extend
ActiveSupport
::
DescendantsTracker
class
<<
self
# Returns all mailer preview classes
def
all
load_previews
if
descendants
.
empty?
descendants
end
# Returns the mail object for the given email name
def
call
(
email
)
preview
=
self
.
new
preview
.
public_send
(
email
)
end
# Returns all of the available email previews
def
emails
public_instance_methods
(
false
).
map
(
&
:to_s
).
sort
end
# Returns true if the email exists
def
email_exists?
(
email
)
emails
.
include?
(
email
)
end
# Returns true if the preview exists
def
exists?
(
preview
)
all
.
any?
{
|
p
|
p
.
preview_name
==
preview
}
end
# Find a mailer preview by its underscored class name
def
find
(
preview
)
all
.
find
{
|
p
|
p
.
preview_name
==
preview
}
end
# Returns the underscored name of the mailer preview without the suffix
def
preview_name
name
.
sub
(
/Preview$/
,
''
).
underscore
end
protected
def
load_previews
#:nodoc:
Dir
[
"
#{
preview_path
}
/**/*_preview.rb"
].
each
{
|
file
|
require_dependency
file
}
end
def
preview_path
#:nodoc:
Base
.
preview_path
end
end
end
end
actionmailer/lib/action_mailer/railtie.rb
浏览文件 @
d6dec7fc
...
@@ -40,5 +40,13 @@ class Railtie < Rails::Railtie # :nodoc:
...
@@ -40,5 +40,13 @@ class Railtie < Rails::Railtie # :nodoc:
config
.
compile_methods!
if
config
.
respond_to?
(
:compile_methods!
)
config
.
compile_methods!
if
config
.
respond_to?
(
:compile_methods!
)
end
end
end
end
initializer
"action_mailer.configure_mailer_previews"
,
before: :set_autoload_paths
do
|
app
|
if
Rails
.
env
.
development?
options
=
app
.
config
.
action_mailer
options
.
preview_path
||=
defined?
(
Rails
.
root
)
?
"
#{
Rails
.
root
}
/test/mailers/previews"
:
nil
app
.
config
.
autoload_paths
<<
options
.
preview_path
end
end
end
end
end
end
actionpack/lib/action_dispatch/routing/inspector.rb
浏览文件 @
d6dec7fc
...
@@ -69,7 +69,7 @@ def action
...
@@ -69,7 +69,7 @@ def action
end
end
def
internal?
def
internal?
controller
.
to_s
=~
%r{
\A
rails/(info|welcome)}
||
path
=~
%r{
\A
#{
Rails
.
application
.
config
.
assets
.
prefix
}
}
controller
.
to_s
=~
%r{
\A
rails/(info|
mailers|
welcome)}
||
path
=~
%r{
\A
#{
Rails
.
application
.
config
.
assets
.
prefix
}
}
end
end
def
engine?
def
engine?
...
...
railties/lib/rails.rb
浏览文件 @
d6dec7fc
...
@@ -25,6 +25,7 @@ module Rails
...
@@ -25,6 +25,7 @@ module Rails
autoload
:Info
autoload
:Info
autoload
:InfoController
autoload
:InfoController
autoload
:MailersController
autoload
:WelcomeController
autoload
:WelcomeController
class
<<
self
class
<<
self
...
...
railties/lib/rails/application/finisher.rb
浏览文件 @
d6dec7fc
...
@@ -22,6 +22,8 @@ module Finisher
...
@@ -22,6 +22,8 @@ module Finisher
initializer
:add_builtin_route
do
|
app
|
initializer
:add_builtin_route
do
|
app
|
if
Rails
.
env
.
development?
if
Rails
.
env
.
development?
app
.
routes
.
append
do
app
.
routes
.
append
do
get
'/rails/mailers'
=>
"rails/mailers#index"
get
'/rails/mailers/*path'
=>
"rails/mailers#preview"
get
'/rails/info/properties'
=>
"rails/info#properties"
get
'/rails/info/properties'
=>
"rails/info#properties"
get
'/rails/info/routes'
=>
"rails/info#routes"
get
'/rails/info/routes'
=>
"rails/info#routes"
get
'/rails/info'
=>
"rails/info#index"
get
'/rails/info'
=>
"rails/info#index"
...
...
railties/lib/rails/generators/test_unit/mailer/mailer_generator.rb
浏览文件 @
d6dec7fc
...
@@ -4,11 +4,18 @@ module TestUnit # :nodoc:
...
@@ -4,11 +4,18 @@ module TestUnit # :nodoc:
module
Generators
# :nodoc:
module
Generators
# :nodoc:
class
MailerGenerator
<
Base
# :nodoc:
class
MailerGenerator
<
Base
# :nodoc:
argument
:actions
,
type: :array
,
default:
[],
banner:
"method method"
argument
:actions
,
type: :array
,
default:
[],
banner:
"method method"
check_class_collision
suffix:
"Test"
def
check_class_collision
class_collisions
"
#{
class_name
}
Test"
,
"
#{
class_name
}
Preview"
end
def
create_test_files
def
create_test_files
template
"functional_test.rb"
,
File
.
join
(
'test/mailers'
,
class_path
,
"
#{
file_name
}
_test.rb"
)
template
"functional_test.rb"
,
File
.
join
(
'test/mailers'
,
class_path
,
"
#{
file_name
}
_test.rb"
)
end
end
def
create_preview_files
template
"preview.rb"
,
File
.
join
(
'test/mailers/previews'
,
class_path
,
"
#{
file_name
}
_preview.rb"
)
end
end
end
end
end
end
end
railties/lib/rails/generators/test_unit/mailer/templates/preview.rb
0 → 100644
浏览文件 @
d6dec7fc
<
% module_namespacing
do
-
%>
class <%= class_name %>
Preview
<
ActionMailer
::
Preview
<
% actions.each
do
|
action
|
-
%>
def <%= action %>
<
%= class_name %>.<%=
action
%>
end
<% end -%>
end
<
% end
-
%>
railties/lib/rails/mailers_controller.rb
0 → 100644
浏览文件 @
d6dec7fc
require
'rails/application_controller'
class
Rails::MailersController
<
Rails
::
ApplicationController
# :nodoc:
prepend_view_path
ActionDispatch
::
DebugExceptions
::
RESCUES_TEMPLATE_PATH
before_filter
:require_local!
before_filter
:find_preview
,
only: :preview
def
index
@previews
=
ActionMailer
::
Preview
.
all
@page_title
=
"Mailer Previews"
end
def
preview
if
params
[
:path
]
==
@preview
.
preview_name
@page_title
=
"Mailer Previews for
#{
@preview
.
preview_name
}
"
render
action:
'mailer'
else
email
=
File
.
basename
(
params
[
:path
])
if
@preview
.
email_exists?
(
email
)
@email
=
@preview
.
call
(
email
)
if
params
[
:part
]
part_type
=
Mime
::
Type
.
lookup
(
params
[
:part
])
if
part
=
find_part
(
part_type
)
response
.
content_type
=
part_type
render
text:
part
.
respond_to?
(
:decoded
)
?
part
.
decoded
:
part
else
raise
AbstractController
::
ActionNotFound
,
"Email part '
#{
part_type
}
' not found in
#{
@preview
.
name
}
#
#{
email
}
"
end
else
@part
=
find_preferred_part
(
request
.
format
,
Mime
::
HTML
,
Mime
::
TEXT
)
render
action:
'email'
,
layout:
false
,
formats:
%w[html]
end
else
raise
AbstractController
::
ActionNotFound
,
"Email '
#{
email
}
' not found in
#{
@preview
.
name
}
"
end
end
end
protected
def
find_preview
candidates
=
[]
params
[
:path
].
to_s
.
scan
(
%r{/|$}
){
candidates
<<
$`
}
preview
=
candidates
.
detect
{
|
candidate
|
ActionMailer
::
Preview
.
exists?
(
candidate
)
}
if
preview
@preview
=
ActionMailer
::
Preview
.
find
(
preview
)
else
raise
AbstractController
::
ActionNotFound
,
"Mailer preview '
#{
params
[
:path
]
}
' not found"
end
end
def
find_preferred_part
(
*
formats
)
if
@email
.
multipart?
formats
.
each
do
|
format
|
return
find_part
(
format
)
if
@email
.
parts
.
any?
{
|
p
|
p
.
mime_type
==
format
}
end
else
@email
end
end
def
find_part
(
format
)
if
@email
.
multipart?
@email
.
parts
.
find
{
|
p
|
p
.
mime_type
==
format
}
elsif
@email
.
mime_type
==
format
@email
end
end
end
\ No newline at end of file
railties/lib/rails/templates/layouts/application.html.erb
浏览文件 @
d6dec7fc
...
@@ -29,7 +29,12 @@
...
@@ -29,7 +29,12 @@
</style>
</style>
</head>
</head>
<body>
<body>
<h2>
Your App:
<%=
link_to
'properties'
,
'/rails/info/properties'
%>
|
<%=
link_to
'routes'
,
'/rails/info/routes'
%>
</h2>
<h2>
Your App:
<%=
link_to
'mailers'
,
'/rails/mailers'
%>
|
<%=
link_to
'properties'
,
'/rails/info/properties'
%>
|
<%=
link_to
'routes'
,
'/rails/info/routes'
%>
</h2>
<%=
yield
%>
<%=
yield
%>
</body>
</body>
...
...
railties/lib/rails/templates/rails/mailers/email.html.erb
0 → 100644
浏览文件 @
d6dec7fc
<!DOCTYPE html>
<html><head>
<meta
name=
"viewport"
content=
"width=device-width"
/>
<style
type=
"text/css"
>
header
{
width
:
100%
;
padding
:
10px
0
0
0
;
margin
:
0
;
background
:
white
;
font
:
12px
"Lucida Grande"
,
sans-serif
;
border-bottom
:
1px
solid
#dedede
;
overflow
:
hidden
;
}
dl
{
margin
:
0
0
10px
0
;
padding
:
0
;
}
dt
{
width
:
80px
;
padding
:
1px
;
float
:
left
;
clear
:
left
;
text-align
:
right
;
color
:
#7f7f7f
;
}
dd
{
margin-left
:
90px
;
/* 80px + 10px */
padding
:
1px
;
}
iframe
{
border
:
0
;
width
:
100%
;
height
:
800px
;
}
</style>
</head>
<body>
<header>
<dl>
<%
if
@email
.
respond_to?
(
:smtp_envelope_from
)
&&
Array
(
@email
.
from
)
!=
Array
(
@email
.
smtp_envelope_from
)
%>
<dt>
SMTP-From:
</dt>
<dd>
<%=
@email
.
smtp_envelope_from
%>
</dd>
<%
end
%>
<%
if
@email
.
respond_to?
(
:smtp_envelope_to
)
&&
@email
.
to
!=
@email
.
smtp_envelope_to
%>
<dt>
SMTP-To:
</dt>
<dd>
<%=
@email
.
smtp_envelope_to
%>
</dd>
<%
end
%>
<dt>
From:
</dt>
<dd>
<%=
@email
.
header
[
'from'
]
%>
</dd>
<%
if
@email
.
reply_to
%>
<dt>
Reply-To:
</dt>
<dd>
<%=
@email
.
header
[
'reply-to'
]
%>
</dd>
<%
end
%>
<dt>
To:
</dt>
<dd>
<%=
@email
.
header
[
'to'
]
%>
</dd>
<%
if
@email
.
cc
%>
<dt>
CC:
</dt>
<dd>
<%=
@email
.
header
[
'cc'
]
%>
</dd>
<%
end
%>
<dt>
Date:
</dt>
<dd>
<%=
Time
.
current
.
rfc2822
%>
</dd>
<dt>
Subject:
</dt>
<dd><strong>
<%=
@email
.
subject
%>
</strong></dd>
<%
unless
@email
.
attachments
.
nil?
||
@email
.
attachments
.
empty?
%>
<dt>
Attachments:
</dt>
<dd>
<%=
@email
.
attachments
.
map
{
|
a
|
a
.
respond_to?
(
:original_filename
)
?
a
.
original_filename
:
a
.
filename
}.
inspect
%>
</dd>
<%
end
%>
<%
if
@email
.
multipart?
%>
<dd>
<select
onchange=
"document.getElementsByName('messageBody')[0].src=this.options[this.selectedIndex].value;"
>
<option
<%=
request
.
format
==
Mime
::
HTML
?
'selected'
:
''
%>
value=
"?part=text%2Fhtml"
>
View as HTML email
</option>
<option
<%=
request
.
format
==
Mime
::
TEXT
?
'selected'
:
''
%>
value=
"?part=text%2Fplain"
>
View as plain-text email
</option>
</select>
</dd>
<%
end
%>
</dl>
</header>
<iframe
seamless
name=
"messageBody"
src=
"?part=
<%=
Rack
::
Utils
.
escape
(
@part
.
mime_type
)
%>
"
></iframe>
</body>
</html>
\ No newline at end of file
railties/lib/rails/templates/rails/mailers/index.html.erb
0 → 100644
浏览文件 @
d6dec7fc
<%
@previews
.
each
do
|
preview
|
%>
<h3>
<%=
link_to
preview
.
preview_name
.
titleize
,
"/rails/mailers/
#{
preview
.
preview_name
}
"
%>
</h3>
<ul>
<%
preview
.
emails
.
each
do
|
email
|
%>
<li>
<%=
link_to
email
,
"/rails/mailers/
#{
preview
.
preview_name
}
/
#{
email
}
"
%>
</li>
<%
end
%>
</ul>
<%
end
%>
railties/lib/rails/templates/rails/mailers/mailer.html.erb
0 → 100644
浏览文件 @
d6dec7fc
<h3>
<%=
@preview
.
preview_name
.
titleize
%>
</h3>
<ul>
<%
@preview
.
emails
.
each
do
|
email
|
%>
<li>
<%=
link_to
email
,
"/rails/mailers/
#{
@preview
.
preview_name
}
/
#{
email
}
"
%>
</li>
<%
end
%>
</ul>
railties/test/application/mailer_previews_test.rb
0 → 100644
浏览文件 @
d6dec7fc
require
'isolation/abstract_unit'
require
'rack/test'
module
ApplicationTests
class
MailerPreviewsTest
<
ActiveSupport
::
TestCase
include
ActiveSupport
::
Testing
::
Isolation
include
Rack
::
Test
::
Methods
def
setup
build_app
boot_rails
end
def
teardown
teardown_app
end
test
"/rails/mailers is accessible in development"
do
app
(
"development"
)
get
"/rails/mailers"
assert_equal
200
,
last_response
.
status
end
test
"/rails/mailers is not accessible in production"
do
app
(
"production"
)
get
"/rails/mailers"
assert_equal
404
,
last_response
.
status
end
test
"mailer previews are loaded from the default preview_path"
do
mailer
'notifier'
,
<<-
RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo
mail to: "to@example.org"
end
end
RUBY
text_template
'notifier/foo'
,
<<-
RUBY
Hello, World!
RUBY
mailer_preview
'notifier'
,
<<-
RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
end
RUBY
app
(
'development'
)
get
"/rails/mailers"
assert_match
'<h3><a href="/rails/mailers/notifier">Notifier</a></h3>'
,
last_response
.
body
assert_match
'<li><a href="/rails/mailers/notifier/foo">foo</a></li>'
,
last_response
.
body
end
test
"mailer previews are loaded from a custom preview_path"
do
add_to_config
"config.action_mailer.preview_path = '
#{
app_path
}
/lib/mailer_previews'"
mailer
'notifier'
,
<<-
RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo
mail to: "to@example.org"
end
end
RUBY
text_template
'notifier/foo'
,
<<-
RUBY
Hello, World!
RUBY
app_file
'lib/mailer_previews/notifier_preview.rb'
,
<<-
RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
end
RUBY
app
(
'development'
)
get
"/rails/mailers"
assert_match
'<h3><a href="/rails/mailers/notifier">Notifier</a></h3>'
,
last_response
.
body
assert_match
'<li><a href="/rails/mailers/notifier/foo">foo</a></li>'
,
last_response
.
body
end
test
"mailer previews are reloaded across requests"
do
app
(
'development'
)
get
"/rails/mailers"
assert_no_match
'<h3><a href="/rails/mailers/notifier">Notifier</a></h3>'
,
last_response
.
body
mailer
'notifier'
,
<<-
RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo
mail to: "to@example.org"
end
end
RUBY
text_template
'notifier/foo'
,
<<-
RUBY
Hello, World!
RUBY
mailer_preview
'notifier'
,
<<-
RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
end
RUBY
get
"/rails/mailers"
assert_match
'<h3><a href="/rails/mailers/notifier">Notifier</a></h3>'
,
last_response
.
body
remove_file
'test/mailers/previews/notifier_preview.rb'
sleep
(
1
)
get
"/rails/mailers"
assert_no_match
'<h3><a href="/rails/mailers/notifier">Notifier</a></h3>'
,
last_response
.
body
end
test
"mailer preview actions are added and removed"
do
mailer
'notifier'
,
<<-
RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo
mail to: "to@example.org"
end
end
RUBY
text_template
'notifier/foo'
,
<<-
RUBY
Hello, World!
RUBY
mailer_preview
'notifier'
,
<<-
RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
end
RUBY
app
(
'development'
)
get
"/rails/mailers"
assert_match
'<h3><a href="/rails/mailers/notifier">Notifier</a></h3>'
,
last_response
.
body
assert_match
'<li><a href="/rails/mailers/notifier/foo">foo</a></li>'
,
last_response
.
body
assert_no_match
'<li><a href="/rails/mailers/notifier/bar">bar</a></li>'
,
last_response
.
body
mailer
'notifier'
,
<<-
RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo
mail to: "to@example.org"
end
def bar
mail to: "to@example.net"
end
end
RUBY
text_template
'notifier/foo'
,
<<-
RUBY
Hello, World!
RUBY
text_template
'notifier/bar'
,
<<-
RUBY
Goodbye, World!
RUBY
mailer_preview
'notifier'
,
<<-
RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
def bar
Notifier.bar
end
end
RUBY
sleep
(
1
)
get
"/rails/mailers"
assert_match
'<h3><a href="/rails/mailers/notifier">Notifier</a></h3>'
,
last_response
.
body
assert_match
'<li><a href="/rails/mailers/notifier/foo">foo</a></li>'
,
last_response
.
body
assert_match
'<li><a href="/rails/mailers/notifier/bar">bar</a></li>'
,
last_response
.
body
mailer
'notifier'
,
<<-
RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo
mail to: "to@example.org"
end
end
RUBY
remove_file
'app/views/notifier/bar.text.erb'
mailer_preview
'notifier'
,
<<-
RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
end
RUBY
sleep
(
1
)
get
"/rails/mailers"
assert_match
'<h3><a href="/rails/mailers/notifier">Notifier</a></h3>'
,
last_response
.
body
assert_match
'<li><a href="/rails/mailers/notifier/foo">foo</a></li>'
,
last_response
.
body
assert_no_match
'<li><a href="/rails/mailers/notifier/bar">bar</a></li>'
,
last_response
.
body
end
test
"mailer preview not found"
do
app
(
'development'
)
get
"/rails/mailers/notifier"
assert
last_response
.
not_found?
assert_match
"Mailer preview 'notifier' not found"
,
last_response
.
body
end
test
"mailer preview email not found"
do
mailer
'notifier'
,
<<-
RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo
mail to: "to@example.org"
end
end
RUBY
text_template
'notifier/foo'
,
<<-
RUBY
Hello, World!
RUBY
mailer_preview
'notifier'
,
<<-
RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
end
RUBY
app
(
'development'
)
get
"/rails/mailers/notifier/bar"
assert
last_response
.
not_found?
assert_match
"Email 'bar' not found in NotifierPreview"
,
last_response
.
body
end
test
"mailer preview email part not found"
do
mailer
'notifier'
,
<<-
RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo
mail to: "to@example.org"
end
end
RUBY
text_template
'notifier/foo'
,
<<-
RUBY
Hello, World!
RUBY
mailer_preview
'notifier'
,
<<-
RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
end
RUBY
app
(
'development'
)
get
"/rails/mailers/notifier/foo?part=text%2Fhtml"
assert
last_response
.
not_found?
assert_match
"Email part 'text/html' not found in NotifierPreview#foo"
,
last_response
.
body
end
test
"message header uses full display names"
do
mailer
'notifier'
,
<<-
RUBY
class Notifier < ActionMailer::Base
default from: "Ruby on Rails <core@rubyonrails.org>"
def foo
mail to: "Andrew White <andyw@pixeltrix.co.uk>",
cc: "David Heinemeier Hansson <david@heinemeierhansson.com>"
end
end
RUBY
text_template
'notifier/foo'
,
<<-
RUBY
Hello, World!
RUBY
mailer_preview
'notifier'
,
<<-
RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
end
RUBY
app
(
'development'
)
get
"/rails/mailers/notifier/foo"
assert_equal
200
,
last_response
.
status
assert_match
"Ruby on Rails <core@rubyonrails.org>"
,
last_response
.
body
assert_match
"Andrew White <andyw@pixeltrix.co.uk>"
,
last_response
.
body
assert_match
"David Heinemeier Hansson <david@heinemeierhansson.com>"
,
last_response
.
body
end
test
"part menu selects correct option"
do
mailer
'notifier'
,
<<-
RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo
mail to: "to@example.org"
end
end
RUBY
html_template
'notifier/foo'
,
<<-
RUBY
<p>Hello, World!</p>
RUBY
text_template
'notifier/foo'
,
<<-
RUBY
Hello, World!
RUBY
mailer_preview
'notifier'
,
<<-
RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
end
RUBY
app
(
'development'
)
get
"/rails/mailers/notifier/foo.html"
assert_equal
200
,
last_response
.
status
assert_match
'<option selected value="?part=text%2Fhtml">View as HTML email</option>'
,
last_response
.
body
get
"/rails/mailers/notifier/foo.txt"
assert_equal
200
,
last_response
.
status
assert_match
'<option selected value="?part=text%2Fplain">View as plain-text email</option>'
,
last_response
.
body
end
private
def
build_app
super
app_file
'config/routes.rb'
,
"Rails.application.routes.draw do; end"
end
def
mailer
(
name
,
contents
)
app_file
(
"app/mailers/
#{
name
}
.rb"
,
contents
)
end
def
mailer_preview
(
name
,
contents
)
app_file
(
"test/mailers/previews/
#{
name
}
_preview.rb"
,
contents
)
end
def
html_template
(
name
,
contents
)
app_file
(
"app/views/
#{
name
}
.html.erb"
,
contents
)
end
def
text_template
(
name
,
contents
)
app_file
(
"app/views/
#{
name
}
.text.erb"
,
contents
)
end
end
end
railties/test/generators/mailer_generator_test.rb
浏览文件 @
d6dec7fc
require
'generators/generators_test_helper'
require
'generators/generators_test_helper'
require
'rails/generators/mailer/mailer_generator'
require
'rails/generators/mailer/mailer_generator'
class
MailerGeneratorTest
<
Rails
::
Generators
::
TestCase
class
MailerGeneratorTest
<
Rails
::
Generators
::
TestCase
include
GeneratorsTestHelper
include
GeneratorsTestHelper
arguments
%w(notifier foo bar)
arguments
%w(notifier foo bar)
...
@@ -23,8 +22,11 @@ def test_mailer_with_i18n_helper
...
@@ -23,8 +22,11 @@ def test_mailer_with_i18n_helper
end
end
def
test_check_class_collision
def
test_check_class_collision
content
=
capture
(
:stderr
){
run_generator
[
"object"
]
}
Object
.
send
:const_set
,
:Notifier
,
Class
.
new
assert_match
(
/The name 'Object' is either already used in your application or reserved/
,
content
)
content
=
capture
(
:stderr
){
run_generator
}
assert_match
(
/The name 'Notifier' is either already used in your application or reserved/
,
content
)
ensure
Object
.
send
:remove_const
,
:Notifier
end
end
def
test_invokes_default_test_framework
def
test_invokes_default_test_framework
...
@@ -34,6 +36,31 @@ def test_invokes_default_test_framework
...
@@ -34,6 +36,31 @@ def test_invokes_default_test_framework
assert_match
(
/test "foo"/
,
test
)
assert_match
(
/test "foo"/
,
test
)
assert_match
(
/test "bar"/
,
test
)
assert_match
(
/test "bar"/
,
test
)
end
end
assert_file
"test/mailers/previews/notifier_preview.rb"
do
|
mailer
|
assert_match
(
/class NotifierPreview < ActionMailer::Preview/
,
mailer
)
assert_instance_method
:foo
,
mailer
do
|
foo
|
assert_match
(
/Notifier.foo/
,
foo
)
end
assert_instance_method
:bar
,
mailer
do
|
bar
|
assert_match
(
/Notifier.bar/
,
bar
)
end
end
end
def
test_check_test_class_collision
Object
.
send
:const_set
,
:NotifierTest
,
Class
.
new
content
=
capture
(
:stderr
){
run_generator
}
assert_match
(
/The name 'NotifierTest' is either already used in your application or reserved/
,
content
)
ensure
Object
.
send
:remove_const
,
:NotifierTest
end
def
test_check_preview_class_collision
Object
.
send
:const_set
,
:NotifierPreview
,
Class
.
new
content
=
capture
(
:stderr
){
run_generator
}
assert_match
(
/The name 'NotifierPreview' is either already used in your application or reserved/
,
content
)
ensure
Object
.
send
:remove_const
,
:NotifierPreview
end
end
def
test_invokes_default_template_engine
def
test_invokes_default_template_engine
...
@@ -65,6 +92,9 @@ def test_mailer_with_namedspaced_mailer
...
@@ -65,6 +92,9 @@ def test_mailer_with_namedspaced_mailer
assert_match
(
/class Farm::Animal < ActionMailer::Base/
,
mailer
)
assert_match
(
/class Farm::Animal < ActionMailer::Base/
,
mailer
)
assert_match
(
/en\.farm\.animal\.moos\.subject/
,
mailer
)
assert_match
(
/en\.farm\.animal\.moos\.subject/
,
mailer
)
end
end
assert_file
"test/mailers/previews/farm/animal_preview.rb"
do
|
mailer
|
assert_match
(
/class Farm::AnimalPreview < ActionMailer::Preview/
,
mailer
)
end
assert_file
"app/views/farm/animal/moos.text.erb"
assert_file
"app/views/farm/animal/moos.text.erb"
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录