将 Rails Engine 纳入全局布局

发布于 2024-12-06 16:20:55 字数 508 浏览 0 评论 0原文

我目前正在编写一个模块化 Rails 应用程序,其中每个功能都位于 Rails 引擎内。我已经设置了我的第一个引擎,到目前为止一切正常。现在我想知道将引擎挂接到当前在 app/views/layouts/application.html.haml 中呈现的全局导航的最佳方法是什么,如下所示:

%nav#main-nav
  %ul
    %li
      = link_to "Users", users_path, :class => "no-submenu settings"
      %ul
        %li ...

我找到的最接近的东西是 spreeHookListener,它使用 deface gem。不幸的是 deface 只适用于 html/erb 输出,因为它使用 nokogiri 解析 DOM,这无论如何都不是最好的主意。

I'm currently writing a modular rails app where every functionality is inside a rails engine. I've set up my first engine and everything's working fine so far. now I'm wondering what is the best way to hook the engine into my global navigation that is currently rendered in app/views/layouts/application.html.haml, like this:

%nav#main-nav
  %ul
    %li
      = link_to "Users", users_path, :class => "no-submenu settings"
      %ul
        %li ...

The closest thing I found was the HookListener of spree, which uses the deface gem. unfortunately deface only works with html/erb output since it parses the DOM with nokogiri, which isn't the best idea anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

z祗昰~ 2024-12-13 16:20:55

作为记录,我已经这样解决了:

  • 将基本应用程序移动到引擎/gem,以便轻松地需要
  • Navigation
  • 寄存器从 MyEngine 添加到此类中

core/lib/navigation.rb:

class Navigation

  @registered_blocks = {}

  class << self
    def register(name, &block)
      @registered_blocks[name] ||= block
    end

    def bind(root)
      @registered_blocks.each do |name, block|
        block.call(root)
      end
    end
  end
end

myext/lib/myext/engine.rb:

Navigation.register :myext do |root|
  root.item :mylink, "My Link", "/"
end

config/navigation.rb (对于简单导航):

navigation.items do |root|
  Navigation.bind(root)
end

for the record, i've solved it like this:

  • move the base app to an engine/gem as well to make it easily require'able
  • add a Navigation class
  • register into this class from MyEngine

core/lib/navigation.rb:

class Navigation

  @registered_blocks = {}

  class << self
    def register(name, &block)
      @registered_blocks[name] ||= block
    end

    def bind(root)
      @registered_blocks.each do |name, block|
        block.call(root)
      end
    end
  end
end

myext/lib/myext/engine.rb:

Navigation.register :myext do |root|
  root.item :mylink, "My Link", "/"
end

config/navigation.rb (for simple-navigation):

navigation.items do |root|
  Navigation.bind(root)
end
肥爪爪 2024-12-13 16:20:55

在您的应用程序中创建一个模块,您将在每个引擎中包含该模块,例如 MyAppModule

class CmsModule::Railtie < ...
  include MyAppModule
end

然后使用 included 挂钩在您的应用程序中注册您的导航等。您可以将所有挂钩等封装在 MyAppModule 中,为所有引擎提供一致的集成。

create a module in your app, which you'll include in every engine, e.g. MyAppModule

class CmsModule::Railtie < ...
  include MyAppModule
end

then use the included hook to register your navigation etc with your application. you can encapsulate all your hooks etc within the MyAppModule, providing a consistent integration for all your engines.

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