Question

I'm working on a Rails 3 app, and I've got a hierarchy of classes in my lib folder, e.g.:

lib
├── assets
├── tasks
│   └── import.rake
└── importer
    ├── base.rb
    └── source
        ├── facebook.rb
        ├── google.rb
        └── twitter.rb

I've updated config/application.rb to include this line:

config.autoload_paths += %W(#{config.root}/lib)

Then inside of Importer::Base, I have an instance method that attempts to load all classes in the Provider module, e.g.:

Importer::Source.constants.each do |class_name|
  Importer::Source.const_get(class_name).process
end

The three classes in lib/importer/base have a class hierarchy similar to:

module Importer
  module Source
    class Facebook
      # ...
    end
  end
end

When I call this method, Importer::Source.constants ends up returning an empty array. The classes appear to be lazy-loaded properly if I reference them by name directly, but they are not accessible in the constants call. How can I fix this?

Was it helpful?

Solution

Using @apneadiving's suggestion, I was able to fix this by adding this line to the beginning of my base.rb file:

Dir[Rails.root.join('lib/importer/source/**/*.rb')].each(&method(:require))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top