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



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

#childrenObject



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

#descendantsObject



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_descendantsObject



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_positionObject



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

Returns:

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

Returns:

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

Returns:

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

Returns:

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

Returns:

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

Returns:

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



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

#parentObject



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

#rootObject



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

Returns:

  • (Boolean)


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

def root?
  self[parent_id_field].nil?
end

#self_and_ancestorsObject



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

def self_and_ancestors
  ancestors << self
end

#self_and_descendantsObject



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

def self_and_descendants
  [self] + self.descendants
end

#self_and_siblingsObject



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

#siblingsObject



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_treeObject



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