Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
d0171673
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,发现更多精彩内容 >>
提交
d0171673
编写于
1月 13, 2010
作者:
J
Joshua Peek
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add router support for resources :only and :except actions
上级
5d787590
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
56 addition
and
16 deletion
+56
-16
actionpack/lib/action_dispatch/routing/mapper.rb
actionpack/lib/action_dispatch/routing/mapper.rb
+38
-16
actionpack/test/dispatch/routing_test.rb
actionpack/test/dispatch/routing_test.rb
+18
-0
未找到文件。
actionpack/lib/action_dispatch/routing/mapper.rb
浏览文件 @
d0171673
...
...
@@ -324,7 +324,11 @@ module Resources
CRUD_ACTIONS
=
[
:index
,
:show
,
:new
,
:edit
,
:create
,
:update
,
:destroy
]
class
Resource
#:nodoc:
attr_reader
:plural
,
:singular
def
self
.
default_actions
[
:index
,
:create
,
:new
,
:show
,
:update
,
:destroy
,
:edit
]
end
attr_reader
:plural
,
:singular
,
:options
def
initialize
(
entities
,
options
=
{})
entities
=
entities
.
to_s
...
...
@@ -334,8 +338,22 @@ def initialize(entities, options = {})
@singular
=
entities
.
singularize
end
def
default_actions
self
.
class
.
default_actions
end
def
actions
if
only
=
options
[
:only
]
only
.
map
(
&
:to_sym
)
elsif
except
=
options
[
:except
]
default_actions
-
except
.
map
(
&
:to_sym
)
else
default_actions
end
end
def
name
@
options
[
:as
]
||
plural
options
[
:as
]
||
plural
end
def
controller
...
...
@@ -356,12 +374,16 @@ def id_segment
end
class
SingletonResource
<
Resource
#:nodoc:
def
self
.
default_actions
[
:show
,
:create
,
:update
,
:destroy
,
:new
,
:edit
]
end
def
initialize
(
entity
,
options
=
{})
super
end
def
name
@
options
[
:as
]
||
singular
options
[
:as
]
||
singular
end
end
...
...
@@ -387,12 +409,12 @@ def resource(*resources, &block)
with_scope_level
(
:resource
,
resource
)
do
yield
if
block_given?
get
"(.:format)"
,
:to
=>
:show
,
:as
=>
resource
.
member_name
post
"(.:format)"
,
:to
=>
:create
put
"(.:format)"
,
:to
=>
:update
delete
"(.:format)"
,
:to
=>
:destroy
get
"/new(.:format)"
,
:to
=>
:new
,
:as
=>
"new_
#{
resource
.
singular
}
"
get
"/edit(.:format)"
,
:to
=>
:edit
,
:as
=>
"edit_
#{
resource
.
singular
}
"
get
"(.:format)"
,
:to
=>
:show
,
:as
=>
resource
.
member_name
if
resource
.
actions
.
include?
(
:show
)
post
"(.:format)"
,
:to
=>
:create
if
resource
.
actions
.
include?
(
:create
)
put
"(.:format)"
,
:to
=>
:update
if
resource
.
actions
.
include?
(
:update
)
delete
"(.:format)"
,
:to
=>
:destroy
if
resource
.
actions
.
include?
(
:destroy
)
get
"/new(.:format)"
,
:to
=>
:new
,
:as
=>
"new_
#{
resource
.
singular
}
"
if
resource
.
actions
.
include?
(
:new
)
get
"/edit(.:format)"
,
:to
=>
:edit
,
:as
=>
"edit_
#{
resource
.
singular
}
"
if
resource
.
actions
.
include?
(
:edit
)
end
end
...
...
@@ -422,22 +444,22 @@ def resources(*resources, &block)
yield
if
block_given?
with_scope_level
(
:collection
)
do
get
"(.:format)"
,
:to
=>
:index
,
:as
=>
resource
.
collection_name
post
"(.:format)"
,
:to
=>
:create
get
"(.:format)"
,
:to
=>
:index
,
:as
=>
resource
.
collection_name
if
resource
.
actions
.
include?
(
:index
)
post
"(.:format)"
,
:to
=>
:create
if
resource
.
actions
.
include?
(
:create
)
with_exclusive_name_prefix
:new
do
get
"/new(.:format)"
,
:to
=>
:new
,
:as
=>
resource
.
singular
get
"/new(.:format)"
,
:to
=>
:new
,
:as
=>
resource
.
singular
if
resource
.
actions
.
include?
(
:new
)
end
end
with_scope_level
(
:member
)
do
scope
(
"/:id"
)
do
get
"(.:format)"
,
:to
=>
:show
,
:as
=>
resource
.
member_name
put
"(.:format)"
,
:to
=>
:update
delete
"(.:format)"
,
:to
=>
:destroy
get
"(.:format)"
,
:to
=>
:show
,
:as
=>
resource
.
member_name
if
resource
.
actions
.
include?
(
:show
)
put
"(.:format)"
,
:to
=>
:update
if
resource
.
actions
.
include?
(
:update
)
delete
"(.:format)"
,
:to
=>
:destroy
if
resource
.
actions
.
include?
(
:destroy
)
with_exclusive_name_prefix
:edit
do
get
"/edit(.:format)"
,
:to
=>
:edit
,
:as
=>
resource
.
singular
get
"/edit(.:format)"
,
:to
=>
:edit
,
:as
=>
resource
.
singular
if
resource
.
actions
.
include?
(
:edit
)
end
end
end
...
...
actionpack/test/dispatch/routing_test.rb
浏览文件 @
d0171673
...
...
@@ -93,6 +93,8 @@ def self.matches?(request)
end
end
resources
:posts
,
:only
=>
[
:index
,
:show
]
match
'sprockets.js'
=>
::
TestRoutingMapper
::
SprocketsApp
match
'people/:id/update'
,
:to
=>
'people#update'
,
:as
=>
:update_person
...
...
@@ -421,6 +423,22 @@ def test_projects_posts
end
end
def
test_posts
with_test_routes
do
get
'/posts'
assert_equal
'posts#index'
,
@response
.
body
assert_equal
'/posts'
,
posts_path
get
'/posts/1'
assert_equal
'posts#show'
,
@response
.
body
assert_equal
'/posts/1'
,
post_path
(
:id
=>
1
)
assert_raise
(
ActionController
::
RoutingError
)
{
post
'/posts'
}
assert_raise
(
ActionController
::
RoutingError
)
{
put
'/posts/1'
}
assert_raise
(
ActionController
::
RoutingError
)
{
delete
'/posts/1'
}
end
end
def
test_sprockets
with_test_routes
do
get
'/sprockets.js'
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录