I am using Rails 3.2.2 and want to load all the code in a certain directory recursively. For example:
[Rails root]/lib/my_lib/my_lib.rb
[Rails root]/lib/my_lib/subdir/support_file_00.rb
[Rails root]/lib/my_lib/subdir/support_file_01.rb
...
Based on Googling, I tried:
config.autoload_paths += ["#{Rails.root.to_s}/lib/my_lib/**"]
config.autoload_paths += ["#{Rails.root.to_s}/lib/my_lib/**/"]
config.autoload_paths += ["#{Rails.root.to_s}/lib/my_lib/**/*"]
config.autoload_paths += ["#{Rails.root.to_s}/lib/my_lib/{**}"]
config.autoload_paths += ["#{Rails.root.to_s}/lib/my_lib/{**}/"]
None of these load any of the code and I get "uninitialized constant" errors.
This loads files directly in /my_lib/
, but not subdirectories:
config.autoload_paths += ["#{Rails.root.to_s}/lib/my_lib"]
UPDATE
Thanks for the comments.
I put this in my application.rb
:
Dir["#{Rails.root.to_s}/lib/**/*.rb"].each { |file| config.autoload_paths += [file] }
The application launches but classes declared in my library are not available:
> MyClass
NameError: uninitialized constant MyClass