Module: MongoMapper::Acts::Tree::InstanceMethods
- Defined in:
- lib/mongo_mapper_acts_as_tree.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
- #ancestors ⇒ Object
- #children ⇒ Object
- #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
- #parent=(var) ⇒ 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
45 46 47 48 49 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 45 def ==(other) return true if other.equal?(self) return true if other.instance_of?(self.class) and other._id == self._id false end |
#ancestors ⇒ Object
93 94 95 96 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 93 def ancestors return [] if root? search_class.find(self[path_field]) end |
#children ⇒ Object
110 111 112 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 110 def children search_class.all(parent_id_field => self._id, :order => tree_order) end |
#descendants ⇒ Object
114 115 116 117 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 114 def descendants return [] if new_record? search_class.all(path_field => self._id, :order => tree_order) end |
#destroy_descendants ⇒ Object
158 159 160 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 158 def destroy_descendants search_class.destroy(self.descendants.map(&:_id)) end |
#fix_position ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 69 def fix_position if parent.nil? self[parent_id_field] = nil self[path_field] = [] self[depth_field] = 0 else self[parent_id_field] = parent._id self[path_field] = parent[path_field] + [parent._id] self[depth_field] = parent[depth_field] + 1 end end |
#is_ancestor_of?(other) ⇒ Boolean
123 124 125 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 123 def is_ancestor_of?(other) other[path_field].include?(self._id) end |
#is_descendant_of?(other) ⇒ Boolean
131 132 133 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 131 def is_descendant_of?(other) self[path_field].include?(other._id) end |
#is_or_is_ancestor_of?(other) ⇒ Boolean
127 128 129 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 127 def is_or_is_ancestor_of?(other) (other == self) or is_ancestor_of?(other) end |
#is_or_is_descendant_of?(other) ⇒ Boolean
135 136 137 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 135 def is_or_is_descendant_of?(other) (other == self) or is_descendant_of?(other) end |
#is_or_is_sibling_of?(other) ⇒ Boolean
143 144 145 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 143 def is_or_is_sibling_of?(other) (other == self) or is_sibling_of?(other) end |
#is_sibling_of?(other) ⇒ Boolean
139 140 141 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 139 def is_sibling_of?(other) (other != self) and (other[parent_id_field] == self[parent_id_field]) end |
#move_children ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 147 def move_children if @_will_move @_will_move = false for child in self.children child.fix_position child.save end @_will_move = true end end |
#parent ⇒ Object
81 82 83 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 81 def parent @_parent or (self[parent_id_field].nil? ? nil : search_class.find(self[parent_id_field])) end |
#parent=(var) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 51 def parent=(var) var = search_class.find(var) if var.is_a? String if self.descendants.include? var @_cyclic = true else @_parent = var fix_position @_will_move = true end end |
#root ⇒ Object
89 90 91 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 89 def root self[path_field].first.nil? ? self : search_class.find(self[path_field].first) end |
#root? ⇒ Boolean
85 86 87 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 85 def root? self[parent_id_field].nil? end |
#self_and_ancestors ⇒ Object
98 99 100 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 98 def self_and_ancestors ancestors << self end |
#self_and_descendants ⇒ Object
119 120 121 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 119 def self_and_descendants [self] + self.descendants end |
#self_and_siblings ⇒ Object
106 107 108 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 106 def self_and_siblings search_class.all(parent_id_field => self[parent_id_field], :order => tree_order) end |
#siblings ⇒ Object
102 103 104 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 102 def siblings search_class.all(:_id => {"$ne" => self._id}, parent_id_field => self[parent_id_field], :order => tree_order) end |
#will_save_tree ⇒ Object
63 64 65 66 67 |
# File 'lib/mongo_mapper_acts_as_tree.rb', line 63 def will_save_tree if @_cyclic errors.add(:base, "Can't be children of a descendant") end end |