Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
435bccda
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,发现更多精彩内容 >>
提交
435bccda
编写于
12月 19, 2010
作者:
J
John Firebaugh
提交者:
José Valim
12月 20, 2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Replace AD::Callbacks.to_prepare with AD::Reloader.to_prepare
Signed-off-by:
N
José Valim
<
jose.valim@gmail.com
>
上级
0f7c970e
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
34 addition
and
96 deletion
+34
-96
actionpack/lib/action_dispatch/middleware/callbacks.rb
actionpack/lib/action_dispatch/middleware/callbacks.rb
+6
-25
actionpack/test/dispatch/callbacks_test.rb
actionpack/test/dispatch/callbacks_test.rb
+13
-59
activerecord/lib/active_record/railtie.rb
activerecord/lib/active_record/railtie.rb
+1
-1
activesupport/lib/active_support/file_update_checker.rb
activesupport/lib/active_support/file_update_checker.rb
+1
-1
activesupport/lib/active_support/i18n_railtie.rb
activesupport/lib/active_support/i18n_railtie.rb
+1
-1
railties/lib/rails/application.rb
railties/lib/rails/application.rb
+1
-1
railties/lib/rails/application/finisher.rb
railties/lib/rails/application/finisher.rb
+6
-2
railties/lib/rails/console/app.rb
railties/lib/rails/console/app.rb
+1
-2
railties/test/application/console_test.rb
railties/test/application/console_test.rb
+4
-4
未找到文件。
actionpack/lib/action_dispatch/middleware/callbacks.rb
浏览文件 @
435bccda
module
ActionDispatch
# Provide callbacks to be executed before and after the request dispatch.
#
# It also provides a to_prepare callback, which is performed in all requests
# in development by only once in production and notification callback for async
# operations.
#
class
Callbacks
include
ActiveSupport
::
Callbacks
define_callbacks
:call
,
:rescuable
=>
true
define_callbacks
:prepare
,
:scope
=>
:name
# Add a preparation callback. Preparation callbacks are run before every
# request in development mode, and before the first request in production mode.
#
# If a symbol with a block is given, the symbol is used as an identifier.
# That allows to_prepare to be called again with the same identifier to
# replace the existing callback. Passing an identifier is a suggested
# practice if the code adding a preparation block may be reloaded.
def
self
.
to_prepare
(
*
args
,
&
block
)
first_arg
=
args
.
first
if
first_arg
.
is_a?
(
Symbol
)
&&
block_given?
remove_method
:"__
#{
first_arg
}
"
if
method_defined?
(
:"__
#{
first_arg
}
"
)
define_method
:"__
#{
first_arg
}
"
,
&
block
set_callback
(
:prepare
,
:"__
#{
first_arg
}
"
)
else
set_callback
(
:prepare
,
*
args
,
&
block
)
end
ActiveSupport
::
Deprecation
.
warn
"ActionDispatch::Callbacks.to_prepare is deprecated. "
<<
"Please use ActionDispatch::Reloader.to_prepare instead."
ActionDispatch
::
Reloader
.
to_prepare
(
*
args
,
&
block
)
end
def
self
.
before
(
*
args
,
&
block
)
...
...
@@ -37,14 +19,13 @@ def self.after(*args, &block)
set_callback
(
:call
,
:after
,
*
args
,
&
block
)
end
def
initialize
(
app
,
prepare_each_request
=
false
)
@app
,
@prepare_each_request
=
app
,
prepare_each_request
_run_prepare_callbacks
def
initialize
(
app
,
unused
=
nil
)
ActiveSupport
::
Deprecation
.
warn
"Passing a second argument to ActionDispatch::Callbacks.new is deprecated."
unless
unused
.
nil?
@app
=
app
end
def
call
(
env
)
_run_call_callbacks
do
_run_prepare_callbacks
if
@prepare_each_request
@app
.
call
(
env
)
end
end
...
...
actionpack/test/dispatch/callbacks_test.rb
浏览文件 @
435bccda
require
'abstract_unit'
class
DispatcherTest
<
Test
::
Uni
t
::
TestCase
class
DispatcherTest
<
ActiveSuppor
t
::
TestCase
class
Foo
cattr_accessor
:a
,
:b
end
...
...
@@ -13,65 +13,9 @@ def call(env)
def
setup
Foo
.
a
,
Foo
.
b
=
0
,
0
ActionDispatch
::
Callbacks
.
reset_callbacks
(
:prepare
)
ActionDispatch
::
Callbacks
.
reset_callbacks
(
:call
)
end
def
test_prepare_callbacks_with_cache_classes
a
=
b
=
c
=
nil
ActionDispatch
::
Callbacks
.
to_prepare
{
|*
args
|
a
=
b
=
c
=
1
}
ActionDispatch
::
Callbacks
.
to_prepare
{
|*
args
|
b
=
c
=
2
}
ActionDispatch
::
Callbacks
.
to_prepare
{
|*
args
|
c
=
3
}
# Ensure to_prepare callbacks are not run when defined
assert_nil
a
||
b
||
c
# Run callbacks
dispatch
assert_equal
1
,
a
assert_equal
2
,
b
assert_equal
3
,
c
# Make sure they are only run once
a
=
b
=
c
=
nil
dispatch
assert_nil
a
||
b
||
c
end
def
test_prepare_callbacks_without_cache_classes
a
=
b
=
c
=
nil
ActionDispatch
::
Callbacks
.
to_prepare
{
|*
args
|
a
=
b
=
c
=
1
}
ActionDispatch
::
Callbacks
.
to_prepare
{
|*
args
|
b
=
c
=
2
}
ActionDispatch
::
Callbacks
.
to_prepare
{
|*
args
|
c
=
3
}
# Ensure to_prepare callbacks are not run when defined
assert_nil
a
||
b
||
c
# Run callbacks
dispatch
(
false
)
assert_equal
1
,
a
assert_equal
2
,
b
assert_equal
3
,
c
# Make sure they are run again
a
=
b
=
c
=
nil
dispatch
(
false
)
assert_equal
1
,
a
assert_equal
2
,
b
assert_equal
3
,
c
end
def
test_to_prepare_with_identifier_replaces
ActionDispatch
::
Callbacks
.
to_prepare
(
:unique_id
)
{
|*
args
|
Foo
.
a
,
Foo
.
b
=
1
,
1
}
ActionDispatch
::
Callbacks
.
to_prepare
(
:unique_id
)
{
|*
args
|
Foo
.
a
=
2
}
dispatch
assert_equal
2
,
Foo
.
a
assert_equal
0
,
Foo
.
b
end
def
test_before_and_after_callbacks
ActionDispatch
::
Callbacks
.
before
{
|*
args
|
Foo
.
a
+=
1
;
Foo
.
b
+=
1
}
ActionDispatch
::
Callbacks
.
after
{
|*
args
|
Foo
.
a
+=
1
;
Foo
.
b
+=
1
}
...
...
@@ -85,10 +29,20 @@ def test_before_and_after_callbacks
assert_equal
4
,
Foo
.
b
end
def
test_to_prepare_deprecation
prepared
=
false
assert_deprecated
do
ActionDispatch
::
Callbacks
.
to_prepare
{
prepared
=
true
}
end
ActionDispatch
::
Reloader
.
prepare!
assert
prepared
end
private
def
dispatch
(
cache_classes
=
true
,
&
block
)
@dispatcher
||=
ActionDispatch
::
Callbacks
.
new
(
block
||
DummyApp
.
new
,
!
cache_classes
)
def
dispatch
(
&
block
)
@dispatcher
||=
ActionDispatch
::
Callbacks
.
new
(
block
||
DummyApp
.
new
)
@dispatcher
.
call
({
'rack.input'
=>
StringIO
.
new
(
''
)})
end
...
...
activerecord/lib/active_record/railtie.rb
浏览文件 @
435bccda
...
...
@@ -82,7 +82,7 @@ class Railtie < Rails::Railtie
ActiveSupport
.
on_load
(
:active_record
)
do
instantiate_observers
ActionDispatch
::
Callbacks
.
to_prepare
(
:activerecord_instantiate_observers
)
do
ActionDispatch
::
Reloader
.
to_prepare
(
:activerecord_instantiate_observers
)
do
ActiveRecord
::
Base
.
instantiate_observers
end
end
...
...
activesupport/lib/active_support/file_update_checker.rb
浏览文件 @
435bccda
...
...
@@ -8,7 +8,7 @@ module ActiveSupport
# I18n.reload!
# end
#
# ActionDispatch::
Callbacks
.to_prepare do
# ActionDispatch::
Reloader
.to_prepare do
# i18n_reloader.execute_if_updated
# end
#
...
...
activesupport/lib/active_support/i18n_railtie.rb
浏览文件 @
435bccda
...
...
@@ -19,7 +19,7 @@ def self.reloader
# on to_prepare callbacks. This will only happen on the config.after_initialize
# callback below.
initializer
"i18n.callbacks"
do
ActionDispatch
::
Callbacks
.
to_prepare
do
ActionDispatch
::
Reloader
.
to_prepare
do
I18n
::
Railtie
.
reloader
.
execute_if_updated
end
end
...
...
railties/lib/rails/application.rb
浏览文件 @
435bccda
...
...
@@ -157,7 +157,7 @@ def default_middleware_stack
middleware
.
use
::
ActionDispatch
::
RemoteIp
,
config
.
action_dispatch
.
ip_spoofing_check
,
config
.
action_dispatch
.
trusted_proxies
middleware
.
use
::
Rack
::
Sendfile
,
config
.
action_dispatch
.
x_sendfile_header
middleware
.
use
::
ActionDispatch
::
Reloader
unless
config
.
cache_classes
middleware
.
use
::
ActionDispatch
::
Callbacks
,
!
config
.
cache_classes
middleware
.
use
::
ActionDispatch
::
Callbacks
middleware
.
use
::
ActionDispatch
::
Cookies
if
config
.
session_store
...
...
railties/lib/rails/application/finisher.rb
浏览文件 @
435bccda
...
...
@@ -21,7 +21,7 @@ module Finisher
initializer
:add_to_prepare_blocks
do
config
.
to_prepare_blocks
.
each
do
|
block
|
ActionDispatch
::
Callbacks
.
to_prepare
(
&
block
)
ActionDispatch
::
Reloader
.
to_prepare
(
&
block
)
end
end
...
...
@@ -37,6 +37,10 @@ module Finisher
build_middleware_stack
end
initializer
:run_prepare_callbacks
do
ActionDispatch
::
Reloader
.
prepare!
end
initializer
:eager_load!
do
if
config
.
cache_classes
&&
!
$rails_rake_task
ActiveSupport
.
run_load_hooks
(
:before_eager_load
,
self
)
...
...
@@ -52,7 +56,7 @@ module Finisher
initializer
:set_routes_reloader
do
|
app
|
reloader
=
lambda
{
app
.
routes_reloader
.
execute_if_updated
}
reloader
.
call
ActionDispatch
::
Callbacks
.
to_prepare
(
&
reloader
)
ActionDispatch
::
Reloader
.
to_prepare
(
&
reloader
)
end
# Disable dependency loading during request cycle
...
...
railties/lib/rails/console/app.rb
浏览文件 @
435bccda
...
...
@@ -26,7 +26,6 @@ def new_session
# reloads the environment
def
reload!
(
print
=
true
)
puts
"Reloading..."
if
print
# This triggers the to_prepare callbacks
ActionDispatch
::
Callbacks
.
new
(
Proc
.
new
{},
false
).
call
({})
ActionDispatch
::
Reloader
.
reload!
true
end
railties/test/application/console_test.rb
浏览文件 @
435bccda
...
...
@@ -26,14 +26,14 @@ def test_new_session_should_return_integration_session
assert_instance_of
ActionDispatch
::
Integration
::
Session
,
session
end
def
test_reload_should_fire_preparation_callbacks
def
test_reload_should_fire_preparation_
and_cleanup_
callbacks
load_environment
a
=
b
=
c
=
nil
# TODO: These should be defined on the initializer
ActionDispatch
::
Callbacks
.
to_prepare
{
a
=
b
=
c
=
1
}
ActionDispatch
::
Callbacks
.
to_prepare
{
b
=
c
=
2
}
ActionDispatch
::
Callbacks
.
to_prepare
{
c
=
3
}
ActionDispatch
::
Reloader
.
to_prepare
{
a
=
b
=
c
=
1
}
ActionDispatch
::
Reloader
.
to_prepare
{
b
=
c
=
2
}
ActionDispatch
::
Reloader
.
to_cleanup
{
c
=
3
}
# Hide Reloading... output
silence_stream
(
STDOUT
)
{
reload!
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录