Read more

Zeitwerk: How to collapse folders in Rails

Niklas Hä.
November 08, 2023Software engineer at makandra GmbH

All direct child directories of app are automatically added to the eager- and autoload paths. They do NOT create a module for namespacing. This is intuitive, since there normally is no module Model, or module Controller. If you want to add a new base directory, there's no additional config needed.


├── controllers
├── helpers
├── inputs # No config needed 
├── mailers
├── models
├── uploaders # No config needed
├── util # No config needed
└── workers # No config needed
Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show snapshot

Sometimes it's handy to group files within a directory, but not reflect that grouping within the Ruby module structure. The typical example would be the concerns folder, which exists in new Rails applications by default and does not create a constant module Concerns.

├── models
    ├── concerns
        ├── shareable.rb # Defines constant Shareable, not Concerns::Shareable

If you want to collapse your own directories, you can do so with the following official api:

├── models
    ├── shared
        ├── shareable.rb # Defines constant Shared::Shareable

# e.g. in application.rb

# with collapsed shared dir
├── models
    ├── shared
        ├── shareable.rb # Defines constant Shareable

Posted by Niklas Hä. to makandra dev (2023-11-08 13:54)