Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Commit

Permalink
all tree tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
saks committed Apr 4, 2010
1 parent a896a52 commit 94048a5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
12 changes: 9 additions & 3 deletions lib/mongoid_acts_as_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ def fix_position
self[path_field] = []
self[depth_field] = 0
else
self[parent_id_field] = parent._id
self[path_field] = parent[path_field] + [parent._id]
self.send "#{parent_id_field}=", parent._id
self.send "#{path_field}=", (parent[path_field] + [parent._id])
self[depth_field] = parent[depth_field] + 1
self.save
end
end

Expand Down Expand Up @@ -126,6 +127,8 @@ def children

def descendants
return [] if new_record?
# self.class.find(:all, :conditions => {:path.in => self._id})
# self.class.where(path_field.to_sym.in => self._id)
self.class.all(:conditions => {path_field => self._id})#, :order => tree_order})
end

Expand Down Expand Up @@ -158,9 +161,12 @@ def is_or_is_sibling_of?(other)
end

def move_children
# puts "move_children for #{self.name}" if $verbose

if @_will_move
@_will_move = false
for child in self.children
# puts "fixing position for #{child.name}" if $verbose
child.fix_position
child.save
end
Expand Down Expand Up @@ -190,7 +196,7 @@ def find_children_for_owner
end

def <<(object)
if @owner.descendants.include? object
if object.descendants.include? @owner
object.instance_variable_set :@_cyclic, true
else
object.send "#{@owner.parent_id_field}=", @owner._id
Expand Down
11 changes: 3 additions & 8 deletions test/test_tree.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'helper'
require 'set'

$verbose = false

class TestMongoidActsAsTree < Test::Unit::TestCase
context "Tree" do
setup do
Expand Down Expand Up @@ -118,14 +120,7 @@ class TestMongoidActsAsTree < Test::Unit::TestCase
should "move children on save" do
@root_2.children << @child_2

assert !@root_2.is_or_is_ancestor_of?(@child_2_1)
assert !@child_2_1.is_or_is_descendant_of?(@root_2)
assert !@root_2.descendants.include?(@child_2_1)

@child_2.save
@child_2_1.reload

# puts Category.all.map{|d| "#{d.name}: parent:#{d.parent_id and Category.find(d.parent_id).name}, path:#{d.path.map{|id| Category.find(id).name}.inspect}"}
@child_2_1.reload

assert @root_2.is_or_is_ancestor_of?(@child_2_1)
assert @child_2_1.is_or_is_descendant_of?(@root_2)
Expand Down

0 comments on commit 94048a5

Please sign in to comment.