In our catalog, appwe want some subdirectories to contain classes with names, and some to top-level classes. For instance:
app/models/user.rb defines ::Userapp/operations/foo.rb defines ::Operations::Fooapp/operations/user/foo.rb defines ::Operations::User::Foo
Our application.rbcontains the following configuration:
config.paths = Rails::Paths::Root.new(Rails.root)
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 when Rails startup is turned on, this leads to loading of the wrong classes. For example, it is ::Usermistaken for Operations::Userand vice versa.
Is there a way to configure this behavior so that it works without errors?
If not, the only workaround I can think of is to create a second directory for the classes with names along the appand lines app_namespaced. Or else app/namespaced, since application-level code must be inside app. But they seem like ugly workarounds to me.
Edit: small example given by @dgilperez:
class User
end
class Group
def some_method
User.new
end
end
module Operations
end
module Operations::User
class Create
def some_method
::User.new
end
end
end
source
share