从父应用程序重新打开 Rails 3 引擎类

发布于 2024-10-20 07:12:41 字数 817 浏览 1 评论 0原文

就目前而言,您无法通过简单地在父应用程序的 /app 目录中添加相同的类来重新打开引擎的 /app 目录中包含的引擎类。例如:

/my_engine/app/controllers/users_controller.rb
/my_app/app/controllers/users_controller.rb

如果父应用程序中存在同名文件,则 my_engine 中的文件甚至不会加载。更多详细信息请参见:

http://www.cowboycoded.com/2011/02/28/why-you-cant-reopen-rails-3-engine-classes-from-the-parent-app/

我正在寻找一种解决方法,允许我将相同的文件名/类放在与父应用程序相同的路径中,然后重新打开而不是覆盖该类。也许我错过了一些明显的东西。我可以使用使用 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:

http://www.cowboycoded.com/2011/02/28/why-you-cant-reopen-rails-3-engine-classes-from-the-parent-app/

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶情人眼里出诗心の 2024-10-27 07:12:41

在 Rails 3.2.2 / Ruby 1.9 中,打开插件的重新加载,然后在重新打开类并添加功能之前使用 require_dependency 要求引擎中的类。即使在开发环境中(即类重新加载),这也有效。

# development.rb
config.reload_plugins = true 

# app/controllers/my_engine/documents_controller.rb
require_dependency MyEngine::Engine.root.join('app', 'controllers', 'my_engine', 'documents_controller').to_s

module MyEngine
  class DocumentsController
    def show
      render :text => 'different'
    end
  end
end

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).

# development.rb
config.reload_plugins = true 

# app/controllers/my_engine/documents_controller.rb
require_dependency MyEngine::Engine.root.join('app', 'controllers', 'my_engine', 'documents_controller').to_s

module MyEngine
  class DocumentsController
    def show
      render :text => 'different'
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文