Rails のオートロード パスをすべて一覧表示するにはどうすればよいですか?
Railsコンソールでこれを行うと、構成に追加されたカスタムパスのみがリストされます:
$ rails c
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > MyRailsApp::Application.config.autoload_paths
=> []
Rails のオートロード パスをすべて一覧表示するにはどうすればよいですか?
Railsコンソールでこれを行うと、構成に追加されたカスタムパスのみがリストされます:
$ rails c
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > MyRailsApp::Application.config.autoload_paths
=> []
すべての自動ロードパスにアクセスできますActiveSupport::Dependencies.autoload_paths
コンソールから呼び出すかrails r 'puts ActiveSupport::Dependencies.autoload_paths'
、コマンド ラインから実行します。
詳細はこちら (Rails 4 の場合ですが、Rails 3 にも適用されます): http://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoload-paths
更新: 以下の ActiveSupport::Dependencies.autoload_paths を使用した Laura の回答を参照してください。別の方法として、この回答をここに残しました。
Rails::Engine
Rails アプリケーションのモジュールに含まれている には、次のメソッドがあります。
def _all_autoload_paths
@_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
end
したがって、次のいずれかを実行できます。
(MyRailsApp::Application.config.autoload_paths + MyRailsApp::Application.config.eager_load_paths + MyRailsApp::Application.config.autoload_once_paths).uniq
また:
[:autoload_paths, :eager_load_paths, :autoload_once_paths].collect{|m|MyRailsApp::Application.config.send(m)}.flatten.uniq
あるいは単に:
MyRailsApp::Application._all_autoload_paths
Rails 3.2.9 のデフォルトの結果は次のとおりです。
["/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"]
これには、他の gem およびカスタム ロード パスによって追加されたすべての自動ロード パスを含める必要があります。