Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/acts_as_tree_ext.rb,
lib/activerecord_ext.rb,
lib/activerecord_ext.rb
Overview
Wymaga do działania bibliotekli rand.rb raa.ruby-lang.org/project/rand/
Instance Method Summary collapse
-
#depth ⇒ Object
Return the tree depth.
- #has_children? ⇒ Boolean
- #has_siblings? ⇒ Boolean
- #leaf(random = false) ⇒ Object
- #mh_extensions ⇒ Object
-
#nodes_to_root ⇒ Object
Ile węzłów mamy do roota z miejsca gdzie jesteśmy.
-
#root ⇒ Object
Znajduje korzeń i zwraca go.
-
#valid_parent? ⇒ Boolean
Sprawdza czy nasz rodzic w ogole istnieje.
-
#width ⇒ Object
Return the tree width.
Instance Method Details
#depth ⇒ Object
Return the tree depth
17 18 19 20 |
# File 'lib/acts_as_tree_ext.rb', line 17 def depth return 0 unless has_children? children.inject(0) {|dep,c| dep > c.depth ? dep : c.depth} + 1 end |
#has_children? ⇒ Boolean
8 9 10 |
# File 'lib/acts_as_tree_ext.rb', line 8 def has_children? !self.children.size.zero? end |
#has_siblings? ⇒ Boolean
12 13 14 |
# File 'lib/acts_as_tree_ext.rb', line 12 def has_siblings? !self.siblings.size.zero? end |
#leaf(random = false) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/acts_as_tree_ext.rb', line 59 def leaf(random = false) unless self.has_children? return self end children = random ? self.children.shuffle : self.children children.each { |child| return child.leaf(random) } nil end |
#mh_extensions ⇒ Object
37 38 39 |
# File 'lib/activerecord_ext.rb', line 37 def mh_extensions self.class.mh_extensions end |
#nodes_to_root ⇒ Object
Ile węzłów mamy do roota z miejsca gdzie jesteśmy
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/acts_as_tree_ext.rb', line 29 def nodes_to_root stop = false levels = 0 parent = self.parent_id while !stop el = parent ? self.class.find(parent) : nil if el && el.parent_id != el.id parent = el.parent_id levels += 1 else stop = true end end levels end |
#root ⇒ Object
Znajduje korzeń i zwraca go
51 52 53 54 55 56 57 |
# File 'lib/acts_as_tree_ext.rb', line 51 def root el = self nodes_to_root.times { el = self.parent } el end |
#valid_parent? ⇒ Boolean
Sprawdza czy nasz rodzic w ogole istnieje
46 47 48 |
# File 'lib/acts_as_tree_ext.rb', line 46 def valid_parent? self.class.exists?(self.parent_id.to_i) end |
#width ⇒ Object
Return the tree width
23 24 25 26 |
# File 'lib/acts_as_tree_ext.rb', line 23 def width return 1 unless has_children? children.inject(0) {|sum,c| sum + c.width} end |