Ale*_*tis 6 ruby ruby-on-rails rails-engines
我正在编写一个可配置的Rails引擎.我有一个authentication_helper配置选项来定义应该before_action在需要身份验证的所有控制器中调用哪个帮助程序.
问题是我无法从引擎的控制器访问父应用程序的帮助程序.我的理解是,这是因为发动机是孤立的.
我考虑使用块而不是方法名称,但我不确定这是否可行,或者我是否能够从控制器外部干净地访问授权逻辑.
我过去使用过的Active Admin有一个类似的配置选项.我注意到他们的发动机没有隔离,所以也许我高估了发动机隔离的重要性?
是否有一种优雅的方式来获得引擎隔离的好处,同时还允许这种定制?或者我应该完全放弃隔离?
编辑#1
Brad Werth指出了我正确的方向,因为这适用于继承自ApplicationController::Base以下的常规控制器:
module MyBigFancyEngine
class Engine < Rails::Engine
isolate_namespace MyBigFancyEngine
config.to_prepare do
# Make the implementing application's helpers available to the engine.
# This is required for the overriding of engine views and helpers to work correctly.
MyBigFancyEngine::ApplicationController.helper Rails.application.helpers
end
end
end
Run Code Online (Sandbox Code Playgroud)
但是,我的引擎ApplicationController继承自RocketPant::Base,但没有提供helper方法.我试图使用一个简单的include(适用于常规控制器),但这也不起作用(控制器找不到帮助器).
有任何想法吗?
您可以通过在engine.rb文件中包含以下代码来公开引擎可用的实现应用程序帮助程序:
engine.rb
module MyBigFancyEngine
class Engine < Rails::Engine
isolate_namespace MyBigFancyEngine
config.to_prepare do
# Make the implementing application's helpers available to the engine.
# This is required for the overriding of engine views and helpers to work correctly.
MyBigFancyEngine::ApplicationController.helper Rails.application.helpers
end
end
end
Run Code Online (Sandbox Code Playgroud)
RailsAdmin 引擎也是隔离的,但它们具有与您想要实现的相同的配置选项。它们具有可配置的 before_filters 用于身份验证和授权。看看这个。
据我所知,它们只是像这样子类化父控制器::ApplicationController,或者您可以配置一个(参考)。
对于您的控制器,您可以创建自己的EngineController,它继承自并且可能只是创建一个方法,该方法直接通过父控制器RocketPant::Base调用配置的身份验证方法。send
由于该类RocketPant::Base不继承自,ApplicationController::Base我想您必须找到一些自定义方法来解决这个问题,并且不能采用 Rails 引擎的正常方法。也许您也可以尝试向 Rocket_pant 存储库提交问题,以添加该helper方法。据我所知,他们很快就想从ApplicationController::Base2.0 继承(参考)。
| 归档时间: |
|
| 查看次数: |
1718 次 |
| 最近记录: |