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

Defined in:
lib/mongoid_tree.rb

Instance Method Summary collapse

Instance Method Details

#<=>(another_node) ⇒ Object

Comparable



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

def <=> (another_node)
    position <=> another_node.position
end

#ancestorsObject



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

def ancestors
  base_class.where(:_id.in => parent_ids)
end

#ancestors_and_selfObject



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

def ancestors_and_self
  ancestors << self
end

#base_classObject



194
195
196
197
198
199
# File 'lib/mongoid_tree.rb', line 194

def base_class
  @base_class ||= begin
    parent_classes = self.class.ancestors
    parent_classes[parent_classes.index(Mongoid::Acts::Tree::InstanceMethods) - 1]
  end
end

#breadth_firstObject Also known as: bfs

Returns the whole subtree including itself as array



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mongoid_tree.rb', line 114

def breadth_first
    result = []
    queue = [self]
    while !queue.empty?
        node = queue.shift
        result << node
        node.children.sort.each do |child|
            queue << child
        end
    end
    return result
end

#depthObject



70
71
72
# File 'lib/mongoid_tree.rb', line 70

def depth
    parent_ids.count
end

#depth_firstObject Also known as: dfs

Returns the whole subtree including itself as array



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/mongoid_tree.rb', line 100

def depth_first
    result = [self]
    if child_ids.empty?
        return result
    else
        children.sort.each do |child|
            result += child.depth_first
        end    
    end
    return result                    
end

#insert_after(new_child) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/mongoid_tree.rb', line 138

def insert_after ( new_child )
    new_child.position = position + 1
    parent.children.each do |child|
        if child.position >= new_child.position
            child.update_attributes(:position => child.position + 1)
        end
    end
    parent.children << new_child
end

#insert_before(new_child) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/mongoid_tree.rb', line 128

def insert_before( new_child )
    new_child.position = position
    parent.children.each do |child|
        if child.position >= new_child.position
            child.update_attributes(:position => child.position + 1)
        end
    end
    parent.children << new_child
end

#leaf?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/mongoid_tree.rb', line 74

def leaf?
   child_ids.empty?
end

#move_to(target_node) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/mongoid_tree.rb', line 156

def move_to(target_node)
    # unhinge - I was getting a nil on another implementation, so this is a bit longer but works
    unhinge
    # and append
    target_node.children << self
    # recurse through subtree
    rebuild_paths
end

#move_to_position(target_node, index) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/mongoid_tree.rb', line 165

def move_to_position(target_node, index)
  if index > target_node.child_ids.count
    move_to(target_node)
  else
    # unhinge - I was getting a nil on another implementation, so this is a bit longer but works
    child_ids_array = parent.child_ids.clone
    child_ids_array.delete(id)
    parent.update_attributes(:child_ids => child_ids_array )
    self.update_attributes(:parent_ids => [], :position => nil )
    
    target_node.children.sort[index - 1].insert_before(self)
    
    # recurse through subtree
    rebuild_paths
  end
end

#parentObject



66
67
68
# File 'lib/mongoid_tree.rb', line 66

def parent
    parents.last
end

#rebuild_pathsObject



183
184
185
186
187
188
# File 'lib/mongoid_tree.rb', line 183

def rebuild_paths
    update_path
    children.each do |child|
        child.rebuild_paths
    end
end

#rootObject



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

def root
  base_class.find(parent_ids.first) unless root?
end

#root?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/mongoid_tree.rb', line 78

def root?
  parent_ids.empty?
end

#unhingeObject



148
149
150
151
152
153
154
# File 'lib/mongoid_tree.rb', line 148

def unhinge
  # unhinge - I was getting a nil on another implementation, so this is a bit longer but works
  child_ids_array = parent.child_ids.clone
  child_ids_array.delete(id)
  parent.update_attributes(:child_ids => child_ids_array )
  self.update_attributes(:parent_ids => [], :position => nil )
end

#update_pathObject



190
191
192
# File 'lib/mongoid_tree.rb', line 190

def update_path
    self.update_attributes(:parent_ids => self.parent.parent_ids + [self.parent.id])
end