Xero
Xero

Reputation: 4175

RoR autoload sub directories

I have created a "services" dir in app/ where I have created some classes (services). Now it's looks like this :

-app
   -services
      -class1
      -class2
      -class3
      -class4

For now, I have added app/services in config/spring.rb

%w(
  .ruby-version
  .rbenv-vars
  tmp/restart.txt
  tmp/caching-dev.txt
  app/services
  app/errors
).each { |path| Spring.watch(path) }

So my classes in services are loaded.

But now, I want to do this :

-app
   -services
      -dir1
         -class1
         -class2
      -dir2
         -class3
      -class4

I got error :

uninitialized constant TransactionService::AuthorizeRequest

How autoload a dir and subdir ?

Upvotes: 0

Views: 85

Answers (1)

nattfodd
nattfodd

Reputation: 1890

Rails tries to guess namespaces by dirnames & filenames. So, to have a TransactionService::AuthorizeRequest class name, you should have the exact following structure:

app
- services
-- transaction_service
--- authorize_request.rb

Your class sould looks like :

module TransactionService
  class AuthorizeRequest
  end
end

Upvotes: 1

Related Questions