Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3f78de67
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,发现更多精彩内容 >>
提交
3f78de67
编写于
8月 29, 2009
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Ensure that blocks are also handled inside the responder.
上级
684a6b3c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
43 addition
and
23 deletion
+43
-23
actionpack/lib/action_controller/metal/mime_responds.rb
actionpack/lib/action_controller/metal/mime_responds.rb
+16
-18
actionpack/lib/action_controller/metal/responder.rb
actionpack/lib/action_controller/metal/responder.rb
+20
-5
actionpack/test/controller/mime_responds_test.rb
actionpack/test/controller/mime_responds_test.rb
+7
-0
未找到文件。
actionpack/lib/action_controller/metal/mime_responds.rb
浏览文件 @
3f78de67
...
...
@@ -178,7 +178,10 @@ def clear_respond_to
#
def
respond_to
(
*
mimes
,
&
block
)
raise
ArgumentError
,
"respond_to takes either types or a block, never both"
if
mimes
.
any?
&&
block_given?
collect_mimes_for_render
(
mimes
,
block
){
default_render
}
if
response
=
retrieve_response_from_mimes
(
mimes
,
&
block
)
response
.
call
end
end
# respond_with wraps a resource around a responder for default representation.
...
...
@@ -211,8 +214,9 @@ def respond_to(*mimes, &block)
# a proc to it.
#
def
respond_with
(
*
resources
,
&
block
)
collect_mimes_for_render
([],
block
)
do
if
response
=
retrieve_response_from_mimes
([],
&
block
)
options
=
resources
.
extract_options!
options
.
merge!
(
:default_response
=>
response
)
(
options
.
delete
(
:responder
)
||
responder
).
call
(
self
,
resources
,
options
)
end
end
...
...
@@ -242,34 +246,29 @@ def collect_mimes_from_class_level #:nodoc:
end
end
# Receives a collection of mimes and a block with formats and initialize a
# collector. If a response was added to the collector, uses it to satisfy
# the request, otherwise yields the block given.
# Collects mimes and return the response for the negotiated format. Returns
# nil if :not_acceptable was sent to the client.
#
def
collect_mimes_for_render
(
mimes
,
formats
)
collector
=
Collector
.
new
def
retrieve_response_from_mimes
(
mimes
,
&
block
)
collector
=
Collector
.
new
{
default_render
}
mimes
=
collect_mimes_from_class_level
if
mimes
.
empty?
mimes
.
each
{
|
mime
|
collector
.
send
(
mime
)
}
formats
.
call
(
collector
)
if
formats
block
.
call
(
collector
)
if
block_given?
if
format
=
request
.
negotiate_mime
(
collector
.
order
)
self
.
formats
=
[
format
.
to_sym
]
if
response
=
collector
.
response_for
(
format
)
response
.
call
else
yield
end
collector
.
response_for
(
format
)
else
head
:not_acceptable
nil
end
end
class
Collector
#:nodoc:
attr_accessor
:order
def
initialize
@order
,
@responses
=
[],
{}
def
initialize
(
&
block
)
@order
,
@responses
,
@default_response
=
[],
{},
block
end
def
any
(
*
args
,
&
block
)
...
...
@@ -283,13 +282,12 @@ def any(*args, &block)
def
custom
(
mime_type
,
&
block
)
mime_type
=
mime_type
.
is_a?
(
Mime
::
Type
)
?
mime_type
:
Mime
::
Type
.
lookup
(
mime_type
.
to_s
)
@order
<<
mime_type
@responses
[
mime_type
]
||=
block
end
def
response_for
(
mime
)
@responses
[
mime
]
||
@responses
[
Mime
::
ALL
]
@responses
[
mime
]
||
@responses
[
Mime
::
ALL
]
||
@default_response
end
def
self
.
generate_method_for_mime
(
mime
)
...
...
actionpack/lib/action_controller/metal/responder.rb
浏览文件 @
3f78de67
...
...
@@ -79,15 +79,16 @@ module ActionController #:nodoc:
# Check polymorphic_url documentation for more examples.
#
class
Responder
attr_reader
:controller
,
:request
,
:format
,
:resource
,
:resource
_location
,
:options
attr_reader
:controller
,
:request
,
:format
,
:resource
,
:resource
s
,
:options
def
initialize
(
controller
,
resources
,
options
=
{})
@controller
=
controller
@request
=
controller
.
request
@format
=
controller
.
formats
.
first
@resource
=
resources
.
is_a?
(
Array
)
?
resources
.
last
:
resources
@resource
_location
=
options
[
:location
]
||
resources
@resource
s
=
resources
@options
=
options
@default_response
=
options
.
delete
(
:default_response
)
end
delegate
:head
,
:render
,
:redirect_to
,
:to
=>
:controller
...
...
@@ -109,7 +110,7 @@ def self.call(*args)
# template.
#
def
to_html
render
default_
render
rescue
ActionView
::
MissingTemplate
if
get?
raise
...
...
@@ -125,7 +126,7 @@ def to_html
# responds to :to_format and display it.
#
def
to_format
render
default_
render
rescue
ActionView
::
MissingTemplate
raise
unless
resourceful?
...
...
@@ -148,6 +149,20 @@ def resourceful?
resource
.
respond_to?
(
:"to_
#{
format
}
"
)
end
# Returns the resource location by retrieving it from the options or
# returning the resources array.
#
def
resource_location
options
[
:location
]
||
resources
end
# If a given response block was given, use it, otherwise call render on
# controller.
#
def
default_render
@default_response
.
call
end
# display is just a shortcut to render a resource with the current format.
#
# display @user, :status => :ok
...
...
@@ -166,7 +181,7 @@ def resourceful?
# render :xml => @user, :status => :created
#
def
display
(
resource
,
given_options
=
{})
render
given_options
.
merge!
(
options
).
merge!
(
format
=>
resource
)
controller
.
render
given_options
.
merge!
(
options
).
merge!
(
format
=>
resource
)
end
# Check if the resource has errors or not.
...
...
actionpack/test/controller/mime_responds_test.rb
浏览文件 @
3f78de67
...
...
@@ -726,6 +726,13 @@ def test_first_in_respond_to_has_higher_priority
assert_equal
"<name>david</name>"
,
@response
.
body
end
def
test_block_inside_respond_with_is_rendered
@controller
=
InheritedRespondWithController
.
new
@request
.
accept
=
"application/json"
get
:index
assert_equal
"JSON"
,
@response
.
body
end
def
test_no_double_render_is_raised
@request
.
accept
=
"text/html"
assert_raise
ActionView
::
MissingTemplate
do
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录