如何列出 Rails 中的所有自动加载路径 [英] How to list all autoload paths in Rails
本文介绍了如何列出 Rails 中的所有自动加载路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你如何列出 Rails 中的所有自动加载路径?
How do you list all of the autoload paths in Rails?
在 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
=> []
推荐答案
更新:请使用下面的 ActiveSupport::Dependencies.autoload_paths 查看劳拉的回答.我把这个答案留在这里作为替代方法.
在包含在 Rails 应用程序模块中的 Rails::Engine
中,有以下方法:
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
所以,你可以这样做:
(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 中的默认结果是:
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"]
这应该包括其他 gem 添加的所有自动加载路径和自定义加载路径.
This should include all the autoload paths that were added by other gems and custom load paths.
这篇关于如何列出 Rails 中的所有自动加载路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文