Class: Compass::Menu
- Inherits:
-
Object
- Object
- Compass::Menu
- 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
Defined Under Namespace
Class Method Summary collapse
-
.build(items = Compass.config.menu.items, **context) ⇒ ItemList
Allow building a menu from a list of items.
-
.item { ... } ⇒ Object
Define the root item of the menu.
Instance Method Summary collapse
-
#initialize(**context) ⇒ Menu
constructor
Initialize the menu with the given context.
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.:
= UserMenu.new(current_user: user)
.current_user # => user
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..items = ["UserMenu", "AdminMenu"]
Compass::Menu.build(current_user: user)
72 73 74 75 76 |
# File 'lib/compass/menu.rb', line 72 def self.build(items = Compass.config..items, **context) Array(items).map(&:constantize) .map { _1.new(**context).build } .then(&ItemList.method(:new)) end |