Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
18616114
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,发现更多精彩内容 >>
提交
18616114
编写于
12月 08, 2013
作者:
D
David Heinemeier Hansson
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #13235 from strzalek/variants-inline
Inline syntax for variants
上级
cad9eb17
2647d2f6
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
28 addition
and
10 deletion
+28
-10
actionpack/lib/action_controller/metal/mime_responds.rb
actionpack/lib/action_controller/metal/mime_responds.rb
+15
-9
actionpack/test/controller/mime/respond_to_test.rb
actionpack/test/controller/mime/respond_to_test.rb
+13
-1
未找到文件。
actionpack/lib/action_controller/metal/mime_responds.rb
浏览文件 @
18616114
...
...
@@ -215,7 +215,7 @@ def respond_to(*mimes, &block)
raise
ArgumentError
,
"respond_to takes either types or a block, never both"
if
mimes
.
any?
&&
block_given?
if
collector
=
retrieve_collector_from_mimes
(
mimes
,
&
block
)
response
=
collector
.
response
(
request
.
variant
)
response
=
collector
.
response
response
?
response
.
call
:
render
({})
end
end
...
...
@@ -357,7 +357,7 @@ def respond_with(*resources, &block)
if
collector
=
retrieve_collector_from_mimes
(
&
block
)
options
=
resources
.
size
==
1
?
{}
:
resources
.
extract_options!
options
=
options
.
clone
options
[
:default_response
]
=
collector
.
response
(
request
.
variant
)
options
[
:default_response
]
=
collector
.
response
(
options
.
delete
(
:responder
)
||
self
.
class
.
responder
).
call
(
self
,
resources
,
options
)
end
end
...
...
@@ -390,7 +390,7 @@ def collect_mimes_from_class_level #:nodoc:
# is available.
def
retrieve_collector_from_mimes
(
mimes
=
nil
,
&
block
)
#:nodoc:
mimes
||=
collect_mimes_from_class_level
collector
=
Collector
.
new
(
mimes
)
collector
=
Collector
.
new
(
mimes
,
request
.
variant
)
block
.
call
(
collector
)
if
block_given?
format
=
collector
.
negotiate_format
(
request
)
...
...
@@ -428,9 +428,11 @@ class Collector
include
AbstractController
::
Collector
attr_accessor
:format
def
initialize
(
mimes
)
def
initialize
(
mimes
,
variant
=
nil
)
@responses
=
{}
mimes
.
each
{
|
mime
|
send
(
mime
)
}
@variant
=
variant
mimes
.
each
{
|
mime
|
@responses
[
"Mime::
#{
mime
.
upcase
}
"
.
constantize
]
=
nil
}
end
def
any
(
*
args
,
&
block
)
...
...
@@ -444,15 +446,19 @@ def any(*args, &block)
def
custom
(
mime_type
,
&
block
)
mime_type
=
Mime
::
Type
.
lookup
(
mime_type
.
to_s
)
unless
mime_type
.
is_a?
(
Mime
::
Type
)
@responses
[
mime_type
]
||=
block
@responses
[
mime_type
]
||=
if
block_given?
block
else
VariantFilter
.
new
(
@variant
)
end
end
def
response
(
variant
)
def
response
response
=
@responses
.
fetch
(
format
,
@responses
[
Mime
::
ALL
])
if
response
.
nil?
||
response
.
arity
==
0
if
response
.
is_a?
(
VariantFilter
)
||
response
.
nil?
||
response
.
arity
==
0
response
else
lambda
{
response
.
call
VariantFilter
.
new
(
variant
)
}
lambda
{
response
.
call
VariantFilter
.
new
(
@
variant
)
}
end
end
...
...
actionpack/test/controller/mime/respond_to_test.rb
浏览文件 @
18616114
...
...
@@ -175,6 +175,12 @@ def variant_plus_none_for_format
end
end
def
variant_inline_syntax
respond_to
do
|
format
|
format
.
html
.
phone
{
render
text:
"phone"
}
end
end
protected
def
set_layout
case
action_name
...
...
@@ -554,10 +560,16 @@ def test_multiple_variants_for_format
assert_equal
"tablet"
,
@response
.
body
end
def
test_no_variant_in_variant_setup
get
:variant_plus_none_for_format
assert_equal
"text/html"
,
@response
.
content_type
assert_equal
"none"
,
@response
.
body
end
def
test_variant_inline_syntax
@request
.
variant
=
:phone
get
:variant_inline_syntax
assert_equal
"text/html"
,
@response
.
content_type
assert_equal
"phone"
,
@response
.
body
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录