Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hook Rails Engine into global layout

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.

like image 955
unhollow Avatar asked Feb 19 '26 21:02

unhollow


1 Answers

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
like image 126
unhollow Avatar answered Feb 21 '26 12:02

unhollow