Class: Refinery::MenuItem

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

Instance Method Summary collapse

Instance Method Details

#ancestorsObject



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

def ancestors
  return @ancestors if @ancestors
  @ancestors = []
  p = self
  @ancestors << p until(p = p.parent).nil?

  @ancestors
end

#childrenObject



25
26
27
28
29
30
31
# File 'lib/refinery/menu_item.rb', line 25

def children
  @children ||= if has_children?
    menu.select{|item| item.type == type && item.parent_id == id}
  else
    []
  end
end

#descendantsObject



33
34
35
36
37
38
39
# File 'lib/refinery/menu_item.rb', line 33

def descendants
  @descendants ||= if has_descendants?
    menu.select{|item| item.type == type && item.lft > lft && item.rgt < rgt}
  else
    []
  end
end

#has_children?Boolean Also known as: has_descendants?

Returns:

  • (Boolean)


41
42
43
# File 'lib/refinery/menu_item.rb', line 41

def has_children?
  @has_children ||= (rgt > lft + 1)
end

#has_parent?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/refinery/menu_item.rb', line 47

def has_parent?
  !parent_id.nil?
end

#inspectObject



51
52
53
54
55
56
57
58
59
# File 'lib/refinery/menu_item.rb', line 51

def inspect
  hash = {}

  ATTRIBUTES.each do |attribute|
    hash[attribute] = self[attribute]
  end

  hash
end


61
62
63
# File 'lib/refinery/menu_item.rb', line 61

def menu
  ::Refinery.menus[menu_id]
end

#parentObject



65
66
67
# File 'lib/refinery/menu_item.rb', line 65

def parent
  @parent ||= (menu.detect{|item| item.type == type && item.id == parent_id} if has_parent?)
end

#siblingsObject Also known as: shown_siblings



69
70
71
# File 'lib/refinery/menu_item.rb', line 69

def siblings
  @siblings ||= ((has_parent? ? children : menu.roots) - [self])
end