Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
d0d7ce00
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,发现更多精彩内容 >>
提交
d0d7ce00
编写于
9月 01, 2017
作者:
M
Matthew Draper
提交者:
GitHub
9月 01, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #29233 from matthewd/redefine-method
Clarify intentions around method redefinitions
上级
2cd8ac1b
50dbf817
变更
18
隐藏空白更改
内联
并排
Showing
18 changed file
with
98 addition
and
67 deletion
+98
-67
actioncable/lib/action_cable/remote_connections.rb
actioncable/lib/action_cable/remote_connections.rb
+2
-0
actionpack/lib/action_controller/metal/renderers.rb
actionpack/lib/action_controller/metal/renderers.rb
+1
-1
actionpack/lib/action_controller/test_case.rb
actionpack/lib/action_controller/test_case.rb
+2
-1
actionpack/lib/action_dispatch/routing/route_set.rb
actionpack/lib/action_dispatch/routing/route_set.rb
+2
-1
actionview/lib/action_view/layouts.rb
actionview/lib/action_view/layouts.rb
+2
-2
actionview/lib/action_view/test_case.rb
actionview/lib/action_view/test_case.rb
+2
-2
activemodel/lib/active_model/naming.rb
activemodel/lib/active_model/naming.rb
+2
-2
activerecord/lib/active_record/nested_attributes.rb
activerecord/lib/active_record/nested_attributes.rb
+2
-3
activesupport/lib/active_support/core_ext/class/attribute.rb
activesupport/lib/active_support/core_ext/class/attribute.rb
+11
-14
activesupport/lib/active_support/core_ext/date/conversions.rb
...vesupport/lib/active_support/core_ext/date/conversions.rb
+5
-9
activesupport/lib/active_support/core_ext/date_time/compatibility.rb
...rt/lib/active_support/core_ext/date_time/compatibility.rb
+2
-2
activesupport/lib/active_support/core_ext/module.rb
activesupport/lib/active_support/core_ext/module.rb
+1
-0
activesupport/lib/active_support/core_ext/module/redefine_method.rb
...ort/lib/active_support/core_ext/module/redefine_method.rb
+49
-0
activesupport/lib/active_support/core_ext/module/remove_method.rb
...pport/lib/active_support/core_ext/module/remove_method.rb
+3
-23
activesupport/lib/active_support/core_ext/string/output_safety.rb
...pport/lib/active_support/core_ext/string/output_safety.rb
+3
-3
activesupport/lib/active_support/core_ext/time/compatibility.rb
...support/lib/active_support/core_ext/time/compatibility.rb
+2
-2
activesupport/lib/active_support/core_ext/uri.rb
activesupport/lib/active_support/core_ext/uri.rb
+2
-1
guides/source/active_support_core_extensions.md
guides/source/active_support_core_extensions.md
+5
-1
未找到文件。
actioncable/lib/action_cable/remote_connections.rb
浏览文件 @
d0d7ce00
# frozen_string_literal: true
require
"active_support/core_ext/module/redefine_method"
module
ActionCable
# If you need to disconnect a given connection, you can go through the
# RemoteConnections. You can find the connections you're looking for by
...
...
actionpack/lib/action_controller/metal/renderers.rb
浏览文件 @
d0d7ce00
...
...
@@ -85,7 +85,7 @@ def self.add(key, &block)
def
self
.
remove
(
key
)
RENDERERS
.
delete
(
key
.
to_sym
)
method_name
=
_render_with_renderer_method_name
(
key
)
remove_
method
(
method_name
)
if
method_defined?
(
method_name
)
remove_
possible_method
(
method_name
)
end
def
self
.
_render_with_renderer_method_name
(
key
)
...
...
actionpack/lib/action_controller/test_case.rb
浏览文件 @
d0d7ce00
...
...
@@ -4,6 +4,7 @@
require
"active_support/core_ext/hash/conversions"
require
"active_support/core_ext/object/to_query"
require
"active_support/core_ext/module/anonymous"
require
"active_support/core_ext/module/redefine_method"
require
"active_support/core_ext/hash/keys"
require
"active_support/testing/constant_lookup"
require_relative
"template_assertions"
...
...
@@ -19,7 +20,7 @@ module Live
# the database on the main thread, so they could open a txn, then the
# controller thread will open a new connection and try to access data
# that's only visible to the main thread's txn. This is the problem in #23483.
remove
_method
:new_controller_thread
silence_redefinition_of
_method
:new_controller_thread
def
new_controller_thread
# :nodoc:
yield
end
...
...
actionpack/lib/action_dispatch/routing/route_set.rb
浏览文件 @
d0d7ce00
...
...
@@ -3,6 +3,7 @@
require_relative
"../journey"
require
"active_support/core_ext/object/to_query"
require
"active_support/core_ext/hash/slice"
require
"active_support/core_ext/module/redefine_method"
require
"active_support/core_ext/module/remove_method"
require
"active_support/core_ext/array/extract_options"
require
"action_controller/metal/exceptions"
...
...
@@ -546,7 +547,7 @@ def url_options; {}; end
# plus a singleton class method called _routes ...
included
do
singleton_class
.
send
(
:redefine_method
,
:_routes
)
{
routes
}
redefine_singleton_method
(
:_routes
)
{
routes
}
end
# And an instance method _routes. Note that
...
...
actionview/lib/action_view/layouts.rb
浏览文件 @
d0d7ce00
# frozen_string_literal: true
require_relative
"rendering"
require
"active_support/core_ext/module/re
mov
e_method"
require
"active_support/core_ext/module/re
defin
e_method"
module
ActionView
# Layouts reverse the common pattern of including shared headers and footers in many templates to isolate changes in
...
...
@@ -279,7 +279,7 @@ def layout(layout, conditions = {})
# If a layout is not explicitly mentioned then look for a layout with the controller's name.
# if nothing is found then try same procedure to find super class's layout.
def
_write_layout_method
# :nodoc:
remove_possible
_method
(
:_layout
)
silence_redefinition_of
_method
(
:_layout
)
prefixes
=
/\blayouts/
.
match?
(
_implied_layout_name
)
?
[]
:
[
"layouts"
]
default_behavior
=
"lookup_context.find_all('
#{
_implied_layout_name
}
',
#{
prefixes
.
inspect
}
, false, [], { formats: formats }).first || super"
...
...
actionview/lib/action_view/test_case.rb
浏览文件 @
d0d7ce00
# frozen_string_literal: true
require
"active_support/core_ext/module/re
mov
e_method"
require
"active_support/core_ext/module/re
defin
e_method"
require
"action_controller"
require
"action_controller/test_case"
require
"action_view"
...
...
@@ -171,7 +171,7 @@ def document_root_element
def
say_no_to_protect_against_forgery!
_helpers
.
module_eval
do
remove_possible
_method
:protect_against_forgery?
silence_redefinition_of
_method
:protect_against_forgery?
def
protect_against_forgery?
false
end
...
...
activemodel/lib/active_model/naming.rb
浏览文件 @
d0d7ce00
...
...
@@ -2,7 +2,7 @@
require
"active_support/core_ext/hash/except"
require
"active_support/core_ext/module/introspection"
require
"active_support/core_ext/module/re
mov
e_method"
require
"active_support/core_ext/module/re
defin
e_method"
module
ActiveModel
class
Name
...
...
@@ -218,7 +218,7 @@ def _singularize(string)
# provided method below, or rolling your own is required.
module
Naming
def
self
.
extended
(
base
)
#:nodoc:
base
.
remove_possible
_method
:model_name
base
.
silence_redefinition_of
_method
:model_name
base
.
delegate
:model_name
,
to: :class
end
...
...
activerecord/lib/active_record/nested_attributes.rb
浏览文件 @
d0d7ce00
# frozen_string_literal: true
require
"active_support/core_ext/hash/except"
require
"active_support/core_ext/module/redefine_method"
require
"active_support/core_ext/object/try"
require
"active_support/core_ext/hash/indifferent_access"
...
...
@@ -355,9 +356,7 @@ def accepts_nested_attributes_for(*attr_names)
# associations are just regular associations.
def
generate_association_writer
(
association_name
,
type
)
generated_association_methods
.
module_eval
<<-
eoruby
,
__FILE__
,
__LINE__
+
1
if method_defined?(:
#{
association_name
}
_attributes=)
remove_method(:
#{
association_name
}
_attributes=)
end
silence_redefinition_of_method :
#{
association_name
}
_attributes=
def
#{
association_name
}
_attributes=(attributes)
assign_nested_attributes_for_
#{
type
}
_association(:
#{
association_name
}
, attributes)
end
...
...
activesupport/lib/active_support/core_ext/class/attribute.rb
浏览文件 @
d0d7ce00
# frozen_string_literal: true
require_relative
"../kernel/singleton_class"
require_relative
"../module/re
mov
e_method"
require_relative
"../module/re
defin
e_method"
require_relative
"../array/extract_options"
class
Class
...
...
@@ -92,25 +92,23 @@ def class_attribute(*attrs)
default_value
=
options
.
fetch
(
:default
,
nil
)
attrs
.
each
do
|
name
|
remove_possible_singleton
_method
(
name
)
singleton_class
.
silence_redefinition_of
_method
(
name
)
define_singleton_method
(
name
)
{
nil
}
remove_possible_singleton
_method
(
"
#{
name
}
?"
)
singleton_class
.
silence_redefinition_of
_method
(
"
#{
name
}
?"
)
define_singleton_method
(
"
#{
name
}
?"
)
{
!!
public_send
(
name
)
}
if
instance_predicate
ivar
=
"@
#{
name
}
"
remove_possible_singleton
_method
(
"
#{
name
}
="
)
singleton_class
.
silence_redefinition_of
_method
(
"
#{
name
}
="
)
define_singleton_method
(
"
#{
name
}
="
)
do
|
val
|
singleton_class
.
class_eval
do
remove_possible_method
(
name
)
define_method
(
name
)
{
val
}
redefine_method
(
name
)
{
val
}
end
if
singleton_class?
class_eval
do
remove_possible_method
(
name
)
define_method
(
name
)
do
redefine_method
(
name
)
do
if
instance_variable_defined?
ivar
instance_variable_get
ivar
else
...
...
@@ -123,8 +121,7 @@ def class_attribute(*attrs)
end
if
instance_reader
remove_possible_method
name
define_method
(
name
)
do
redefine_method
(
name
)
do
if
instance_variable_defined?
(
ivar
)
instance_variable_get
ivar
else
...
...
@@ -132,13 +129,13 @@ def class_attribute(*attrs)
end
end
remove_possible_method
"
#{
name
}
?"
define_method
(
"
#{
name
}
?"
)
{
!!
public_send
(
name
)
}
if
instance_predicate
redefine_method
(
"
#{
name
}
?"
)
{
!!
public_send
(
name
)
}
if
instance_predicate
end
if
instance_writer
remove_possible_method
"
#{
name
}
="
attr_writer
name
redefine_method
(
"
#{
name
}
="
)
do
|
val
|
instance_variable_set
ivar
,
val
end
end
unless
default_value
.
nil?
...
...
activesupport/lib/active_support/core_ext/date/conversions.rb
浏览文件 @
d0d7ce00
...
...
@@ -3,7 +3,7 @@
require
"date"
require_relative
"../../inflector/methods"
require_relative
"zones"
require_relative
"../module/re
mov
e_method"
require_relative
"../module/re
defin
e_method"
class
Date
DATE_FORMATS
=
{
...
...
@@ -19,14 +19,6 @@ class Date
iso8601:
lambda
{
|
date
|
date
.
iso8601
}
}
# Ruby 1.9 has Date#to_time which converts to localtime only.
remove_method
:to_time
# Ruby 1.9 has Date#xmlschema which converts to a string without the time
# component. This removal may generate an issue on FreeBSD, that's why we
# need to use remove_possible_method here
remove_possible_method
:xmlschema
# Convert to a formatted string. See DATE_FORMATS for predefined formats.
#
# This method is aliased to <tt>to_s</tt>.
...
...
@@ -72,6 +64,8 @@ def readable_inspect
alias_method
:default_inspect
,
:inspect
alias_method
:inspect
,
:readable_inspect
silence_redefinition_of_method
:to_time
# Converts a Date instance to a Time, where the time is set to the beginning of the day.
# The timezone can be either :local or :utc (default :local).
#
...
...
@@ -89,6 +83,8 @@ def to_time(form = :local)
::
Time
.
send
(
form
,
year
,
month
,
day
)
end
silence_redefinition_of_method
:xmlschema
# Returns a string which represents the time in used time zone as DateTime
# defined by XML Schema:
#
...
...
activesupport/lib/active_support/core_ext/date_time/compatibility.rb
浏览文件 @
d0d7ce00
# frozen_string_literal: true
require_relative
"../date_and_time/compatibility"
require_relative
"../module/re
mov
e_method"
require_relative
"../module/re
defin
e_method"
class
DateTime
include
DateAndTime
::
Compatibility
remove_possible
_method
:to_time
silence_redefinition_of
_method
:to_time
# Either return an instance of `Time` with the same UTC offset
# as +self+ or an instance of `Time` representing the same time
...
...
activesupport/lib/active_support/core_ext/module.rb
浏览文件 @
d0d7ce00
...
...
@@ -10,4 +10,5 @@
require_relative
"module/concerning"
require_relative
"module/delegation"
require_relative
"module/deprecation"
require_relative
"module/redefine_method"
require_relative
"module/remove_method"
activesupport/lib/active_support/core_ext/module/redefine_method.rb
0 → 100644
浏览文件 @
d0d7ce00
# frozen_string_literal: true
class
Module
if
RUBY_VERSION
>=
"2.3"
# Marks the named method as intended to be redefined, if it exists.
# Suppresses the Ruby method redefinition warning. Prefer
# #redefine_method where possible.
def
silence_redefinition_of_method
(
method
)
if
method_defined?
(
method
)
||
private_method_defined?
(
method
)
# This suppresses the "method redefined" warning; the self-alias
# looks odd, but means we don't need to generate a unique name
alias_method
method
,
method
end
end
else
def
silence_redefinition_of_method
(
method
)
if
method_defined?
(
method
)
||
private_method_defined?
(
method
)
alias_method
:__rails_redefine
,
method
remove_method
:__rails_redefine
end
end
end
# Replaces the existing method definition, if there is one, with the passed
# block as its body.
def
redefine_method
(
method
,
&
block
)
visibility
=
method_visibility
(
method
)
silence_redefinition_of_method
(
method
)
define_method
(
method
,
&
block
)
send
(
visibility
,
method
)
end
# Replaces the existing singleton method definition, if there is one, with
# the passed block as its body.
def
redefine_singleton_method
(
method
,
&
block
)
singleton_class
.
redefine_method
(
method
,
&
block
)
end
def
method_visibility
(
method
)
# :nodoc:
case
when
private_method_defined?
(
method
)
:private
when
protected_method_defined?
(
method
)
:protected
else
:public
end
end
end
activesupport/lib/active_support/core_ext/module/remove_method.rb
浏览文件 @
d0d7ce00
# frozen_string_literal: true
require_relative
"redefine_method"
class
Module
# Removes the named method, if it exists.
def
remove_possible_method
(
method
)
...
...
@@ -10,28 +12,6 @@ def remove_possible_method(method)
# Removes the named singleton method, if it exists.
def
remove_possible_singleton_method
(
method
)
singleton_class
.
instance_eval
do
remove_possible_method
(
method
)
end
end
# Replaces the existing method definition, if there is one, with the passed
# block as its body.
def
redefine_method
(
method
,
&
block
)
visibility
=
method_visibility
(
method
)
remove_possible_method
(
method
)
define_method
(
method
,
&
block
)
send
(
visibility
,
method
)
end
def
method_visibility
(
method
)
# :nodoc:
case
when
private_method_defined?
(
method
)
:private
when
protected_method_defined?
(
method
)
:protected
else
:public
end
singleton_class
.
remove_possible_method
(
method
)
end
end
activesupport/lib/active_support/core_ext/string/output_safety.rb
浏览文件 @
d0d7ce00
...
...
@@ -2,6 +2,7 @@
require
"erb"
require_relative
"../kernel/singleton_class"
require_relative
"../module/redefine_method"
require_relative
"../../multibyte/unicode"
class
ERB
...
...
@@ -23,13 +24,12 @@ def html_escape(s)
unwrapped_html_escape
(
s
).
html_safe
end
# Aliasing twice issues a warning "discarding old...". Remove first to avoid it.
remove_method
(
:h
)
silence_redefinition_of_method
:h
alias
h
html_escape
module_function
:h
singleton_class
.
s
end
(
:remove_method
,
:html_escape
)
singleton_class
.
s
ilence_redefinition_of_method
:html_escape
module_function
:html_escape
# HTML escapes strings but doesn't wrap them with an ActiveSupport::SafeBuffer.
...
...
activesupport/lib/active_support/core_ext/time/compatibility.rb
浏览文件 @
d0d7ce00
# frozen_string_literal: true
require_relative
"../date_and_time/compatibility"
require_relative
"../module/re
mov
e_method"
require_relative
"../module/re
defin
e_method"
class
Time
include
DateAndTime
::
Compatibility
remove_possible
_method
:to_time
silence_redefinition_of
_method
:to_time
# Either return +self+ or the time in the local system timezone depending
# on the setting of +ActiveSupport.to_time_preserves_timezone+.
...
...
activesupport/lib/active_support/core_ext/uri.rb
浏览文件 @
d0d7ce00
...
...
@@ -5,8 +5,9 @@
parser
=
URI
::
Parser
.
new
unless
str
==
parser
.
unescape
(
parser
.
escape
(
str
))
require
"active_support/core_ext/module/redefine_method"
URI
::
Parser
.
class_eval
do
remove
_method
:unescape
silence_redefinition_of
_method
:unescape
def
unescape
(
str
,
escaped
=
/%[a-fA-F\d]{2}/
)
# TODO: Are we actually sure that ASCII == UTF-8?
# YK: My initial experiments say yes, but let's be sure please
...
...
guides/source/active_support_core_extensions.md
浏览文件 @
d0d7ce00
...
...
@@ -864,7 +864,11 @@ There are cases where you need to define a method with `define_method`, but don'
The method
`redefine_method`
prevents such a potential warning, removing the existing method before if needed.
NOTE: Defined in
`active_support/core_ext/module/remove_method.rb`
.
You can also use
`silence_redefinition_of_method`
if you need to define
the replacement method yourself (because you're using
`delegate`
, for
example).
NOTE: Defined in
`active_support/core_ext/module/redefine_method.rb`
.
Extensions to `Class`
---------------------
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录