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

Commit

Permalink
Added support for parent_id setter with blank value
Browse files Browse the repository at this point in the history
  • Loading branch information
Papipo authored and saks committed Jul 5, 2010
1 parent 7e2a125 commit 64d11f0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/mongoid_acts_as_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ def acts_as_tree(options = {})

self.class_eval do
define_method "#{parent_id_field}=" do | new_parent_id |
new_parent = self.class.find new_parent_id
new_parent.children.push self, false
if new_parent_id.present?
new_parent = self.class.find new_parent_id
new_parent.children.push self, false
else
self.write_attribute parent_id_field, nil
self[path_field] = []
self[depth_field] = 0
end
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/test_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ class TestMongoidActsAsTree < Test::Unit::TestCase
assert more_deep_child.ancestors.include? child
assert more_deep_child.ancestors.include? parent
end

should "assign blank parent_id" do
@child_1.parent_id = ''
@child_1.save

assert_nil @child_1.reload.parent_id
assert_equal 0, @child_1.depth
assert_equal [], @child_1.path
end

context "node" do
should "have a root" do
Expand Down

0 comments on commit 64d11f0

Please sign in to comment.