Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
6d6ae084
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,发现更多精彩内容 >>
提交
6d6ae084
编写于
10月 07, 2009
作者:
C
Carl Lerche
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Start moving the initializers into the application object
上级
d0965892
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
225 addition
and
73 deletion
+225
-73
railties/lib/rails/application.rb
railties/lib/rails/application.rb
+31
-7
railties/lib/rails/generators/rails/app/templates/config/boot.rb
...s/lib/rails/generators/rails/app/templates/config/boot.rb
+6
-7
railties/lib/rails/initializable.rb
railties/lib/rails/initializable.rb
+70
-0
railties/lib/rails/initializer.rb
railties/lib/rails/initializer.rb
+4
-38
railties/test/application/initializable_test.rb
railties/test/application/initializable_test.rb
+26
-0
railties/test/application/initializer_test.rb
railties/test/application/initializer_test.rb
+33
-0
railties/test/application/load_test.rb
railties/test/application/load_test.rb
+2
-2
railties/test/initializer/initialize_i18n_test.rb
railties/test/initializer/initialize_i18n_test.rb
+51
-0
railties/test/initializer_test.rb
railties/test/initializer_test.rb
+0
-18
railties/test/isolation/abstract_unit.rb
railties/test/isolation/abstract_unit.rb
+2
-1
未找到文件。
railties/lib/rails/application.rb
浏览文件 @
6d6ae084
module
Rails
class
Application
extend
Initializable
def
self
.
inherited
(
child
)
child
.
initializers
=
initializers
.
dup
end
def
self
.
config
@config
||=
Configuration
.
new
...
...
@@ -9,21 +14,40 @@ def self.config=(config)
@config
=
config
end
def
config
self
.
class
.
config
end
def
routes
def
self
.
routes
ActionController
::
Routing
::
Routes
end
def
middleware
def
self
.
middleware
config
.
middleware
end
def
call
(
env
)
def
self
.
call
(
env
)
@app
||=
middleware
.
build
(
routes
)
@app
.
call
(
env
)
end
def
self
.
new
initializers
.
run
self
end
initializer
:initialize_rails
do
Rails
.
initializers
.
run
end
# Set the <tt>$LOAD_PATH</tt> based on the value of
# Configuration#load_paths. Duplicates are removed.
initializer
:set_load_path
do
config
.
paths
.
add_to_load_path
$LOAD_PATH
.
uniq!
end
# Bail if boot.rb is outdated
initializer
:freak_out_if_boot_rb_is_outdated
do
unless
defined?
(
Rails
::
BOOTSTRAP_VERSION
)
abort
%{Your config/boot.rb is outdated: Run "rake rails:update".}
end
end
end
end
railties/lib/rails/generators/rails/app/templates/config/boot.rb
浏览文件 @
6d6ae084
...
...
@@ -40,20 +40,20 @@ def preinitializer_path
class
Boot
def
run
load_initializer
set_load_paths
load_initializer
end
def
set_load_paths
%w(
railties
railties/lib
activesupport/lib
actionmailer/lib
actionpack/lib
activemodel/lib
activerecord/lib
actionmailer/lib
activeresource/lib
actionwebservice/lib
activesupport/lib
railties/lib
railties
)
.
reverse_each
do
|
path
|
path
=
"
#{
framework_root_path
}
/
#{
path
}
"
$LOAD_PATH
.
unshift
(
path
)
if
File
.
directory?
(
path
)
...
...
@@ -68,7 +68,6 @@ def framework_root_path
class
VendorBoot
<
Boot
def
load_initializer
$:
.
unshift
(
"
#{
framework_root_path
}
/railties/lib"
)
require
"rails"
install_gem_spec_stubs
Rails
::
GemDependency
.
add_frozen_gem_path
...
...
railties/lib/rails/initializable.rb
0 → 100644
浏览文件 @
6d6ae084
module
Rails
module
Initializable
# A collection of initializers
class
Collection
<
ActiveSupport
::
OrderedHash
# def initialize_copy(other)
# super
# each do |key, value|
# self[key] = value.dup
# end
# end
def
run
each
do
|
key
,
initializer
|
initializer
.
run
end
self
end
end
class
Initializer
attr_reader
:name
,
:options
,
:block
def
initialize
(
name
,
options
=
{},
&
block
)
@name
,
@options
,
@block
=
name
,
options
,
block
end
def
run
return
if
@already_ran
@block
.
call
@already_ran
=
true
end
end
def
initializer
(
name
,
options
=
{},
&
block
)
initializers
[
name
]
=
Initializer
.
new
(
name
,
options
,
&
block
)
end
def
initializers
@initializers
||=
Collection
.
new
end
def
initializers
=
(
initializers
)
@initializers
=
initializers
end
end
extend
Initializable
# Check for valid Ruby version (1.8.2 or 1.8.4 or higher). This is done in an
# external file, so we can use it from the `rails` program as well without duplication.
initializer
:check_ruby_version
do
require
'rails/ruby_version_check'
end
# For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the
# multibyte safe operations. Plugin authors supporting other encodings
# should override this behaviour and set the relevant +default_charset+
# on ActionController::Base.
#
# For Ruby 1.9, UTF-8 is the default internal and external encoding.
initializer
:initialize_encoding
do
if
RUBY_VERSION
<
'1.9'
$KCODE
=
'u'
else
Encoding
.
default_external
=
Encoding
::
UTF_8
end
end
end
\ No newline at end of file
railties/lib/rails/initializer.rb
浏览文件 @
6d6ae084
require
"pathname"
require
'active_support/ordered_hash'
require
'rails/initializable'
require
'rails/application'
require
'rails/railties_path'
require
'rails/version'
...
...
@@ -12,10 +14,6 @@
RAILS_ENV
=
(
ENV
[
'RAILS_ENV'
]
||
'development'
).
dup
unless
defined?
(
RAILS_ENV
)
module
Rails
# Sanity check to make sure this file is only loaded once
# TODO: Get to the point where this can be removed.
raise
"It looks like initializer.rb was required twice"
if
defined?
(
Initializer
)
class
Initializer
class
Error
<
StandardError
;
end
...
...
@@ -110,6 +108,8 @@ def self.run(initializer = nil, config = nil)
default
.
run
(
initializer
)
else
Rails
.
application
=
Class
.
new
(
Application
)
# Trigger the initializer
Rails
.
application
.
new
yield
Rails
.
application
.
config
if
block_given?
default
.
config
=
Rails
.
application
.
config
default
.
run
...
...
@@ -117,26 +117,6 @@ def self.run(initializer = nil, config = nil)
end
end
# Check for valid Ruby version (1.8.2 or 1.8.4 or higher). This is done in an
# external file, so we can use it from the `rails` program as well without duplication.
Initializer
.
default
.
add
:check_ruby_version
do
require
'rails/ruby_version_check'
end
# Bail if boot.rb is outdated
Initializer
.
default
.
add
:freak_out_if_boot_rb_is_outdated
do
unless
defined?
(
Rails
::
BOOTSTRAP_VERSION
)
abort
%{Your config/boot.rb is outdated: Run "rake rails:update".}
end
end
# Set the <tt>$LOAD_PATH</tt> based on the value of
# Configuration#load_paths. Duplicates are removed.
Initializer
.
default
.
add
:set_load_path
do
configuration
.
paths
.
add_to_load_path
$LOAD_PATH
.
uniq!
end
# Requires all frameworks specified by the Configuration#frameworks
# list. By default, all frameworks (Active Record, Active Support,
# Action Pack, Action Mailer, and Active Resource) are loaded.
...
...
@@ -230,20 +210,6 @@ def self.run(initializer = nil, config = nil)
end
end
# For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the
# multibyte safe operations. Plugin authors supporting other encodings
# should override this behaviour and set the relevant +default_charset+
# on ActionController::Base.
#
# For Ruby 1.9, UTF-8 is the default internal and external encoding.
Initializer
.
default
.
add
:initialize_encoding
do
if
RUBY_VERSION
<
'1.9'
$KCODE
=
'u'
else
Encoding
.
default_external
=
Encoding
::
UTF_8
end
end
# This initialization routine does nothing unless <tt>:active_record</tt>
# is one of the frameworks to load (Configuration#frameworks). If it is,
# this sets the database configuration from Configuration#database_configuration
...
...
railties/test/application/initializable_test.rb
0 → 100644
浏览文件 @
6d6ae084
require
"isolation/abstract_unit"
module
ApplicationTests
class
InitializerTest
<
Test
::
Unit
::
TestCase
include
ActiveSupport
::
Testing
::
Isolation
def
setup
build_app
boot_rails
end
test
"initializers only ever run once"
do
class
MyApp
<
Rails
::
Application
initializer
:counter
do
$counter
+=
1
end
end
$counter
=
0
MyApp
.
initializers
[
:counter
].
run
MyApp
.
initializers
[
:counter
].
run
assert_equal
1
,
$counter
end
end
end
\ No newline at end of file
railties/test/application/initializer_test.rb
0 → 100644
浏览文件 @
6d6ae084
require
"isolation/abstract_unit"
module
ApplicationTests
class
InitializerTest
<
Test
::
Unit
::
TestCase
include
ActiveSupport
::
Testing
::
Isolation
def
setup
build_app
boot_rails
end
test
"initializing an application initializes rails"
do
class
MyApp
<
Rails
::
Application
;
end
if
RUBY_VERSION
<
'1.9'
$KCODE
=
''
MyApp
.
new
assert_equal
'UTF8'
,
$KCODE
else
Encoding
.
default_external
=
Encoding
::
US_ASCII
MyApp
.
new
assert_equal
Encoding
::
UTF_8
,
Encoding
.
default_external
end
end
test
"initializing an application adds the application paths to the load path"
do
class
MyApp
<
Rails
::
Application
;
end
MyApp
.
new
assert
$:
.
include?
(
"
#{
app_path
}
/app/models"
)
end
end
end
\ No newline at end of file
railties/test/application/load_test.rb
浏览文件 @
6d6ae084
...
...
@@ -40,14 +40,14 @@ def setup
test
"Rails.application is available after config.ru has been racked up"
do
rackup
assert
Rails
.
application
.
new
.
is_a?
(
Rails
::
Application
)
assert
Rails
.
application
.
new
<
Rails
::
Application
end
# Passenger still uses AC::Dispatcher, so we need to
# keep it working for now
test
"deprecated ActionController::Dispatcher still works"
do
rackup
assert
ActionController
::
Dispatcher
.
new
.
is_a?
(
Rails
::
Application
)
assert
ActionController
::
Dispatcher
.
new
<
Rails
::
Application
end
test
"the config object is available on the application object"
do
...
...
railties/test/initializer/initialize_i18n_test.rb
0 → 100644
浏览文件 @
6d6ae084
require
"isolation/abstract_unit"
module
InitializerTests
class
InitializeI18nTest
<
Test
::
Unit
::
TestCase
include
ActiveSupport
::
Testing
::
Isolation
def
setup
build_app
boot_rails
end
# test_config_defaults_and_settings_should_be_added_to_i18n_defaults
test
"i18n config defaults and settings should be added to i18n defaults"
do
Rails
::
Initializer
.
run
do
|
c
|
c
.
i18n
.
load_path
<<
"my/other/locale.yml"
end
#{RAILS_FRAMEWORK_ROOT}/railties/test/fixtures/plugins/engines/engine/config/locales/en.yml
assert_equal
%W(
#{
RAILS_FRAMEWORK_ROOT
}
/activesupport/lib/active_support/locale/en.yml
#{
RAILS_FRAMEWORK_ROOT
}
/activemodel/lib/active_model/locale/en.yml
#{
RAILS_FRAMEWORK_ROOT
}
/activerecord/lib/active_record/locale/en.yml
#{
RAILS_FRAMEWORK_ROOT
}
/actionpack/lib/action_view/locale/en.yml
#{
RAILS_FRAMEWORK_ROOT
}
/railties/tmp/app/config/locales/en.yml
my/other/locale.yml
)
,
I18n
.
load_path
end
test
"i18n finds locale files in engines"
do
app_file
"vendor/plugins/engine/init.rb"
,
""
app_file
"vendor/plugins/engine/app/models/hellos.rb"
,
"class Hello ; end"
app_file
"vendor/plugins/engine/lib/omg.rb"
,
"puts 'omg'"
app_file
"vendor/plugins/engine/config/locales/en.yml"
,
"hello:"
Rails
::
Initializer
.
run
do
|
c
|
c
.
i18n
.
load_path
<<
"my/other/locale.yml"
end
#{RAILS_FRAMEWORK_ROOT}/railties/test/fixtures/plugins/engines/engine/config/locales/en.yml
assert_equal
%W(
#{
RAILS_FRAMEWORK_ROOT
}
/activesupport/lib/active_support/locale/en.yml
#{
RAILS_FRAMEWORK_ROOT
}
/activemodel/lib/active_model/locale/en.yml
#{
RAILS_FRAMEWORK_ROOT
}
/activerecord/lib/active_record/locale/en.yml
#{
RAILS_FRAMEWORK_ROOT
}
/actionpack/lib/action_view/locale/en.yml
#{
app_path
}
/config/locales/en.yml
my/other/locale.yml
#{
app_path
}
/vendor/plugins/engine/config/locales/en.yml
)
,
I18n
.
load_path
end
end
end
\ No newline at end of file
railties/test/initializer_test.rb
浏览文件 @
6d6ae084
...
...
@@ -393,24 +393,6 @@ def test_config_defaults_should_be_added_with_config_settings
assert_equal
[
"my/test/locale.yml"
,
"my/other/locale.yml"
],
config
.
i18n
.
load_path
end
def
test_config_defaults_and_settings_should_be_added_to_i18n_defaults
File
.
stubs
(
:exist?
).
returns
(
true
)
Dir
.
stubs
(
:[]
).
returns
([
"my/test/locale.yml"
])
config
=
Rails
::
Configuration
.
new
config
.
i18n
.
load_path
<<
"my/other/locale.yml"
Rails
::
Initializer
.
run
(
:initialize_i18n
,
config
)
assert_equal
[
File
.
expand_path
(
File
.
dirname
(
__FILE__
)
+
"/../../activesupport/lib/active_support/locale/en.yml"
),
File
.
expand_path
(
File
.
dirname
(
__FILE__
)
+
"/../../actionpack/lib/action_view/locale/en.yml"
),
File
.
expand_path
(
File
.
dirname
(
__FILE__
)
+
"/../../activemodel/lib/active_model/locale/en.yml"
),
File
.
expand_path
(
File
.
dirname
(
__FILE__
)
+
"/../../activerecord/lib/active_record/locale/en.yml"
),
File
.
expand_path
(
File
.
dirname
(
__FILE__
)
+
"/../../railties/test/fixtures/plugins/engines/engine/config/locales/en.yml"
),
"my/test/locale.yml"
,
"my/other/locale.yml"
],
I18n
.
load_path
.
collect
{
|
path
|
path
=~
/\.\./
?
File
.
expand_path
(
path
)
:
path
}
end
def
test_setting_another_default_locale
config
=
Rails
::
Configuration
.
new
config
.
i18n
.
default_locale
=
:de
...
...
railties/test/isolation/abstract_unit.rb
浏览文件 @
6d6ae084
...
...
@@ -91,7 +91,8 @@ def build_app(options = {})
end
def
app_file
(
path
,
contents
)
File
.
open
(
app_path
(
path
),
'w'
)
do
|
f
|
FileUtils
.
mkdir_p
File
.
dirname
(
"
#{
app_path
}
/
#{
path
}
"
)
File
.
open
(
"
#{
app_path
}
/
#{
path
}
"
,
'w'
)
do
|
f
|
f
.
puts
contents
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录