Module: Mongoid::Acts::Tree::InstanceMethods

Defined in:
lib/mongoid_acts_as_tree.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



69
70
71
72
73
# File 'lib/mongoid_acts_as_tree.rb', line 69

def ==(other)
  return true if other.equal?(self)
  return true if other.instance_of?(self.class) and other._id == self._id
  false
end

#[](field_name) ⇒ Object



61
62
63
# File 'lib/mongoid_acts_as_tree.rb', line 61

def [](field_name)
  self.send field_name
end

#[]=(field_name, value) ⇒ Object



65
66
67
# File 'lib/mongoid_acts_as_tree.rb', line 65

def []=(field_name, value)
  self.send "#{field_name}=", value
end

#ancestorsObject



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

def ancestors
  return [] if root?
  self.class.where(:_id.in => self[path_field]).order_by(depth_field)
end

#childrenObject



123
124
125
# File 'lib/mongoid_acts_as_tree.rb', line 123

def children
  Children.new self
end

#children=(new_children_list) ⇒ Object Also known as: replace



127
128
129
130
131
132
# File 'lib/mongoid_acts_as_tree.rb', line 127

def children=(new_children_list)
  self.children.clear
  new_children_list.each do | child |
    self.children << child
  end
end

#descendantsObject



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

def descendants
  return [] if new_record?
  self.class.all_in(path_field => [self._id]).order_by tree_order
end

#destroy_descendantsObject



181
182
183
# File 'lib/mongoid_acts_as_tree.rb', line 181

def destroy_descendants
  self.descendants.each &:destroy
end

#fix_positionObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mongoid_acts_as_tree.rb', line 81

def fix_position
  if parent.nil?
    self.write_attribute parent_id_field, nil
    self[path_field] = []
    self[depth_field] = 0
  else
    self.write_attribute parent_id_field, parent._id
    self[path_field] = parent[path_field] + [parent._id]
    self[depth_field] = parent[depth_field] + 1
    self.save
  end
end

#is_ancestor_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/mongoid_acts_as_tree.rb', line 145

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

#is_descendant_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/mongoid_acts_as_tree.rb', line 153

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

#is_or_is_ancestor_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/mongoid_acts_as_tree.rb', line 149

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

#is_or_is_descendant_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/mongoid_acts_as_tree.rb', line 157

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

#is_or_is_sibling_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/mongoid_acts_as_tree.rb', line 165

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

#is_sibling_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/mongoid_acts_as_tree.rb', line 161

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

#move_childrenObject



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/mongoid_acts_as_tree.rb', line 169

def move_children

  if @_will_move
    @_will_move = false
    self.children.each do | child |
      child.fix_position
      child.save
    end
    @_will_move = true
  end
end

#parentObject



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

def parent
  @_parent or (self[parent_id_field].nil? ? nil : self.class.find(self[parent_id_field]))
end

#rootObject



102
103
104
# File 'lib/mongoid_acts_as_tree.rb', line 102

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

#root?Boolean

Returns:

  • (Boolean)


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

def root?
  self[parent_id_field].nil?
end

#self_and_ancestorsObject



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

def self_and_ancestors
  ancestors << self
end

#self_and_descendantsObject



141
142
143
# File 'lib/mongoid_acts_as_tree.rb', line 141

def self_and_descendants
  [self] + self.descendants
end

#self_and_siblingsObject



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

def self_and_siblings
  self.class.where(parent_id_field => self[parent_id_field]).order_by tree_order
end

#siblingsObject



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

def siblings
  self.class.where(:_id.ne => self._id, parent_id_field => self[parent_id_field]).order_by tree_order
end

#will_save_treeObject



75
76
77
78
79
# File 'lib/mongoid_acts_as_tree.rb', line 75

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