为什么在 Rails 中使用 /app/lib 而不是 /lib?
Why use /app/lib instead of /lib in Rails?
在 sidekiq 文档中,有 this quote 关于在与自动加载错误相关的 Rails 项目中更喜欢使用 /app/lib
而不是 /lib
:
A lib/ directory will only cause pain. Move the code to app/lib/ and make sure the code inside follows the class/filename conventions.
此外,还有:
Don't configure extra paths in autoload_paths or eager_load_paths. That's a hack; follow the conventions! Any directory underneath app/ may contain Ruby code, you don't need to explicitly configure anything.
我的问题是:
这些使用 /app/lib
优于 /lib
的说法是否属实?
这是否仅对自动加载 Rails 相关对象(例如 AR 模型、控制器、作业等)有帮助?或者它也会帮助 PORO 吗?
这些评论是否只有在特定的上下文中才有意义?
根据我的经验,app/lib
更易于使用。您可以从字面上坚持使用 Class MathFunction
之类的东西,然后在其他地方(例如控制器或模块)使用 MathFunction.sqrRoot
.
要使用 /lib
,您需要使用 autoload_paths
配置您的 Rails 应用。 autoload_paths
还需要一些调整才能在 production. Matz himself discourages autoload
because it's in the process of being deprecated 中正常工作。
我唯一一次需要使用 lib
目录是为了制作自定义 rake 任务。否则我坚持 app/lib
.
在 sidekiq 文档中,有 this quote 关于在与自动加载错误相关的 Rails 项目中更喜欢使用 /app/lib
而不是 /lib
:
A lib/ directory will only cause pain. Move the code to app/lib/ and make sure the code inside follows the class/filename conventions.
此外,还有:
Don't configure extra paths in autoload_paths or eager_load_paths. That's a hack; follow the conventions! Any directory underneath app/ may contain Ruby code, you don't need to explicitly configure anything.
我的问题是:
这些使用 /app/lib
优于 /lib
的说法是否属实?
这是否仅对自动加载 Rails 相关对象(例如 AR 模型、控制器、作业等)有帮助?或者它也会帮助 PORO 吗?
这些评论是否只有在特定的上下文中才有意义?
根据我的经验,app/lib
更易于使用。您可以从字面上坚持使用 Class MathFunction
之类的东西,然后在其他地方(例如控制器或模块)使用 MathFunction.sqrRoot
.
要使用 /lib
,您需要使用 autoload_paths
配置您的 Rails 应用。 autoload_paths
还需要一些调整才能在 production. Matz himself discourages autoload
because it's in the process of being deprecated 中正常工作。
我唯一一次需要使用 lib
目录是为了制作自定义 rake 任务。否则我坚持 app/lib
.