Module: Mongoid::Acts::Tree::InstanceMethods
- Defined in:
- lib/mongoid_acts_as_tree.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](field_name) ⇒ Object
- #[]=(field_name, value) ⇒ Object
- #ancestors ⇒ Object
- #children ⇒ Object
- #children=(new_children_list) ⇒ Object (also: #replace)
- #descendants ⇒ Object
- #destroy_descendants ⇒ Object
- #fix_position ⇒ Object
- #is_ancestor_of?(other) ⇒ Boolean
- #is_descendant_of?(other) ⇒ Boolean
- #is_or_is_ancestor_of?(other) ⇒ Boolean
- #is_or_is_descendant_of?(other) ⇒ Boolean
- #is_or_is_sibling_of?(other) ⇒ Boolean
- #is_sibling_of?(other) ⇒ Boolean
- #move_children ⇒ Object
- #parent ⇒ Object
- #root ⇒ Object
- #root? ⇒ Boolean
- #self_and_ancestors ⇒ Object
- #self_and_descendants ⇒ Object
- #self_and_siblings ⇒ Object
- #siblings ⇒ Object
- #will_save_tree ⇒ Object
Instance Method Details
#==(other) ⇒ Object
67 68 69 70 71 |
# File 'lib/mongoid_acts_as_tree.rb', line 67 def ==(other) return true if other.equal?(self) return true if other.kind_of?([:class]) and other._id == self._id false end |
#[](field_name) ⇒ Object
59 60 61 |
# File 'lib/mongoid_acts_as_tree.rb', line 59 def [](field_name) self.send field_name end |
#[]=(field_name, value) ⇒ Object
63 64 65 |
# File 'lib/mongoid_acts_as_tree.rb', line 63 def []=(field_name, value) self.send "#{field_name}=", value end |
#ancestors ⇒ Object
104 105 106 107 |
# File 'lib/mongoid_acts_as_tree.rb', line 104 def ancestors return [] if root? [:class].where(:_id.in => self[path_field]).order_by(depth_field) end |
#children ⇒ Object
121 122 123 |
# File 'lib/mongoid_acts_as_tree.rb', line 121 def children Children.new self end |
#children=(new_children_list) ⇒ Object Also known as: replace
125 126 127 128 129 130 |
# File 'lib/mongoid_acts_as_tree.rb', line 125 def children=(new_children_list) self.children.clear new_children_list.each do | child | self.children << child end end |
#descendants ⇒ Object
134 135 136 137 |
# File 'lib/mongoid_acts_as_tree.rb', line 134 def descendants return [] if new_record? self.class.all_in(path_field => [self._id]).order_by tree_order end |
#destroy_descendants ⇒ Object
179 180 181 |
# File 'lib/mongoid_acts_as_tree.rb', line 179 def destroy_descendants self.descendants.each &:destroy end |
#fix_position ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/mongoid_acts_as_tree.rb', line 79 def fix_position if parent.nil? self.write_attribute parent_id_field, nil self[path_field] = [] self[depth_field] = 0 else self.write_attribute parent_id_field, parent._id self[path_field] = parent[path_field] + [parent._id] self[depth_field] = parent[depth_field] + 1 self.save end end |
#is_ancestor_of?(other) ⇒ Boolean
143 144 145 |
# File 'lib/mongoid_acts_as_tree.rb', line 143 def is_ancestor_of?(other) other[path_field].include?(self._id) end |
#is_descendant_of?(other) ⇒ Boolean
151 152 153 |
# File 'lib/mongoid_acts_as_tree.rb', line 151 def is_descendant_of?(other) self[path_field].include?(other._id) end |
#is_or_is_ancestor_of?(other) ⇒ Boolean
147 148 149 |
# File 'lib/mongoid_acts_as_tree.rb', line 147 def is_or_is_ancestor_of?(other) (other == self) or is_ancestor_of?(other) end |
#is_or_is_descendant_of?(other) ⇒ Boolean
155 156 157 |
# File 'lib/mongoid_acts_as_tree.rb', line 155 def is_or_is_descendant_of?(other) (other == self) or is_descendant_of?(other) end |
#is_or_is_sibling_of?(other) ⇒ Boolean
163 164 165 |
# File 'lib/mongoid_acts_as_tree.rb', line 163 def is_or_is_sibling_of?(other) (other == self) or is_sibling_of?(other) end |
#is_sibling_of?(other) ⇒ Boolean
159 160 161 |
# File 'lib/mongoid_acts_as_tree.rb', line 159 def is_sibling_of?(other) (other != self) and (other[parent_id_field] == self[parent_id_field]) end |
#move_children ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/mongoid_acts_as_tree.rb', line 167 def move_children if @_will_move @_will_move = false self.children.each do | child | child.fix_position child.save end @_will_move = true end end |
#parent ⇒ Object
92 93 94 |
# File 'lib/mongoid_acts_as_tree.rb', line 92 def parent @_parent or (self[parent_id_field].nil? ? nil : [:class].find(self[parent_id_field])) end |
#root ⇒ Object
100 101 102 |
# File 'lib/mongoid_acts_as_tree.rb', line 100 def root self[path_field].first.nil? ? self : [:class].find(self[path_field].first) end |
#root? ⇒ Boolean
96 97 98 |
# File 'lib/mongoid_acts_as_tree.rb', line 96 def root? self[parent_id_field].nil? end |
#self_and_ancestors ⇒ Object
109 110 111 |
# File 'lib/mongoid_acts_as_tree.rb', line 109 def self_and_ancestors ancestors << self end |
#self_and_descendants ⇒ Object
139 140 141 |
# File 'lib/mongoid_acts_as_tree.rb', line 139 def self_and_descendants [self] + self.descendants end |
#self_and_siblings ⇒ Object
117 118 119 |
# File 'lib/mongoid_acts_as_tree.rb', line 117 def self_and_siblings [:class].where(parent_id_field => self[parent_id_field]).order_by tree_order end |
#siblings ⇒ Object
113 114 115 |
# File 'lib/mongoid_acts_as_tree.rb', line 113 def siblings [:class].where(:_id.ne => self._id, parent_id_field => self[parent_id_field]).order_by tree_order end |
#will_save_tree ⇒ Object
73 74 75 76 77 |
# File 'lib/mongoid_acts_as_tree.rb', line 73 def will_save_tree if @_cyclic errors.add(:base, "Can't be children of a descendant") end end |