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

Commit

Permalink
parent_id attribute writer now acts as expected. issue#1 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
saks committed Jul 4, 2010
1 parent bd9ac0c commit 8b648b0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/mongoid_acts_as_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ def acts_as_tree(options = {})
field path_field, :type => Array, :default => [], :index => true
field depth_field, :type => Integer, :default => 0

self.class_eval do
define_method "#{parent_id_field}=" do | new_parent_id |
new_parent = self.class.find new_parent_id
new_parent.children << self
end
end

after_save :move_children
validate :will_save_tree
before_destroy :destroy_descendants
Expand Down Expand Up @@ -64,11 +71,11 @@ def will_save_tree

def fix_position
if parent.nil?
self[parent_id_field] = nil
self.write_attribute parent_id_field, nil
self[path_field] = []
self[depth_field] = 0
else
self[parent_id_field] = parent._id
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
Expand Down Expand Up @@ -181,7 +188,7 @@ def <<(object)
if object.descendants.include? @parent
object.instance_variable_set :@_cyclic, true
else
object[object.parent_id_field] = @parent._id
object.write_attribute object.parent_id_field, @parent._id
object[object.path_field] = @parent[@parent.path_field] + [@parent._id]
object[object.depth_field] = @parent[@parent.depth_field] + 1
object.instance_variable_set :@_will_move, true
Expand All @@ -201,7 +208,7 @@ def delete(object_or_id)
object_or_id
end

object[object.parent_id_field] = nil
object.write_attribute object.parent_id_field, nil
object[object.path_field] = []
object[object.depth_field] = 0
object.save
Expand Down
31 changes: 31 additions & 0 deletions test/test_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,37 @@ class TestMongoidActsAsTree < Test::Unit::TestCase
assert eql_arrays?(Category.roots, [@root_1, @root_2])
end

should "assign parent_id" do
child = Category.create :name => 'child'
parent = Category.create :name => 'parent'

child.parent_id = parent.id
child.save

assert_equal parent.children.first.id, child.id
assert_equal parent.id, child.parent_id
assert parent.children.include? child

assert_equal 1, child.depth
assert_equal [parent.id], child.path

more_deep_child = Category.create :name => 'more deep child'
more_deep_child.parent_id = child.id

assert_equal child.children.first.id, more_deep_child.id
assert_equal child.id, more_deep_child.parent_id
assert child.children.include? more_deep_child

assert_equal 2, more_deep_child.depth
assert_equal [parent.id, child.id], more_deep_child.path

assert parent.descendants.include? child
assert parent.descendants.include? more_deep_child

assert more_deep_child.ancestors.include? child
assert more_deep_child.ancestors.include? parent
end

context "node" do
should "have a root" do
assert_equal @root_1.root, @root_1
Expand Down

0 comments on commit 8b648b0

Please sign in to comment.