未验证 提交 92fece96 编写于 作者: R Rafael Mendonça França

Merge pull request #34051 from gmcgibbon/module_parent_method_rename

Prefix Module#parent, Module#parents, and Module#parent_name with module
......@@ -7,7 +7,7 @@ def inherited(klass)
super
return unless klass.respond_to?(:helpers_path=)
if namespace = klass.parents.detect { |m| m.respond_to?(:railtie_helpers_paths) }
if namespace = klass.module_parents.detect { |m| m.respond_to?(:railtie_helpers_paths) }
paths = namespace.railtie_helpers_paths
else
paths = ActionController::Helpers.helpers_path
......
......@@ -444,7 +444,7 @@ def include_helpers(klass, include_path_helpers)
end
def include_helpers_now(klass, include_path_helpers)
namespace = klass.parents.detect { |m| m.respond_to?(:railtie_include_helpers) }
namespace = klass.module_parents.detect { |m| m.respond_to?(:railtie_include_helpers) }
if namespace && namespace.railtie_namespace.routes != self
namespace.railtie_include_helpers(klass, include_path_helpers)
......
......@@ -252,7 +252,7 @@ def self.extended(base) #:nodoc:
# Person.model_name.plural # => "people"
def model_name
@_model_name ||= begin
namespace = parents.detect do |n|
namespace = module_parents.detect do |n|
n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming?
end
ActiveModel::Name.new(self, namespace)
......
......@@ -24,7 +24,7 @@ def self.define_extensions(model, name)
if block_given?
extension_module_name = "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
extension = Module.new(&Proc.new)
model.parent.const_set(extension_module_name, extension)
model.module_parent.const_set(extension_module_name, extension)
end
end
......
......@@ -218,11 +218,11 @@ def reset_table_name #:nodoc:
end
def full_table_name_prefix #:nodoc:
(parents.detect { |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix
(module_parents.detect { |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix
end
def full_table_name_suffix #:nodoc:
(parents.detect { |p| p.respond_to?(:table_name_suffix) } || self).table_name_suffix
(module_parents.detect { |p| p.respond_to?(:table_name_suffix) } || self).table_name_suffix
end
# The array of names of environments where destructive actions should be prohibited. By default,
......@@ -503,9 +503,9 @@ def undecorated_table_name(class_name = base_class.name)
def compute_table_name
if base_class?
# Nested classes are prefixed with singular parent table name.
if parent < Base && !parent.abstract_class?
contained = parent.table_name
contained = contained.singularize if parent.pluralize_table_names
if module_parent < Base && !module_parent.abstract_class?
contained = module_parent.table_name
contained = contained.singularize if module_parent.pluralize_table_names
contained += "_"
end
......
* Rename `Module#parent`, `Module#parents`, and `Module#parent_name` to
`module_parent`, `module_parents`, and `module_parent_name`.
*Gannon McGibbon*
* Deprecate the use of `LoggerSilence` in favor of `ActiveSupport::LoggerSilence`
*Edouard Chin*
......
......@@ -5,8 +5,8 @@
class Module
# Returns the name of the module containing this one.
#
# M::N.parent_name # => "M"
def parent_name
# M::N.module_parent_name # => "M"
def module_parent_name
if defined?(@parent_name)
@parent_name
else
......@@ -16,6 +16,14 @@ def parent_name
end
end
def parent_name
ActiveSupport::Deprecation.warn(<<-MSG.squish)
`Module#parent_name` has been renamed to `module_parent_name`.
`parent_name` is deprecated and will be removed in Rails 6.1.
MSG
module_parent_name
end
# Returns the module which contains this one according to its name.
#
# module M
......@@ -24,15 +32,23 @@ def parent_name
# end
# X = M::N
#
# M::N.parent # => M
# X.parent # => M
# M::N.module_parent # => M
# X.module_parent # => M
#
# The parent of top-level and anonymous modules is Object.
#
# M.parent # => Object
# Module.new.parent # => Object
# M.module_parent # => Object
# Module.new.module_parent # => Object
def module_parent
module_parent_name ? ActiveSupport::Inflector.constantize(module_parent_name) : Object
end
def parent
parent_name ? ActiveSupport::Inflector.constantize(parent_name) : Object
ActiveSupport::Deprecation.warn(<<-MSG.squish)
`Module#parent` has been renamed to `module_parent`.
`parent` is deprecated and will be removed in Rails 6.1.
MSG
module_parent
end
# Returns all the parents of this module according to its name, ordered from
......@@ -44,13 +60,13 @@ def parent
# end
# X = M::N
#
# M.parents # => [Object]
# M::N.parents # => [M, Object]
# X.parents # => [M, Object]
def parents
# M.module_parents # => [Object]
# M::N.module_parents # => [M, Object]
# X.module_parents # => [M, Object]
def module_parents
parents = []
if parent_name
parts = parent_name.split("::")
if module_parent_name
parts = module_parent_name.split("::")
until parts.empty?
parents << ActiveSupport::Inflector.constantize(parts * "::")
parts.pop
......@@ -59,4 +75,12 @@ def parents
parents << Object unless parents.include? Object
parents
end
def parents
ActiveSupport::Deprecation.warn(<<-MSG.squish)
`Module#parents` has been renamed to `module_parents`.
`parents` is deprecated and will be removed in Rails 6.1.
MSG
module_parents
end
end
......@@ -521,8 +521,8 @@ def load_missing_constant(from_mod, const_name)
end
elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
return mod
elsif (parent = from_mod.parent) && parent != from_mod &&
! from_mod.parents.any? { |p| p.const_defined?(const_name, false) }
elsif (parent = from_mod.module_parent) && parent != from_mod &&
! from_mod.module_parents.any? { |p| p.const_defined?(const_name, false) }
# If our parents do not have a constant named +const_name+ then we are free
# to attempt to load upwards. If they do have such a constant, then this
# const_missing must be due to from_mod::const_name, which should not
......
......@@ -15,25 +15,43 @@ module FrozenB; end
end
class IntrospectionTest < ActiveSupport::TestCase
def test_module_parent_name
assert_equal "ParentA", ParentA::B.module_parent_name
assert_equal "ParentA::B", ParentA::B::C.module_parent_name
assert_nil ParentA.module_parent_name
end
def test_module_parent_name_when_frozen
assert_equal "ParentA", ParentA::FrozenB.module_parent_name
assert_equal "ParentA::B", ParentA::B::FrozenC.module_parent_name
end
def test_parent_name
assert_equal "ParentA", ParentA::B.parent_name
assert_equal "ParentA::B", ParentA::B::C.parent_name
assert_nil ParentA.parent_name
assert_deprecated do
assert_equal "ParentA", ParentA::B.parent_name
end
end
def test_parent_name_when_frozen
assert_equal "ParentA", ParentA::FrozenB.parent_name
assert_equal "ParentA::B", ParentA::B::FrozenC.parent_name
def test_module_parent
assert_equal ParentA::B, ParentA::B::C.module_parent
assert_equal ParentA, ParentA::B.module_parent
assert_equal Object, ParentA.module_parent
end
def test_parent
assert_equal ParentA::B, ParentA::B::C.parent
assert_equal ParentA, ParentA::B.parent
assert_equal Object, ParentA.parent
assert_deprecated do
assert_equal ParentA, ParentA::B.parent
end
end
def test_module_parents
assert_equal [ParentA::B, ParentA, Object], ParentA::B::C.module_parents
assert_equal [ParentA, Object], ParentA::B.module_parents
end
def test_parents
assert_equal [ParentA::B, ParentA, Object], ParentA::B::C.parents
assert_equal [ParentA, Object], ParentA::B.parents
assert_deprecated do
assert_equal [ParentA, Object], ParentA::B.parents
end
end
end
......@@ -224,7 +224,7 @@ def config
end
def railtie_namespace #:nodoc:
@railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) }
@railtie_namespace ||= self.class.module_parents.detect { |n| n.respond_to?(:railtie_namespace) }
end
protected
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册