diff options
author | Xavier Noria <fxn@hashref.com> | 2019-05-08 21:04:36 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2019-05-08 21:07:13 +0200 |
commit | 52125dc0f8669d8dd497427c7b177d5d04106e0c (patch) | |
tree | 7a062a2accbe00a7d3ea6862e6e3f42603e4f6f8 | |
parent | 44795cded6ea47c0dd46664173813c0ee7bb44b7 (diff) | |
download | rails-52125dc0f8669d8dd497427c7b177d5d04106e0c.tar.gz rails-52125dc0f8669d8dd497427c7b177d5d04106e0c.tar.bz2 rails-52125dc0f8669d8dd497427c7b177d5d04106e0c.zip |
documents restriction for explicit namespaces [ci skip]
-rw-r--r-- | guides/source/upgrading_ruby_on_rails.md | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/guides/source/upgrading_ruby_on_rails.md b/guides/source/upgrading_ruby_on_rails.md index c9ab655179..5bd4b06274 100644 --- a/guides/source/upgrading_ruby_on_rails.md +++ b/guides/source/upgrading_ruby_on_rails.md @@ -247,6 +247,40 @@ In that case, `app/models/concerns` is assumed to be a root directory (because i The `Concerns::` namespace worked with the classic autoloader as a side-effect of the implementation, but it was not really an intended behavior. An application using `Concerns::` needs to rename those classes and modules to be able to run in `zeitwerk` mode. +#### Autoloaded Constants and Explicit Namespaces + +If a namespace is defined in a file, as `Hotel` is here: + +``` +app/models/hotel.rb # Defines Hotel. +app/models/hotel/pricing.rb # Defines Hotel::Pricing. +``` + +the `Hotel` constant has to be set using the `class` or `module` keywords. For example: + +```ruby +class Hotel +end +``` + +is good. + +Alternatives like + +```ruby +Hotel = Class.new +``` + +or + +```ruby +Hotel = Struct.new +``` + +won't work, child objects like `Hotel::Pricing` won't be found. + +This restriction only applies to explicit namespaces. Classes and modules not defining a namespace can be defined using those idioms. + #### Spring and the `test` Environment Spring reloads the application code if something changes. In the `test` environment you need to enable reloading for that to work: |