Question: Namespacing within `app` directory


Namespacing within `app` directory

Answers 0
Added at 2017-01-05 13:01

In our app directory, we want some of the sub-directories to contain namespaced classes, and some that contain top-level classes. For example:

  • app/models/user.rb defines ::User
  • app/operations/foo.rb defines ::Operations::Foo
  • app/operations/user/foo.rb defines ::Operations::User::Foo

Our application.rb contains the following configuration:

config.paths =
config.paths.add 'app/models', eager_load: true
config.paths.add 'app', eager_load: true

This works fine in most cases, but sometimes in development mode and with Rails' autoreloading turned on, this leads to the wrong classes being loaded. For instance ::User is mistaken for Operations::User and vice-versa.

Is there a way to configure this behavior so that it works without any errors?

If not, the only workaround I can think of is to create a second directory for "namespaced" classes, along the lines of app and app_namespaced. Or else app/namespaced, since app-level code should reside within app. But these seem like ugly workarounds to me.

Answers to

Namespacing within `app` directory

Source Show
◀ Wstecz