Module: MongoMapper::Plugins::Tree
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongo_mapper/plugins/tree.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #ancestors ⇒ Object
- #children ⇒ Object
- #descendants ⇒ Object
- #destroy_descendants ⇒ Object
- #fix_position(opts = {}) ⇒ 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
- #root ⇒ Object
- #root? ⇒ Boolean
- #self_and_ancestors ⇒ Object
- #self_and_descendants ⇒ Object
- #self_and_siblings ⇒ Object
- #siblings ⇒ Object
- #tree_search_class ⇒ Object
- #will_save_tree ⇒ Object
Instance Method Details
#ancestors ⇒ Object
48 49 50 51 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 48 def ancestors return [] if root? tree_search_class.find(self[path_field]).sort_by &:depth end |
#children ⇒ Object
70 71 72 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 70 def children tree_search_class.where(parent_id_field => self._id).sort(tree_order).all end |
#descendants ⇒ Object
74 75 76 77 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 74 def descendants return [] if new_record? tree_search_class.where(path_field => self._id).sort(tree_order).all end |
#destroy_descendants ⇒ Object
117 118 119 120 121 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 117 def destroy_descendants self.descendants.each do |descendant| descendant.destroy end end |
#fix_position(opts = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 23 def fix_position(opts = {}) if parent.nil? self[parent_id_field] = nil self[path_field] = [] self[depth_field] = 0 elsif !!opts[:force] || self.changes.include?(parent_id_field) @_will_move = true self[path_field] = parent[path_field] + [parent._id] self[depth_field] = parent[depth_field] + 1 end end |
#fix_position! ⇒ Object
35 36 37 38 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 35 def fix_position! fix_position(:force => true) save end |
#is_ancestor_of?(other) ⇒ Boolean
83 84 85 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 83 def is_ancestor_of?(other) other[path_field].include?(self._id) end |
#is_descendant_of?(other) ⇒ Boolean
91 92 93 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 91 def is_descendant_of?(other) self[path_field].include?(other._id) end |
#is_or_is_ancestor_of?(other) ⇒ Boolean
87 88 89 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 87 def is_or_is_ancestor_of?(other) (other == self) or is_ancestor_of?(other) end |
#is_or_is_descendant_of?(other) ⇒ Boolean
95 96 97 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 95 def is_or_is_descendant_of?(other) (other == self) or is_descendant_of?(other) end |
#is_or_is_sibling_of?(other) ⇒ Boolean
103 104 105 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 103 def is_or_is_sibling_of?(other) (other == self) or is_sibling_of?(other) end |
#is_sibling_of?(other) ⇒ Boolean
99 100 101 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 99 def is_sibling_of?(other) (other != self) and (other[parent_id_field] == self[parent_id_field]) end |
#move_children ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 107 def move_children if @_will_move @_will_move = false self.children.each do |child| child.fix_position! end @_will_move = true end end |
#root ⇒ Object
44 45 46 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 44 def root self[path_field].first.nil? ? self : tree_search_class.find(self[path_field].first) end |
#root? ⇒ Boolean
40 41 42 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 40 def root? self[parent_id_field].nil? end |
#self_and_ancestors ⇒ Object
53 54 55 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 53 def self_and_ancestors ancestors << self end |
#self_and_descendants ⇒ Object
79 80 81 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 79 def self_and_descendants [self] + self.descendants end |
#self_and_siblings ⇒ Object
64 65 66 67 68 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 64 def self_and_siblings tree_search_class.where({ parent_id_field => self[parent_id_field] }).sort(tree_order).all end |
#siblings ⇒ Object
57 58 59 60 61 62 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 57 def siblings tree_search_class.where({ :_id => { "$ne" => self._id }, parent_id_field => self[parent_id_field] }).sort(tree_order).all end |
#tree_search_class ⇒ Object
13 14 15 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 13 def tree_search_class self.class.tree_search_class end |
#will_save_tree ⇒ Object
17 18 19 20 21 |
# File 'lib/mongo_mapper/plugins/tree.rb', line 17 def will_save_tree if parent && self.descendants.include?(parent) errors.add(:base, :cyclic) end end |