Sha256: 1d141cfd85f7ffac574316e5212f3a08766c6f8340cca5840f2c335d2d7a5db9

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

module ActiveAdmin

  class MenuBuilder

    def self.build_for_namespace(namespace)
      new(namespace).menu
    end

    attr_reader :menu

    def initialize(namespace)
      @namespace = namespace
    end

    def menu
      @menu ||= build_menu
    end

    private

    def namespace
      @namespace
    end

    def build_menu
      menu = Menu.new

      Dashboards.add_to_menu(namespace, menu)

      namespace.resources.each do |resource|
        register_with_menu(menu, resource) if resource.include_in_menu?
      end

      menu
    end

    # Does all the work of registernig a config with the menu system
    def register_with_menu(menu, resource)
      # The menu we're going to add this resource to
      add_to = menu

      # Adding as a child
      if resource.parent_menu_item_name
        # Create the parent if it doesn't exist
        unless menu[resource.parent_menu_item_name]
          item = MenuItem.new(:label => resource.parent_menu_item_name, :url => "#", :id => resource.parent_menu_item_name)
          add_to.add(item)
        end

        add_to = menu[resource.parent_menu_item_name]
      end

      if add_to[resource.menu_item.id]
        existing = add_to[resource.menu_item.id]
        add_to.children.delete(existing)
        add_to.add(resource.menu_item)
        resource.menu_item.add(*existing.children)
      else
        add_to.add resource.menu_item
      end
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activeadmin-0.5.1 lib/active_admin/menu_builder.rb
activeadmin-0.5.0 lib/active_admin/menu_builder.rb
activeadmin-0.5.0.pre1 lib/active_admin/menu_builder.rb
activeadmin-0.5.0.pre lib/active_admin/menu_builder.rb