Method: ActiveSupport::Dependencies::ModuleConstMissing#const_missing
- Defined in:
- lib/active_support/dependencies.rb
#const_missing(const_name, nesting = nil) ⇒ Object
Use const_missing to autoload associations so we don't have to require_association when using single-table inheritance.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/active_support/dependencies.rb', line 175 def const_missing(const_name, nesting = nil) klass_name = name.presence || "Object" unless nesting # We'll assume that the nesting of Foo::Bar is ["Foo::Bar", "Foo"] # even though it might not be, such as in the case of # class Foo::Bar; Baz; end nesting = [] klass_name.to_s.scan(/::|$/) { nesting.unshift $` } end # If there are multiple levels of nesting to search under, the top # level is the one we want to report as the lookup fail. error = nil nesting.each do |namespace| begin return Dependencies.load_missing_constant Inflector.constantize(namespace), const_name rescue NoMethodError then raise rescue NameError => e error ||= e end end # Raise the first error for this set. If this const_missing came from an # earlier const_missing, this will result in the real error bubbling # all the way up raise error end |