Class: Refinery::MenuItem

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) apply_attributes!



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'core/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

+ (Object) attributes



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

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

Instance Method Details

- (Object) ancestors



35
36
37
38
39
40
41
42
# File 'core/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

- (Object) children



44
45
46
47
48
49
50
# File 'core/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

- (Object) descendants



52
53
54
55
56
57
58
# File 'core/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

- (Boolean) has_children? Also known as: has_descendants?

Returns:

  • (Boolean)


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

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

- (Boolean) has_parent?

Returns:

  • (Boolean)


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

def has_parent?
  !parent_id.nil?
end

- (Object) inspect



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

def inspect
  hash = {}

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

  hash.inspect
end

- (Object) original_id



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

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

- (Object) original_type



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

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

- (Object) parent



80
81
82
# File 'core/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

- (Object) siblings Also known as: shown_siblings



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

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