Module: MongoMapper::Acts::Tree::InstanceMethods

Defined in:
lib/mongo_mapper_acts_as_tree.rb

Instance Method Summary collapse

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

#ancestorsObject



94
95
96
97
# File 'lib/mongo_mapper_acts_as_tree.rb', line 94

def ancestors
  return [] if root?
  search_class.find(self[path_field])
end

#childrenObject



111
112
113
# File 'lib/mongo_mapper_acts_as_tree.rb', line 111

def children
  search_class.all(parent_id_field => self._id, :order => tree_order)
end

#descendantsObject



115
116
117
118
# File 'lib/mongo_mapper_acts_as_tree.rb', line 115

def descendants
  return [] if new_record?
  search_class.all(path_field => self._id, :order => tree_order)
end

#destroy_descendantsObject



159
160
161
# File 'lib/mongo_mapper_acts_as_tree.rb', line 159

def destroy_descendants
  search_class.destroy(self.descendants.map(&:_id))
end

#fix_positionObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mongo_mapper_acts_as_tree.rb', line 70

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

Returns:

  • (Boolean)


124
125
126
# File 'lib/mongo_mapper_acts_as_tree.rb', line 124

def is_ancestor_of?(other)
  other[path_field].include?(self._id)
end

#is_descendant_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/mongo_mapper_acts_as_tree.rb', line 132

def is_descendant_of?(other)
  self[path_field].include?(other._id)
end

#is_or_is_ancestor_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/mongo_mapper_acts_as_tree.rb', line 128

def is_or_is_ancestor_of?(other)
  (other == self) or is_ancestor_of?(other)
end

#is_or_is_descendant_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/mongo_mapper_acts_as_tree.rb', line 136

def is_or_is_descendant_of?(other)
  (other == self) or is_descendant_of?(other)
end

#is_or_is_sibling_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/mongo_mapper_acts_as_tree.rb', line 144

def is_or_is_sibling_of?(other)
  (other == self) or is_sibling_of?(other)
end

#is_sibling_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/mongo_mapper_acts_as_tree.rb', line 140

def is_sibling_of?(other)
  (other != self) and (other[parent_id_field] == self[parent_id_field])
end

#move_childrenObject



148
149
150
151
152
153
154
155
156
157
# File 'lib/mongo_mapper_acts_as_tree.rb', line 148

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

#parentObject



82
83
84
# File 'lib/mongo_mapper_acts_as_tree.rb', line 82

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
62
# File 'lib/mongo_mapper_acts_as_tree.rb', line 51

def parent=(var)
  self[parent_id_field] = nil if var.nil?   
  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

#rootObject



90
91
92
# File 'lib/mongo_mapper_acts_as_tree.rb', line 90

def root
  self[path_field].first.nil? ? self : search_class.find(self[path_field].first)
end

#root?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/mongo_mapper_acts_as_tree.rb', line 86

def root?
  self[parent_id_field].nil?
end

#self_and_ancestorsObject



99
100
101
# File 'lib/mongo_mapper_acts_as_tree.rb', line 99

def self_and_ancestors
  ancestors << self
end

#self_and_descendantsObject



120
121
122
# File 'lib/mongo_mapper_acts_as_tree.rb', line 120

def self_and_descendants
  [self] + self.descendants
end

#self_and_siblingsObject



107
108
109
# File 'lib/mongo_mapper_acts_as_tree.rb', line 107

def self_and_siblings
  search_class.all(parent_id_field => self[parent_id_field], :order => tree_order)
end

#siblingsObject



103
104
105
# File 'lib/mongo_mapper_acts_as_tree.rb', line 103

def siblings
  search_class.all(:_id => {"$ne" => self._id}, parent_id_field => self[parent_id_field], :order => tree_order)
end

#will_save_treeObject



64
65
66
67
68
# File 'lib/mongo_mapper_acts_as_tree.rb', line 64

def will_save_tree
  if @_cyclic
    errors.add(:base, "Can't be children of a descendant")
  end
end