How to list all autoload paths in Rails
Asked Answered
H

3

15

How do you list all of the autoload paths in Rails?

In Rails console when I do this, it only lists the custom paths added to the config:

$ rails c
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > MyRailsApp::Application.config.autoload_paths
=> [] 
Hanhana answered 29/11, 2012 at 15:27 Comment(0)
H
17

Update: please see Laura's answer using ActiveSupport::Dependencies.autoload_paths below. I've left this answer here as an alternate method.

In Rails::Engine which is included in the Rails application's module, there is the following method:

def _all_autoload_paths
  @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
end

So, you could either do:

(MyRailsApp::Application.config.autoload_paths + MyRailsApp::Application.config.eager_load_paths + MyRailsApp::Application.config.autoload_once_paths).uniq

or:

[:autoload_paths, :eager_load_paths, :autoload_once_paths].collect{|m|MyRailsApp::Application.config.send(m)}.flatten.uniq

or just:

MyRailsApp::Application._all_autoload_paths

The default result in Rails 3.2.9 is:

["/path/to/my_rails_app/app/assets", "/path/to/my_rails_app/app/controllers", "/path/to/my_rails_app/app/helpers", "/path/to/my_rails_app/app/mailers", "/path/to/my_rails_app/app/models"]

This should include all the autoload paths that were added by other gems and custom load paths.

Hanhana answered 29/11, 2012 at 15:27 Comment(0)
W
26

You can access all the autoload paths through ActiveSupport::Dependencies.autoload_paths

Invoke it from console or run rails r 'puts ActiveSupport::Dependencies.autoload_paths' from command line.

More info here (For Rails 4, but it applies to Rails 3 as well): http://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoload-paths

Webbed answered 7/4, 2016 at 7:46 Comment(2)
Works in Rails 5 as wellChalmer
Work in Rails 6 as well!Render
H
17

Update: please see Laura's answer using ActiveSupport::Dependencies.autoload_paths below. I've left this answer here as an alternate method.

In Rails::Engine which is included in the Rails application's module, there is the following method:

def _all_autoload_paths
  @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
end

So, you could either do:

(MyRailsApp::Application.config.autoload_paths + MyRailsApp::Application.config.eager_load_paths + MyRailsApp::Application.config.autoload_once_paths).uniq

or:

[:autoload_paths, :eager_load_paths, :autoload_once_paths].collect{|m|MyRailsApp::Application.config.send(m)}.flatten.uniq

or just:

MyRailsApp::Application._all_autoload_paths

The default result in Rails 3.2.9 is:

["/path/to/my_rails_app/app/assets", "/path/to/my_rails_app/app/controllers", "/path/to/my_rails_app/app/helpers", "/path/to/my_rails_app/app/mailers", "/path/to/my_rails_app/app/models"]

This should include all the autoload paths that were added by other gems and custom load paths.

Hanhana answered 29/11, 2012 at 15:27 Comment(0)
T
4
Rails.application.instance_variable_get(:"@_all_autoload_paths")
Technocracy answered 14/3, 2018 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.