Class: Refinery::MenuItem

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply_attributes!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/refinery/menu_item.rb', line 9

def apply_attributes!
  attributes.each do |attribute|
    class_eval %{
      def #{attribute}
        @#{attribute} ||= self[:#{attribute}]
      end
    } unless self.respond_to?(attribute)
    class_eval %{
      def #{attribute}=(attr)
        @#{attribute} = attr
      end
    } unless self.respond_to?(:"#{attribute}=")
  end
end

.attributesObject



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

def attributes
  [:title, :parent_id, :lft, :rgt, :depth, :url, :menu, :menu_match]
end

Instance Method Details

#ancestorsObject



35
36
37
38
39
40
41
42
# File 'lib/refinery/menu_item.rb', line 35

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

  @ancestors
end

#childrenObject



44
45
46
47
48
49
50
# File 'lib/refinery/menu_item.rb', line 44

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

#descendantsObject



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

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

#has_children?Boolean Also known as: has_descendants?

Returns:

  • (Boolean)


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

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

#has_parent?Boolean

Returns:

  • (Boolean)


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

def has_parent?
  !parent_id.nil?
end

#inspectObject



70
71
72
73
74
75
76
77
78
# File 'lib/refinery/menu_item.rb', line 70

def inspect
  hash = {}

  self.class.attributes.each do |attribute|
    hash[attribute] = self[attribute]
  end

  hash.inspect
end

#original_idObject



25
26
27
# File 'lib/refinery/menu_item.rb', line 25

def original_id
  @original_id ||= self[:id]
end

#original_typeObject



29
30
31
# File 'lib/refinery/menu_item.rb', line 29

def original_type
  @original_type ||= self[:type]
end

#parentObject



80
81
82
# File 'lib/refinery/menu_item.rb', line 80

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

#siblingsObject Also known as: shown_siblings



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

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