Class: Refinery::MenuItem
- Inherits:
-
HashWithIndifferentAccess
- Object
- HashWithIndifferentAccess
- Refinery::MenuItem
- Defined in:
- lib/refinery/menu_item.rb
Class Method Summary collapse
Instance Method Summary collapse
- #ancestors ⇒ Object
- #children ⇒ Object
- #descendants ⇒ Object
- #has_children? ⇒ Boolean (also: #has_descendants?)
- #has_parent? ⇒ Boolean
- #inspect ⇒ Object
- #original_id ⇒ Object
- #original_type ⇒ Object
- #parent ⇒ Object
- #siblings ⇒ Object (also: #shown_siblings)
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 |
.attributes ⇒ Object
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
#ancestors ⇒ Object
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 |
#children ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/refinery/menu_item.rb', line 44 def children @children ||= if has_children? .select { |item| item.original_type == original_type && item.parent_id == original_id } else [] end end |
#descendants ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/refinery/menu_item.rb', line 52 def descendants @descendants ||= if has_descendants? .select{|item| item.original_type == original_type && item.lft > lft && item.rgt < rgt} else [] end end |
#has_children? ⇒ Boolean Also known as: has_descendants?
60 61 62 |
# File 'lib/refinery/menu_item.rb', line 60 def has_children? @has_children ||= (rgt > lft + 1) end |
#has_parent? ⇒ Boolean
66 67 68 |
# File 'lib/refinery/menu_item.rb', line 66 def has_parent? !parent_id.nil? end |
#inspect ⇒ Object
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_id ⇒ Object
25 26 27 |
# File 'lib/refinery/menu_item.rb', line 25 def original_id @original_id ||= self[:id] end |
#original_type ⇒ Object
29 30 31 |
# File 'lib/refinery/menu_item.rb', line 29 def original_type @original_type ||= self[:type] end |
#parent ⇒ Object
80 81 82 |
# File 'lib/refinery/menu_item.rb', line 80 def parent @parent ||= (.detect{|item| item.original_type == original_type && item.original_id == parent_id} if has_parent?) end |
#siblings ⇒ Object Also known as: shown_siblings
84 85 86 |
# File 'lib/refinery/menu_item.rb', line 84 def siblings @siblings ||= ((has_parent? ? parent.children : .roots) - [self]) end |