从父应用程序重新打开 Rails 3 引擎类
就目前而言,您无法通过简单地在父应用程序的 /app
目录中添加相同的类来重新打开引擎的 /app
目录中包含的引擎类。例如:
/my_engine/app/controllers/users_controller.rb
/my_app/app/controllers/users_controller.rb
如果父应用程序中存在同名文件,则 my_engine
中的文件甚至不会加载。更多详细信息请参见:
我正在寻找一种解决方法,允许我将相同的文件名/类放在与父应用程序相同的路径中,然后重新打开而不是覆盖该类。也许我错过了一些明显的东西。我可以使用使用 class_eval 的单独文件(不同的文件名)来完成这项工作,但我对该解决方案并不满意。对于这个问题有什么优雅的解决方案吗?
我还想知道这种限制背后是否有原因,或者这只是 Rails 加载文件方式的结果(请参阅包含的链接)而不是故意的。在我看来,改变引擎的负载行为以允许以这种方式重新打开类将是 Rails 中的一个很好的功能。我知道一开始这让我很困惑,而且我相信其他开发人员也会遇到这个问题。
As it stands now, you can't reopen Engine classes contained within the engine's /app
directory by simply adding the same class in the parent app's /app
dir. For example:
/my_engine/app/controllers/users_controller.rb
/my_app/app/controllers/users_controller.rb
The file from my_engine
will not even load if there is a file with the same name in the parent app. More details here:
I am looking for a workaround that will allow me to drop the same filename/class in the same path as the parent app, and reopen instead of overwrite the class. Maybe I am missing something obvious. I am able to make this work with a separate file (different filename) that uses class_eval, but I am not really happy with that solution. Any ideas on an elegant solution for this?
I am also wondering if there is a reason behind this restriction, or is it just a result of how rails loads files (see included link) and not intentional. It seems to me that changing the load behavior of engines to allow reopening classes in this manner would be a good feature in rails. I know it confused me at first, and I am sure other developers will struggle with this problem as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Rails 3.2.2 / Ruby 1.9 中,打开插件的重新加载,然后在重新打开类并添加功能之前使用
require_dependency
要求引擎中的类。即使在开发环境中(即类重新加载),这也有效。In Rails 3.2.2 / Ruby 1.9 turn on reloading of plugins, then require the class in the engine using
require_dependency
before reopening the class and adding functionality. This works even in development environment (i.e class reloading).