Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
f2dfa83f
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,发现更多精彩内容 >>
提交
f2dfa83f
编写于
2月 13, 2014
作者:
D
David Heinemeier Hansson
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #14043 from strzalek/variants-negotiation
Variant negotiation
上级
ea3af7ee
f9b6b865
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
51 addition
and
12 deletion
+51
-12
actionpack/lib/action_controller/metal/mime_responds.rb
actionpack/lib/action_controller/metal/mime_responds.rb
+20
-8
actionpack/lib/action_dispatch/http/mime_negotiation.rb
actionpack/lib/action_dispatch/http/mime_negotiation.rb
+4
-2
actionpack/test/controller/mime/respond_to_test.rb
actionpack/test/controller/mime/respond_to_test.rb
+21
-0
actionpack/test/dispatch/request_test.rb
actionpack/test/dispatch/request_test.rb
+5
-1
actionview/lib/action_view/rendering.rb
actionview/lib/action_view/rendering.rb
+1
-1
未找到文件。
actionpack/lib/action_controller/metal/mime_responds.rb
浏览文件 @
f2dfa83f
...
...
@@ -236,6 +236,18 @@ def clear_respond_to
# end
# end
#
# You can also set an array of variants:
#
# request.variant = [:tablet, :phone]
#
# which will work similarly to formats and MIME types negotiation. If there will be no
# :tablet variant declared, :phone variant will be picked:
#
# respond_to do |format|
# format.html.none
# format.html.phone # this gets rendered
# end
#
# Be sure to check the documentation of +respond_with+ and
# <tt>ActionController::MimeResponds.respond_to</tt> for more examples.
def
respond_to
(
*
mimes
,
&
block
)
...
...
@@ -488,7 +500,7 @@ def response
response
else
# `format.html{ |variant| variant.phone }` - variant block syntax
variant_collector
=
VariantCollector
.
new
(
@variant
)
response
.
call
(
variant_collector
)
#call format block with variants collector
response
.
call
(
variant_collector
)
#
call format block with variants collector
variant_collector
.
variant
end
end
...
...
@@ -519,15 +531,15 @@ def method_missing(name, *args, &block)
end
def
variant
key
=
if
@variant
.
nil?
:none
elsif
@variants
.
has_key?
(
@variant
)
@variant
if
@variant
.
nil?
@variants
[
:none
]
elsif
(
@variants
.
keys
&
@variant
).
any?
@variant
.
each
do
|
v
|
return
@variants
[
v
]
if
@variants
.
key?
(
v
)
end
else
:any
@variants
[
:any
]
end
@variants
[
key
]
end
end
end
...
...
actionpack/lib/action_dispatch/http/mime_negotiation.rb
浏览文件 @
f2dfa83f
...
...
@@ -68,10 +68,12 @@ def formats
# Sets the \variant for template.
def
variant
=
(
variant
)
if
variant
.
is_a?
Symbol
if
variant
.
is_a?
(
Symbol
)
@variant
=
[
variant
]
elsif
variant
.
is_a?
(
Array
)
@variant
=
variant
else
raise
ArgumentError
,
"request.variant must be set to a Symbol, not a
#{
variant
.
class
}
. "
\
raise
ArgumentError
,
"request.variant must be set to a Symbol
or Array
, not a
#{
variant
.
class
}
. "
\
"For security reasons, never directly set the variant to a user-provided value, "
\
"like params[:variant].to_sym. Check user-provided value against a whitelist first, "
\
"then set the variant: request.variant = :tablet if params[:variant] == 'tablet'"
...
...
actionpack/test/controller/mime/respond_to_test.rb
浏览文件 @
f2dfa83f
...
...
@@ -740,4 +740,25 @@ def test_format_any_variant_any
assert_equal
"text/javascript"
,
@response
.
content_type
assert_equal
"tablet"
,
@response
.
body
end
def
test_variant_negotiation_inline_syntax
@request
.
variant
=
[
:tablet
,
:phone
]
get
:variant_inline_syntax_without_block
assert_equal
"text/html"
,
@response
.
content_type
assert_equal
"phone"
,
@response
.
body
end
def
test_variant_negotiation_block_syntax
@request
.
variant
=
[
:tablet
,
:phone
]
get
:variant_plus_none_for_format
assert_equal
"text/html"
,
@response
.
content_type
assert_equal
"phone"
,
@response
.
body
end
def
test_variant_negotiation_without_block
@request
.
variant
=
[
:tablet
,
:phone
]
get
:variant_inline_syntax_without_block
assert_equal
"text/html"
,
@response
.
content_type
assert_equal
"phone"
,
@response
.
body
end
end
actionpack/test/dispatch/request_test.rb
浏览文件 @
f2dfa83f
...
...
@@ -846,8 +846,12 @@ def url_for(options = {})
test
"setting variant"
do
request
=
stub_request
request
.
variant
=
:mobile
assert_equal
:mobile
,
request
.
variant
assert_equal
[
:mobile
],
request
.
variant
request
.
variant
=
[
:phone
,
:tablet
]
assert_equal
[
:phone
,
:tablet
],
request
.
variant
end
test
"setting variant with non symbol value"
do
...
...
actionview/lib/action_view/rendering.rb
浏览文件 @
f2dfa83f
...
...
@@ -94,7 +94,7 @@ def _render_template(options) #:nodoc:
variant
=
options
[
:variant
]
lookup_context
.
rendered_format
=
nil
if
options
[
:formats
]
lookup_context
.
variants
=
[
variant
]
if
variant
lookup_context
.
variants
=
variant
if
variant
view_renderer
.
render
(
view_context
,
options
)
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录