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

Instance Method Details

#ancestorsObject



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

#childrenObject



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

#descendantsObject



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_descendantsObject



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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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_childrenObject



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

#rootObject



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

Returns:

  • (Boolean)


40
41
42
# File 'lib/mongo_mapper/plugins/tree.rb', line 40

def root?
  self[parent_id_field].nil?
end

#self_and_ancestorsObject



53
54
55
# File 'lib/mongo_mapper/plugins/tree.rb', line 53

def self_and_ancestors
  ancestors << self
end

#self_and_descendantsObject



79
80
81
# File 'lib/mongo_mapper/plugins/tree.rb', line 79

def self_and_descendants
  [self] + self.descendants
end

#self_and_siblingsObject



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

#siblingsObject



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_classObject



13
14
15
# File 'lib/mongo_mapper/plugins/tree.rb', line 13

def tree_search_class
  self.class.tree_search_class
end

#will_save_treeObject



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