Permalink
Browse files

Revert to non-recursive descendants, split move callbacks into move_a…

…bsolute and move_relative
  • Loading branch information...
1 parent fd4b03c commit 5aeaa1f6f04d3c44159ca85b0be631e08c008616 @sled committed Jun 20, 2011
Showing with 22 additions and 61 deletions.
  1. +13 −46 lib/mongoid_acts_as_tree.rb
  2. +3 −9 test/models/user_tree.rb
  3. +6 −6 test/test_order.rb
@@ -72,7 +72,8 @@ def parent_with_checking=(new_parent)
before_destroy :destroy_descendants
- define_callbacks :move, :terminator => "result==false"
+ define_callbacks :move_absolute, :terminator => "result==false"
+ define_callbacks :move_relative, :terminator => "result==false"
define_callbacks :unlink, :terminator => "result==false"
end
@@ -229,15 +230,7 @@ def parent_id
def path=(new_path)
write_attribute path_field, new_path
end
-
-
- def walk_descendants(&block)
- self.children.each do |c_child|
- block.call(c_child)
- c_child.walk_descendants(block)
- end
- end
-
+
end
#proxy class
@@ -271,7 +264,7 @@ def <<(object, will_save=true)
# 1. parameter = is absolute move ?
# 2. parameter = parent
- object.run_callbacks :move do
+ object.run_callbacks :move_absolute do
object.write_attribute object.parent_id_field, @parent._id
object.path = @parent.path + [@parent._id]
@@ -288,36 +281,17 @@ def <<(object, will_save=true)
object.acts_as_tree_options[:order] = [object.depth_field, :asc]
# will not have any children if new record (unsaved parent condition)
-
- unless object.new_record?
-
- self.y { |this|
- lambda { |c_children|
- c_children.each do |c_child|
- c_child.run_callbacks :move do
- c_child.depth = c_child.depth + delta_depth
- c_child.path = c_child.path.slice(prev_depth, c_child.path.length - prev_depth).unshift(*object.path)
- # only will_save == false will block autosave
- c_child.save if will_save != false && object.tree_autosave
-
- this.call(c_child.children)
- end
- end
- }
- }.call(object.children)
-
-
-
- #object.descendants.each do |c_desc|
+ unless object.new_record?
+ object.descendants.each do |c_desc|
# maybe set parent nil, because there will be a lot of queries if there are many children!!
- #c_desc.run_callbacks :move do
+ c_desc.run_callbacks :move_relative do
# we need to adapt depth
- #c_desc.depth = c_desc.depth + delta_depth
- #c_desc.path = c_desc.path.slice(prev_depth, c_desc.path.length - prev_depth).unshift(*object.path)
+ c_desc.depth = c_desc.depth + delta_depth
+ c_desc.path = c_desc.path.slice(prev_depth, c_desc.path.length - prev_depth).unshift(*object.path)
# only will_save == false will block autosave
- #c_desc.save if will_save != false && object.tree_autosave
- #end
- #end
+ c_desc.save if will_save != false && object.tree_autosave
+ end
+ end
end
# restore old order
@@ -373,14 +347,7 @@ def clear
self.each do | child |
@parent.children.delete child
end
- end
-
- def y
- lambda { |f| f.call(f) }.call(
- lambda do |g|
- yield(lambda { |*n| g.call(g).call(*n) })
- end)
- end
+ end
private
View
@@ -13,19 +13,13 @@ class UserTree
acts_as_tree :scope => :user_id
- set_callback :move, :before, :custom_before_move
- set_callback :move, :after, :custom_after_move
+ set_callback :move_absolute, :before, :custom_before_absolute_move
set_callback :unlink, :before, :custom_before_unlink
- def custom_before_move
+ def custom_before_absolute_move
return self.moveable
end
-
- def custom_after_move
-
- end
-
-
+
def custom_before_unlink
return self.unlinkable
end
View
@@ -21,14 +21,14 @@ class TestMongoidActsAsTree < Test::Unit::TestCase
should "be in order" do
assert_equal OrderedCategory.roots.to_a, [@root_2, @root_1]
- assert_equal @root_1.children, [@child_1, @child_3, @child_2]
+ assert_equal @root_1.children.to_a, [@child_1, @child_3, @child_2]
- assert_equal @root_1.descendants, [@child_1, @child_2_1, @child_3, @child_2]
- assert_equal @root_1.self_and_descendants, [@child_1, @root_1, @child_2_1, @child_3, @child_2]
+ assert_equal @root_1.descendants.to_a, [@child_1, @child_2_1, @child_3, @child_2]
+ assert_equal @root_1.self_and_descendants.to_a, [@child_1, @root_1, @child_2_1, @child_3, @child_2]
- assert_equal @child_2.siblings, [@child_1, @child_3]
- assert_equal @child_2.self_and_siblings, [@child_1, @child_3, @child_2]
- assert_equal @root_1.self_and_siblings, [@root_2, @root_1]
+ assert_equal @child_2.siblings.to_a, [@child_1, @child_3]
+ assert_equal @child_2.self_and_siblings.to_a, [@child_1, @child_3, @child_2]
+ assert_equal @root_1.self_and_siblings.to_a, [@root_2, @root_1]
end
end
end

0 comments on commit 5aeaa1f

Please sign in to comment.