mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Delete AS::Dependencies.load_missing_constant
This commit is contained in:
parent
27262897c7
commit
1343a3a67d
2 changed files with 6 additions and 75 deletions
|
@ -326,74 +326,6 @@ module ActiveSupport # :nodoc:
|
||||||
mod_name == "Object" ? name.to_s : "#{mod_name}::#{name}"
|
mod_name == "Object" ? name.to_s : "#{mod_name}::#{name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Load the constant named +const_name+ which is missing from +from_mod+. If
|
|
||||||
# it is not possible to load the constant into from_mod, try its parent
|
|
||||||
# module using +const_missing+.
|
|
||||||
def load_missing_constant(from_mod, const_name)
|
|
||||||
from_mod_name = real_mod_name(from_mod)
|
|
||||||
unless qualified_const_defined?(from_mod_name) && Inflector.constantize(from_mod_name).equal?(from_mod)
|
|
||||||
raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!"
|
|
||||||
end
|
|
||||||
|
|
||||||
qualified_name = qualified_name_for(from_mod, const_name)
|
|
||||||
path_suffix = qualified_name.underscore
|
|
||||||
|
|
||||||
file_path = search_for_file(path_suffix)
|
|
||||||
|
|
||||||
if file_path
|
|
||||||
expanded = File.expand_path(file_path)
|
|
||||||
expanded.delete_suffix!(".rb")
|
|
||||||
|
|
||||||
if loading.include?(expanded)
|
|
||||||
raise "Circular dependency detected while autoloading constant #{qualified_name}"
|
|
||||||
else
|
|
||||||
require_or_load(expanded, qualified_name)
|
|
||||||
|
|
||||||
if from_mod.const_defined?(const_name, false)
|
|
||||||
return from_mod.const_get(const_name)
|
|
||||||
else
|
|
||||||
raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
|
|
||||||
return mod
|
|
||||||
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
|
|
||||||
# return constants from from_mod's parents.
|
|
||||||
begin
|
|
||||||
# Since Ruby does not pass the nesting at the point the unknown
|
|
||||||
# constant triggered the callback we cannot fully emulate constant
|
|
||||||
# name lookup and need to make a trade-off: we are going to assume
|
|
||||||
# that the nesting in the body of Foo::Bar is [Foo::Bar, Foo] even
|
|
||||||
# though it might not be. Counterexamples are
|
|
||||||
#
|
|
||||||
# class Foo::Bar
|
|
||||||
# Module.nesting # => [Foo::Bar]
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# or
|
|
||||||
#
|
|
||||||
# module M::N
|
|
||||||
# module S::T
|
|
||||||
# Module.nesting # => [S::T, M::N]
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# for example.
|
|
||||||
return parent.const_missing(const_name)
|
|
||||||
rescue NameError => e
|
|
||||||
raise unless e.missing_name? qualified_name_for(parent, const_name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
name_error = uninitialized_constant(qualified_name, const_name, receiver: from_mod)
|
|
||||||
name_error.set_backtrace(caller.reject { |l| l.start_with? __FILE__ })
|
|
||||||
raise name_error
|
|
||||||
end
|
|
||||||
|
|
||||||
# Remove the constants that have been autoloaded, and those that have been
|
# Remove the constants that have been autoloaded, and those that have been
|
||||||
# marked for unloading. Before each constant is removed a callback is sent
|
# marked for unloading. Before each constant is removed a callback is sent
|
||||||
# to its class/module if it implements +before_remove_const+.
|
# to its class/module if it implements +before_remove_const+.
|
||||||
|
|
|
@ -1571,18 +1571,17 @@ and understands strings that start with lowercase:
|
||||||
|
|
||||||
`underscore` accepts no argument though.
|
`underscore` accepts no argument though.
|
||||||
|
|
||||||
Rails class and module autoloading uses `underscore` to infer the relative path without extension of a file that would define a given missing constant:
|
Rails uses `underscore` to get a lowercased name for controller classes:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
# active_support/dependencies.rb
|
# actionpack/lib/abstract_controller/base.rb
|
||||||
def load_missing_constant(from_mod, const_name)
|
def controller_path
|
||||||
# ...
|
@controller_path ||= name.delete_suffix("Controller").underscore
|
||||||
qualified_name = qualified_name_for from_mod, const_name
|
|
||||||
path_suffix = qualified_name.underscore
|
|
||||||
# ...
|
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
For example, that value is the one you get in `params[:controller]`.
|
||||||
|
|
||||||
INFO: As a rule of thumb you can think of `underscore` as the inverse of `camelize`, though there are cases where that does not hold. For example, `"SSLError".underscore.camelize` gives back `"SslError"`.
|
INFO: As a rule of thumb you can think of `underscore` as the inverse of `camelize`, though there are cases where that does not hold. For example, `"SSLError".underscore.camelize` gives back `"SslError"`.
|
||||||
|
|
||||||
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
|
NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
|
||||||
|
|
Loading…
Reference in a new issue