Class: Compass::Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/compass/menu.rb

Overview

Compass::Menu is the base class for exposing menus from a Compass enabled application. This class acts as a builder for the menu items. Each root menu should extend this class to build a menu tree.

I.e.:

class UserMenu < Compass::Menu
  include Rails.application.routes.mounted_helpers

  item do
    label "Users"

    item do
      label "Profile"
      url :profile_url
    end

    item do
      label "Settings"
      url :user_settings_url
    end
  end
end

In most cases an application will expose multiple menus, and so an Application level base class should be created:

I.e.:

class ApplicationMenu < Compass::Menu
  include Rails.application.routes.mounted_helpers
end

class UserMenu < ApplicationMenu
  item do
    label "Users"

    item do
      label "Profile"
      url :profile_url
    end

    item do
      label "Settings"
      url :user_settings_url
    end
  end
end

See Also:

Defined Under Namespace

Classes: Item, ItemList

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**context) ⇒ Menu

Initialize the menu with the given context. Each context key will be available as a method on the instance. Because items are built from the context, they will have access to these context methods

I.e.:

menu = UserMenu.new(current_user: user)
menu.current_user # => user

Parameters:

  • context (Hash)

    the context to pass to the menu items



111
112
113
114
115
# File 'lib/compass/menu.rb', line 111

def initialize(**context)
  context.each do |key, value|
    define_singleton_method(key) { value }
  end
end

Class Method Details

.build(items = Compass.config.menu.items, **context) ⇒ ItemList

Allow building a menu from a list of items. This is useful for building a menu from a list of items with the given context.

I.e.:

Compass::Menu.build(["UserMenu", "AdminMenu"], current_user: user)

By default, it will build items from ‘Compass.menus`.

I.e.:

Compass.config.menu.items = ["UserMenu", "AdminMenu"]
Compass::Menu.build(current_user: user)

Parameters:

  • items (Array<String>) (defaults to: Compass.config.menu.items)

    the list of menu items to build

  • context (Hash)

    the context to pass to the menu items

Returns:



72
73
74
75
76
# File 'lib/compass/menu.rb', line 72

def self.build(items = Compass.config.menu.items, **context)
  Array(items).map(&:constantize)
              .map { _1.new(**context).build }
              .then(&ItemList.method(:new))
end

.item { ... } ⇒ Object

Define the root item of the menu. This method should be called in the class definition to define the root item of the menu.

I.e.:

class UserMenu < Compass::Menu
  item do
    label "Users"

    item do
      label "Profile"
      url :profile_url
    end
  end
end

Yields:

  • the block to define the root item



95
96
97
98
99
# File 'lib/compass/menu.rb', line 95

def self.item(&block)
  define_method(:build) do
    @item ||= Item.new(self, &block)
  end
end