Class: ActiveAdmin::MenuCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/active_admin/menu_collection.rb

Overview

A MenuCollection stores multiple menus for any given namespace. Namespaces delegate the addition of menu items to this class.

Instance Method Summary collapse

Constructor Details

#initializeMenuCollection

Returns a new instance of MenuCollection.


8
9
10
11
12
# File 'lib/active_admin/menu_collection.rb', line 8

def initialize
  @menus = {}
  @build_callbacks = []
  @built = false
end

Instance Method Details

#add(menu_name, menu_item_options = {}) ⇒ Object

Add a new menu item to a menu in the collection


15
16
17
18
19
# File 'lib/active_admin/menu_collection.rb', line 15

def add(menu_name, menu_item_options = {})
  menu = find_or_create(menu_name)

  menu.add menu_item_options
end

#before_build(&block) ⇒ Object

Add callbacks that will be run before the menu is built


47
48
49
# File 'lib/active_admin/menu_collection.rb', line 47

def before_build(&block)
  @build_callbacks.unshift(block)
end

#clear!Object


21
22
23
24
# File 'lib/active_admin/menu_collection.rb', line 21

def clear!
  @menus = {}
  @built = false
end

#exists?(menu_name) ⇒ Boolean

Returns:

  • (Boolean)

26
27
28
# File 'lib/active_admin/menu_collection.rb', line 26

def exists?(menu_name)
  @menus.keys.include? menu_name
end

#fetch(menu_name) ⇒ Object


30
31
32
33
34
35
# File 'lib/active_admin/menu_collection.rb', line 30

def fetch(menu_name)
  build_menus!

  @menus[menu_name] or
    raise NoMenuError, "No menu by the name of #{menu_name.inspect} in available menus: #{@menus.keys.join(", ")}"
end

Yields:


51
52
53
54
55
56
57
# File 'lib/active_admin/menu_collection.rb', line 51

def menu(menu_name)
  menu = find_or_create(menu_name)

  yield(menu) if block_given?

  menu
end

#on_build(&block) ⇒ Object

Add callbacks that will be run when the menu is going to be built. This helps use with reloading and allows configurations to add items to menus.

Parameters:

  • block (Proc)

    A block which will be ran when the menu is built. The will have the menu collection yielded.


42
43
44
# File 'lib/active_admin/menu_collection.rb', line 42

def on_build(&block)
  @build_callbacks << block
end