Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ddfc0725
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,发现更多精彩内容 >>
提交
ddfc0725
编写于
1月 23, 2010
作者:
J
José Valim and Mikel Lindsaar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added AbstractController::Collector.
上级
1ea84c38
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
91 addition
and
26 deletion
+91
-26
actionpack/lib/abstract_controller.rb
actionpack/lib/abstract_controller.rb
+1
-0
actionpack/lib/abstract_controller/collector.rb
actionpack/lib/abstract_controller/collector.rb
+30
-0
actionpack/lib/action_controller/metal/mime_responds.rb
actionpack/lib/action_controller/metal/mime_responds.rb
+3
-26
actionpack/test/abstract/collector_test.rb
actionpack/test/abstract/collector_test.rb
+57
-0
未找到文件。
actionpack/lib/abstract_controller.rb
浏览文件 @
ddfc0725
...
...
@@ -10,6 +10,7 @@ module AbstractController
autoload
:Base
autoload
:Callbacks
autoload
:Collector
autoload
:Helpers
autoload
:Layouts
autoload
:LocalizedCache
...
...
actionpack/lib/abstract_controller/collector.rb
0 → 100644
浏览文件 @
ddfc0725
module
AbstractController
module
Collector
def
self
.
generate_method_for_mime
(
mime
)
sym
=
mime
.
is_a?
(
Symbol
)
?
mime
:
mime
.
to_sym
const
=
sym
.
to_s
.
upcase
class_eval
<<-
RUBY
,
__FILE__
,
__LINE__
+
1
def
#{
sym
}
(*args, &block) # def html(*args, &block)
custom(Mime::
#{
const
}
, *args, &block) # custom(Mime::HTML, *args, &block)
end # end
RUBY
end
Mime
::
SET
.
each
do
|
mime
|
generate_method_for_mime
(
mime
)
end
protected
def
method_missing
(
symbol
,
&
block
)
mime_constant
=
Mime
.
const_get
(
symbol
.
to_s
.
upcase
)
if
Mime
::
SET
.
include?
(
mime_constant
)
AbstractController
::
Collector
.
generate_method_for_mime
(
mime_constant
)
send
(
symbol
,
&
block
)
else
super
end
end
end
end
\ No newline at end of file
actionpack/lib/action_controller/metal/mime_responds.rb
浏览文件 @
ddfc0725
require
'abstract_controller/collector'
module
ActionController
#:nodoc:
module
MimeResponds
#:nodoc:
extend
ActiveSupport
::
Concern
...
...
@@ -265,6 +267,7 @@ def retrieve_response_from_mimes(mimes=nil, &block)
end
class
Collector
#:nodoc:
include
AbstractController
::
Collector
attr_accessor
:order
def
initialize
(
&
block
)
...
...
@@ -289,32 +292,6 @@ def custom(mime_type, &block)
def
response_for
(
mime
)
@responses
[
mime
]
||
@responses
[
Mime
::
ALL
]
||
@default_response
end
def
self
.
generate_method_for_mime
(
mime
)
sym
=
mime
.
is_a?
(
Symbol
)
?
mime
:
mime
.
to_sym
const
=
sym
.
to_s
.
upcase
class_eval
<<-
RUBY
,
__FILE__
,
__LINE__
+
1
def
#{
sym
}
(&block) # def html(&block)
custom(Mime::
#{
const
}
, &block) # custom(Mime::HTML, &block)
end # end
RUBY
end
Mime
::
SET
.
each
do
|
mime
|
generate_method_for_mime
(
mime
)
end
def
method_missing
(
symbol
,
&
block
)
mime_constant
=
Mime
.
const_get
(
symbol
.
to_s
.
upcase
)
if
Mime
::
SET
.
include?
(
mime_constant
)
self
.
class
.
generate_method_for_mime
(
mime_constant
)
send
(
symbol
,
&
block
)
else
super
end
end
end
end
end
actionpack/test/abstract/collector_test.rb
0 → 100644
浏览文件 @
ddfc0725
require
'abstract_unit'
module
AbstractController
module
Testing
class
MyCollector
include
Collector
attr_accessor
:responses
def
initialize
@responses
=
[]
end
def
custom
(
mime
,
*
args
,
&
block
)
@responses
<<
[
mime
,
args
,
block
]
end
end
class
TestCollector
<
ActiveSupport
::
TestCase
test
"responds to default mime types"
do
collector
=
MyCollector
.
new
assert_respond_to
collector
,
:html
assert_respond_to
collector
,
:text
end
test
"does not respond to unknown mime types"
do
collector
=
MyCollector
.
new
assert
!
collector
.
respond_to?
(
:unknown
)
end
test
"register mime types on method missing"
do
AbstractController
::
Collector
.
send
(
:remove_method
,
:js
)
collector
=
MyCollector
.
new
assert
!
collector
.
respond_to?
(
:js
)
collector
.
js
assert_respond_to
collector
,
:js
end
test
"does not register unknown mime types"
do
collector
=
MyCollector
.
new
assert_raise
NameError
do
collector
.
unknown
end
end
test
"generated methods call custom with args received"
do
collector
=
MyCollector
.
new
collector
.
html
collector
.
text
(
:foo
)
collector
.
js
(
:bar
)
{
:baz
}
assert_equal
[
Mime
::
HTML
,
[],
nil
],
collector
.
responses
[
0
]
assert_equal
[
Mime
::
TEXT
,
[
:foo
],
nil
],
collector
.
responses
[
1
]
assert_equal
[
Mime
::
JS
,
[
:bar
]],
collector
.
responses
[
2
][
0
,
2
]
assert_equal
:baz
,
collector
.
responses
[
2
][
2
].
call
end
end
end
end
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录