在 Rails 开发中加载类后代 [英] Loading class descendants in rails development

查看:36
本文介绍了在 Rails 开发中加载类后代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在本地进入 rails 控制台时,我需要能够从控制器看到所有类的后代.我有这个 Api::BaseController 我所有的 Api 控制器都继承自它.我遇到的问题是,当我跳入 rails 控制台以检查后代中的 Api 控制器时,它一直是空的,直到我调用它们.这可能与开发中的类如何不急切加载有关,并且它们不在本地缓存.

I need to be able to see all of the class descendants from a controller when I go into the rails console locally. I have this Api::BaseController which all my Api controller inherit from. The issue I have is when I hop in to the rails console to check which Api controller are in the descendants, it comes up empty until I call them. This probably has something to do with how the classes in development aren't eager loaded, and they're not cached locally.

sample_app$ rails c
Loading development environment (Rails 4.2.0)
2.1.5 :001 > Api::BaseController.descendants
 => []
2.1.5 :002 > Api::V1::FoosController
 => Api::V1::FoosController
2.1.5 :003 > Api::BaseController.descendants
 => [Api::V1::FoosController]

从这个例子中,你可以看到当我第一次调用 Api::BaseController 上的后代时,它是一个空数组.在调用其中一个控制器之后,该类将被加载并显示为后代.在这种情况下,V1 以及 V2、V3 等中可能有任意数量的控制器......

From this example, you can see when I call descendants on the Api::BaseController the first time, it's an empty array. After calling one of the controllers that class will be then loaded and will show up as a descendant. In this case, there could be an any number of controllers in V1 as well as V2, V3, etc...

作为一个愚蠢丑陋的黑客,我可以做到

As a stupid ugly hack, I could do

Dir.glob("#{Rails.root.join('app', 'controllers', 'api', 'v1')}/**/*.rb").each(&method(:require_dependency))

但我不想每次进入控制台时都必须写.我也在研究 gem,绝对不想把这种代码放在我的 gem 中.

but I don't want to have to write that each time I enter the console. I'm also working on a gem, and definitely don't want to put this sort of code in my gem.

另一种选择是在开发中缓存类,但这本身会导致巨大的问题.有人有什么想法吗?

The other option is caching classes in development, but that causes a huge issues on it's own. Anyone have any ideas?

编辑另一种选择是调用 Rails.application.eager_load!.如果我只能在我的 API 文件夹中指定控制器,这个选项就可以正常工作.然后我就不必急切加载整个应用程序,而只需加载我需要的一小部分控制器.

Edit Another option would be to call Rails.application.eager_load!. This option would work fine if I could specify only controllers in my API folder. Then I wouldn't have to eager load the entire app, but just a small subset of controllers that I need.

推荐答案

我找到了以下帖子:http://avinmathew.com/using-rails-descendants-method-in-development/

简而言之,它说明了以下内容:

In short it says the following:

enviroments/development.rb 中添加以下内容:

In enviroments/development.rb add the following:

config.eager_load_paths += Dir['path/to/files/*.rb']
ActionDispatch::Reloader.to_prepare do
  Dir['path/to/files/*.rb'].each {|file| require_dependency file}
end

第一行添加启动应用程序(或控制台)时应加载的路径其余的告诉 Rails 在每个请求上重新加载类.

The first line adds the path that should be loaded when you start your app (or console) and the rest tells rails to reload the classes on each request.

这篇关于在 Rails 开发中加载类后代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆