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(name, url, priority = 10, options = {}) {|_self| ... } ⇒ MenuItem

Returns a new instance of MenuItem.

Yields:

  • (_self)

Yield Parameters:



16
17
18
19
20
21
22
23
24
# File 'lib/active_admin/menu_item.rb', line 16

def initialize(name, url, priority = 10, options = {})
  @name, @url, @priority = name, url, priority
  @children = []
  @cached_url = {} # Stores the cached url in a hash to allow us to change it and still cache it

  @display_if_block = options.delete(:if)

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

Instance Attribute Details

#display_if_blockObject

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



75
76
77
# File 'lib/active_admin/menu_item.rb', line 75

def display_if_block
  @display_if_block
end

#nameObject

Returns the value of attribute name.



14
15
16
# File 'lib/active_admin/menu_item.rb', line 14

def name
  @name
end

#parentObject

Returns the value of attribute parent.



14
15
16
# File 'lib/active_admin/menu_item.rb', line 14

def parent
  @parent
end

#priorityObject

Returns the value of attribute priority.



14
15
16
# File 'lib/active_admin/menu_item.rb', line 14

def priority
  @priority
end

#urlObject

Returns the value of attribute url.



14
15
16
# File 'lib/active_admin/menu_item.rb', line 14

def url
  @url
end

Class Method Details

.generate_url(named_route) ⇒ Object

Generates a route using the rails application url helpers

Parameters:

  • named_route (Symbol)


10
11
12
# File 'lib/active_admin/menu_item.rb', line 10

def self.generate_url(named_route)
  Rails.application.routes.url_helpers.send(named_route)
end

Instance Method Details

#<=>(other) ⇒ Object



67
68
69
70
71
# File 'lib/active_admin/menu_item.rb', line 67

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

#[](name) ⇒ Object

Returns the child item with the name passed in

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


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

def [](name)
  @children.find{ |i| i.name == name }
end

#add(name, url, priority = 10, options = {}, &block) ⇒ Object



26
27
28
29
30
# File 'lib/active_admin/menu_item.rb', line 26

def add(name, url, priority=10, options = {}, &block)
  item = MenuItem.new(name, url, priority, options, &block)
  item.parent = self
  @children << item
end

#ancestorsObject

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



56
57
58
59
# File 'lib/active_admin/menu_item.rb', line 56

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

#childrenObject



32
33
34
# File 'lib/active_admin/menu_item.rb', line 32

def children
  @children.sort
end

#dom_idObject



40
41
42
# File 'lib/active_admin/menu_item.rb', line 40

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

#parent?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/active_admin/menu_item.rb', line 36

def parent?
  !parent.nil?
end