Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
bf59717b
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,发现更多精彩内容 >>
提交
bf59717b
编写于
6月 19, 2010
作者:
A
Andrew White
提交者:
Jeremy Kemper
6月 19, 2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Custom resource routes should be scoped
[#3765] Signed-off-by:
N
Jeremy Kemper
<
jeremy@bitsweat.net
>
上级
2c2a5fe8
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
83 addition
and
7 deletion
+83
-7
actionpack/lib/action_dispatch/routing/mapper.rb
actionpack/lib/action_dispatch/routing/mapper.rb
+36
-7
actionpack/test/dispatch/routing_test.rb
actionpack/test/dispatch/routing_test.rb
+47
-0
未找到文件。
actionpack/lib/action_dispatch/routing/mapper.rb
浏览文件 @
bf59717b
...
...
@@ -647,7 +647,9 @@ def new
with_scope_level
(
:new
)
do
scope
(
*
parent_resource
.
new_scope
)
do
yield
scope
(
action_path
(
:new
))
do
yield
end
end
end
end
...
...
@@ -723,7 +725,17 @@ def match(*args)
options
=
options_for_action
(
args
.
first
,
options
)
with_exclusive_scope
do
return
match
(
path
,
options
)
return
super
(
path
,
options
)
end
elsif
resource_method_scope?
path
=
path_for_custom_action
options
[
:as
]
=
name_for_action
(
options
[
:as
])
if
options
[
:as
]
args
.
push
(
options
)
with_exclusive_scope
do
scope
(
path
)
do
return
super
end
end
end
...
...
@@ -737,7 +749,7 @@ def match(*args)
def
root
(
options
=
{})
if
@scope
[
:scope_level
]
==
:resources
with_scope_level
(
:
collection
)
do
with_scope_level
(
:
nested
)
do
scope
(
parent_resource
.
path
,
:name_prefix
=>
parent_resource
.
collection_name
)
do
super
(
options
)
end
...
...
@@ -780,12 +792,18 @@ def resource_scope?
[
:resource
,
:resources
].
include?
(
@scope
[
:scope_level
])
end
def
resource_method_scope?
[
:collection
,
:member
,
:new
].
include?
(
@scope
[
:scope_level
])
end
def
with_exclusive_scope
begin
old_name_prefix
,
old_path
=
@scope
[
:name_prefix
],
@scope
[
:path
]
@scope
[
:name_prefix
],
@scope
[
:path
]
=
nil
,
nil
yield
with_scope_level
(
:exclusive
)
do
yield
end
ensure
@scope
[
:name_prefix
],
@scope
[
:path
]
=
old_name_prefix
,
old_path
end
...
...
@@ -844,10 +862,8 @@ def path_for_action(action, path_names)
end
else
case
@scope
[
:scope_level
]
when
:collection
when
:collection
,
:new
"
#{
@scope
[
:path
]
}
/
#{
action_path
(
action
)
}
(.:format)"
when
:new
"
#{
@scope
[
:path
]
}
/
#{
action_path
(
:new
)
}
/
#{
action_path
(
action
)
}
(.:format)"
else
if
parent_resource
.
shallow?
"
#{
@scope
[
:module
]
}
/
#{
parent_resource
.
path
}
/:id/
#{
action_path
(
action
)
}
(.:format)"
...
...
@@ -858,6 +874,19 @@ def path_for_action(action, path_names)
end
end
def
path_for_custom_action
case
@scope
[
:scope_level
]
when
:collection
,
:new
@scope
[
:path
]
else
if
parent_resource
.
shallow?
"
#{
@scope
[
:module
]
}
/
#{
parent_resource
.
path
}
/:id"
else
@scope
[
:path
]
end
end
end
def
action_path
(
name
,
path_names
=
nil
)
path_names
||=
@scope
[
:path_names
]
path_names
[
name
.
to_sym
]
||
name
.
to_s
...
...
actionpack/test/dispatch/routing_test.rb
浏览文件 @
bf59717b
...
...
@@ -180,6 +180,33 @@ def self.matches?(request)
end
end
resources
:customers
do
get
"recent"
=>
"customers#recent"
,
:as
=>
:recent
,
:on
=>
:collection
get
"profile"
=>
"customers#profile"
,
:as
=>
:profile
,
:on
=>
:member
post
"preview"
=>
"customers#preview"
,
:as
=>
:preview
,
:on
=>
:new
resource
:avatar
do
get
"thumbnail(.:format)"
=>
"avatars#thumbnail"
,
:as
=>
:thumbnail
,
:on
=>
:member
end
resources
:invoices
do
get
"outstanding"
=>
"invoices#outstanding"
,
:as
=>
:outstanding
,
:on
=>
:collection
get
"overdue"
,
:to
=>
:overdue
,
:on
=>
:collection
get
"print"
=>
"invoices#print"
,
:as
=>
:print
,
:on
=>
:member
post
"preview"
=>
"invoices#preview"
,
:as
=>
:preview
,
:on
=>
:new
end
resources
:notes
,
:shallow
=>
true
do
get
"preview"
=>
"notes#preview"
,
:as
=>
:preview
,
:on
=>
:new
get
"print"
=>
"notes#print"
,
:as
=>
:print
,
:on
=>
:member
end
end
namespace
:api
do
resources
:customers
do
get
"recent"
=>
"customers#recent"
,
:as
=>
:recent
,
:on
=>
:collection
get
"profile"
=>
"customers#profile"
,
:as
=>
:profile
,
:on
=>
:member
post
"preview"
=>
"customers#preview"
,
:as
=>
:preview
,
:on
=>
:new
end
end
match
'sprockets.js'
=>
::
TestRoutingMapper
::
SprocketsApp
match
'people/:id/update'
,
:to
=>
'people#update'
,
:as
=>
:update_person
...
...
@@ -1295,6 +1322,26 @@ def test_shallow_nested_resources
end
end
def
test_custom_resource_routes_are_scoped
with_test_routes
do
assert_equal
'/customers/recent'
,
recent_customers_path
assert_equal
'/customers/1/profile'
,
profile_customer_path
(
:id
=>
'1'
)
assert_equal
'/customers/new/preview'
,
preview_new_customer_path
assert_equal
'/customers/1/avatar/thumbnail.jpg'
,
thumbnail_customer_avatar_path
(
:customer_id
=>
'1'
,
:format
=>
:jpg
)
assert_equal
'/customers/1/invoices/outstanding'
,
outstanding_customer_invoices_path
(
:customer_id
=>
'1'
)
assert_equal
'/customers/1/invoices/2/print'
,
print_customer_invoice_path
(
:customer_id
=>
'1'
,
:id
=>
'2'
)
assert_equal
'/customers/1/invoices/new/preview'
,
preview_new_customer_invoice_path
(
:customer_id
=>
'1'
)
assert_equal
'/customers/1/notes/new/preview'
,
preview_new_customer_note_path
(
:customer_id
=>
'1'
)
assert_equal
'/notes/1/print'
,
print_note_path
(
:id
=>
'1'
)
assert_equal
'/api/customers/recent'
,
recent_api_customers_path
assert_equal
'/api/customers/1/profile'
,
profile_api_customer_path
(
:id
=>
'1'
)
assert_equal
'/api/customers/new/preview'
,
preview_new_api_customer_path
get
'/customers/1/invoices/overdue'
assert_equal
'invoices#overdue'
,
@response
.
body
end
end
private
def
with_test_routes
yield
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录