Class: ActiveAdmin::MenuItem

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ MenuItem

Build a new menu item

Parameters:

  • options (Hash) (defaults to: {})

    The options for the menu

Options Hash (options):

  • :label (String, Proc)

    The label to display for this menu item. It can either be a String or a Proc. If the option is Proc, it is called each time the label is requested.

  • :id (String)

    A custom id to reference this menu item with. If empty an id is automatically generated for you.

  • :url (String, Symbol)

    A string or symbol representing the url for this item. If it’s a symbol, the view will automatically call the method for you.

  • :priority (Integer)

    MenuItems are sorted by priority then by label. The lower the priority, the earlier in the menu the item will be displayed. Default: 10

  • :if (Proc)

    A block for the view to call to decide if this menu item should be displayed. The block should return true of false

  • :parent (Proc)

    The parent label to display for this menu item. Menu item will be nested under that label. It can either be a String or a Proc. If the option is Proc, it is called each time the label is requested.

Yields:

  • (_self)

Yield Parameters:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_admin/menu_item.rb', line 36

def initialize(options = {})
  @label    = options[:label]
  @id       = MenuItem.generate_item_id(options[:id] || label)
  @url      = options[:url]
  @priority = options[:priority] || 10
  @children = Menu::ItemCollection.new
  @parent   = options[:parent]

  @display_if_block = options[:if]

  yield(self) if block_given? # Builder style syntax
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



5
6
7
# File 'lib/active_admin/menu_item.rb', line 5

def children
  @children
end

#display_if_blockObject

Returns the display if block. If the block was not explicitly defined a default block always returning true will be returned.



102
103
104
# File 'lib/active_admin/menu_item.rb', line 102

def display_if_block
  @display_if_block
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/active_admin/menu_item.rb', line 5

def id
  @id
end

#labelObject

Returns the value of attribute label.



5
6
7
# File 'lib/active_admin/menu_item.rb', line 5

def label
  @label
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/active_admin/menu_item.rb', line 5

def parent
  @parent
end

#priorityObject

Returns the value of attribute priority.



5
6
7
# File 'lib/active_admin/menu_item.rb', line 5

def priority
  @priority
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/active_admin/menu_item.rb', line 5

def url
  @url
end

Class Method Details

.generate_item_id(id) ⇒ Object



49
50
51
# File 'lib/active_admin/menu_item.rb', line 49

def self.generate_item_id(id)
  id.to_s.downcase.gsub(" ", "_")
end

Instance Method Details

#<=>(other) ⇒ Object



94
95
96
97
98
# File 'lib/active_admin/menu_item.rb', line 94

def <=>(other)
  result = priority <=> other.priority
  result = label <=> other.label if result == 0
  result
end

#[](id) ⇒ Object

Returns the child item with the name passed in

@blog_menu["Create New"] => <#MenuItem @name="Create New" >


90
91
92
# File 'lib/active_admin/menu_item.rb', line 90

def [](id)
  @children.find_by_id(id)
end

#add(*menu_items) ⇒ Object



62
63
64
65
66
67
# File 'lib/active_admin/menu_item.rb', line 62

def add(*menu_items)
  menu_items.each do |menu_item|
    menu_item.parent = self
    @children << menu_item
  end
end

#ancestorsObject

Returns an array of the ancestory of this menu item The first item is the immediate parent fo the item



83
84
85
86
# File 'lib/active_admin/menu_item.rb', line 83

def ancestors
  return [] unless parent?
  [parent, parent.ancestors].flatten
end

#dom_idObject



77
78
79
# File 'lib/active_admin/menu_item.rb', line 77

def dom_id
  id.gsub( " ", '_' ).gsub( /[^a-z0-9_]/, '' )
end

#parent?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/active_admin/menu_item.rb', line 73

def parent?
  !parent.nil?
end