Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
24e57122
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,体验更适合开发者的 AI 搜索 >>
提交
24e57122
编写于
10月 29, 2013
作者:
C
Charlie Somerville
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #26 from github/kill-whiny-nils
Kill whiny nils
上级
c717a84b
8f6bafc3
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
1 addition
and
137 deletion
+1
-137
activesupport/lib/active_support/whiny_nil.rb
activesupport/lib/active_support/whiny_nil.rb
+0
-64
activesupport/test/whiny_nil_test.rb
activesupport/test/whiny_nil_test.rb
+0
-50
railties/environments/development.rb
railties/environments/development.rb
+0
-3
railties/environments/test.rb
railties/environments/test.rb
+1
-4
railties/lib/initializer.rb
railties/lib/initializer.rb
+0
-16
未找到文件。
activesupport/lib/active_support/whiny_nil.rb
已删除
100644 → 0
浏览文件 @
c717a84b
# Extensions to +nil+ which allow for more helpful error messages for people who
# are new to Rails.
#
# Ruby raises NoMethodError if you invoke a method on an object that does not
# respond to it:
#
# $ ruby -e nil.destroy
# -e:1: undefined method `destroy' for nil:NilClass (NoMethodError)
#
# With these extensions, if the method belongs to the public interface of the
# classes in NilClass::WHINERS the error message suggests which could be the
# actual intended class:
#
# $ script/runner nil.destroy
# ...
# You might have expected an instance of ActiveRecord::Base.
# ...
#
# NilClass#id exists in Ruby 1.8 (though it is deprecated). Since +id+ is a fundamental
# method of Active Record models NilClass#id is redefined as well to raise a RuntimeError
# and warn the user. She probably wanted a model database identifier and the 4
# returned by the original method could result in obscure bugs.
#
# The flag <tt>config.whiny_nils</tt> determines whether this feature is enabled.
# By default it is on in development and test modes, and it is off in production
# mode.
class
NilClass
WHINERS
=
[
::
Array
]
WHINERS
<<
::
ActiveRecord
::
Base
if
defined?
::
ActiveRecord
METHOD_CLASS_MAP
=
Hash
.
new
WHINERS
.
each
do
|
klass
|
methods
=
klass
.
public_instance_methods
-
public_instance_methods
class_name
=
klass
.
name
methods
.
each
{
|
method
|
METHOD_CLASS_MAP
[
method
.
to_sym
]
=
class_name
}
end
# Raises a RuntimeError when you attempt to call +id+ on +nil+.
def
id
raise
RuntimeError
,
"Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id"
,
caller
end
private
def
method_missing
(
method
,
*
args
,
&
block
)
# Ruby 1.9.2: disallow explicit coercion via method_missing.
if
method
==
:to_ary
||
method
==
:to_str
super
elsif
klass
=
METHOD_CLASS_MAP
[
method
]
raise_nil_warning_for
klass
,
method
,
caller
else
super
end
end
# Raises a NoMethodError when you attempt to call a method on +nil+.
def
raise_nil_warning_for
(
class_name
=
nil
,
selector
=
nil
,
with_caller
=
nil
)
message
=
"You have a nil object when you didn't expect it!"
message
<<
"
\n
You might have expected an instance of
#{
class_name
}
."
if
class_name
message
<<
"
\n
The error occurred while evaluating nil.
#{
selector
}
"
if
selector
raise
NoMethodError
,
message
,
with_caller
||
caller
end
end
activesupport/test/whiny_nil_test.rb
已删除
100644 → 0
浏览文件 @
c717a84b
# Stub to enable testing without Active Record
module
ActiveRecord
class
Base
def
save!
end
end
end
require
'abstract_unit'
require
'active_support/whiny_nil'
class
WhinyNilTest
<
Test
::
Unit
::
TestCase
def
test_unchanged
nil
.
method_thats_not_in_whiners
rescue
NoMethodError
=>
nme
assert
(
nme
.
message
=~
/nil:NilClass/
)
end
def
test_active_record
nil
.
save!
rescue
NoMethodError
=>
nme
assert
(
!
(
nme
.
message
=~
/nil:NilClass/
))
assert_match
(
/nil\.save!/
,
nme
.
message
)
end
def
test_array
nil
.
each
rescue
NoMethodError
=>
nme
assert
(
!
(
nme
.
message
=~
/nil:NilClass/
))
assert_match
(
/nil\.each/
,
nme
.
message
)
end
def
test_id
nil
.
id
rescue
RuntimeError
=>
nme
assert
(
!
(
nme
.
message
=~
/nil:NilClass/
))
end
def
test_no_to_ary_coercion
nil
.
to_ary
rescue
NoMethodError
=>
nme
assert
(
nme
.
message
=~
/nil:NilClass/
)
end
def
test_no_to_str_coercion
nil
.
to_str
rescue
NoMethodError
=>
nme
assert
(
nme
.
message
=~
/nil:NilClass/
)
end
end
railties/environments/development.rb
浏览文件 @
24e57122
...
...
@@ -5,9 +5,6 @@
# since you don't have to restart the webserver when you make code changes.
config
.
cache_classes
=
false
# Log error messages when you accidentally call methods on nil.
config
.
whiny_nils
=
true
# Show full error reports and disable caching
config
.
action_controller
.
consider_all_requests_local
=
true
config
.
action_controller
.
perform_caching
=
false
...
...
railties/environments/test.rb
浏览文件 @
24e57122
...
...
@@ -6,9 +6,6 @@
# and recreated between test runs. Don't rely on the data there!
config
.
cache_classes
=
true
# Log error messages when you accidentally call methods on nil.
config
.
whiny_nils
=
true
# Show full error reports and disable caching
config
.
action_controller
.
consider_all_requests_local
=
true
config
.
action_controller
.
perform_caching
=
false
...
...
@@ -25,4 +22,4 @@
# Use SQL instead of Active Record's schema dumper when creating the test database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
# config.active_record.schema_format = :sql
\ No newline at end of file
# config.active_record.schema_format = :sql
railties/lib/initializer.rb
浏览文件 @
24e57122
...
...
@@ -147,7 +147,6 @@ def process
initialize_framework_logging
initialize_dependency_mechanism
initialize_whiny_nils
initialize_time_zone
initialize_i18n
...
...
@@ -543,12 +542,6 @@ def initialize_dependency_mechanism
ActiveSupport
::
Dependencies
.
mechanism
=
configuration
.
cache_classes
?
:require
:
:load
end
# Loads support for "whiny nil" (noisy warnings when methods are invoked
# on +nil+ values) if Configuration#whiny_nils is true.
def
initialize_whiny_nils
require
(
'active_support/whiny_nil'
)
if
configuration
.
whiny_nils
end
# Sets the default value for Time.zone, and turns on ActiveRecord::Base#time_zone_aware_attributes.
# If assigned value cannot be matched to a TimeZone, an exception will be raised.
def
initialize_time_zone
...
...
@@ -752,10 +745,6 @@ def load_once_paths=(paths)
# The root of the application's views. (Defaults to <tt>app/views</tt>.)
attr_accessor
:view_path
# Set to +true+ if you want to be warned (noisily) when you try to invoke
# any method of +nil+. Set to +false+ for the standard Ruby behavior.
attr_accessor
:whiny_nils
# The list of plugins to load. If this is set to <tt>nil</tt>, all plugins will
# be loaded. If this is set to <tt>[]</tt>, no plugins will be loaded. Otherwise,
# plugins will be loaded in the order specified.
...
...
@@ -870,7 +859,6 @@ def initialize
self
.
preload_frameworks
=
default_preload_frameworks
self
.
cache_classes
=
default_cache_classes
self
.
dependency_loading
=
default_dependency_loading
self
.
whiny_nils
=
default_whiny_nils
self
.
plugins
=
default_plugins
self
.
plugin_paths
=
default_plugin_paths
self
.
plugin_locators
=
default_plugin_locators
...
...
@@ -1067,10 +1055,6 @@ def default_cache_classes
true
end
def
default_whiny_nils
false
end
def
default_plugins
nil
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录