Ticket #42: betternestedset.patch
| File betternestedset.patch, 3.8 kB (added by rails, 11 months ago) |
|---|
-
test/acts_as_nested_set_test.rb
old new 310 310 assert_equal(set2(1), set2(2).parent) 311 311 assert_equal(set2(3), set2(7).parent) 312 312 end 313 314 def test_descendant_of_returns_true_when_the_childs_left_and_right_is_within_the_left_and_right_of_the_passed_in_node 315 child = set2(9) 316 root = set2(1) 317 assert child.descendant_of?(root), "child should have been a descendant of root" 318 319 parent = set2(4) 320 assert_equal parent.id, child.parent_id 321 assert child.descendant_of?(parent), "child should have been a descendant of its parent" 322 323 assert parent.descendant_of?(parent), "parent should have been a descendant of itself" 324 end 313 325 326 def test_descendant_of_returns_false_when_the_childs_left_and_right_are_not_within_the_left_and_right_of_the_passed_in_node 327 child = set2(9) 328 parent = set2(4) 329 assert !parent.descendant_of?(child), "parent should not have been a descendant of its child" 330 331 sibling = set2(8) 332 assert !sibling.descendant_of?(child), "sibling should not have been a descendant of the child" 333 assert !child.descendant_of?(sibling), "child should not have been a descendant of the sibling" 334 335 cousin = set2(7) 336 assert !cousin.descendant_of?(child), "cousin should not have been a descendant of the child" 337 assert !child.descendant_of?(cousin), "child should not have been a descendant of the cousin" 338 end 339 314 340 def test_ancestors 315 341 assert_equal([], set2(1).ancestors) 316 342 assert_equal([set2(1), set2(4), set2(9)], set2(10).ancestors) -
lib/better_nested_set.rb
old new 449 449 self[left_col_name] <=> x[left_col_name] 450 450 end 451 451 452 # Returns true if this is a descendant of the passed in node. Also returns true if this is the passed in node. 453 def descendant_of?(node) 454 node[left_col_name] <= self[left_col_name] && node[right_col_name] >= self[right_col_name] 455 end 456 452 457 # Deprecated. Returns true if this is a root node. 453 458 def root? 454 459 parent_id = self[parent_col_name] … … 925 930 raise ActiveRecord::ActiveRecordError, "You cannot move a node if left or right is nil" unless self[left_col_name] && self[right_col_name] 926 931 927 932 with_optional_transaction(transact) do 928 self.reload (:select => "#{left_col_name}, #{right_col_name}, #{parent_col_name}") # the lft/rgt values could be stale (target is reloaded below)933 self.reload #(:select => "#{left_col_name}, #{right_col_name}, #{parent_col_name}") # the lft/rgt values could be stale (target is reloaded below) 929 934 if target.is_a?(base_set_class) 930 target.reload (:select => "#{left_col_name}, #{right_col_name}, #{parent_col_name}") # could be stale935 target.reload #(:select => "#{left_col_name}, #{right_col_name}, #{parent_col_name}") # could be stale 931 936 else 932 937 target = self.class.find_in_nested_set(target) # load object if we were given an ID 933 938 end … … 1003 1008 ELSE #{parent_col_name} END", 1004 1009 scope_condition) 1005 1010 end 1006 self.reload (:select => "#{left_col_name}, #{right_col_name}, #{parent_col_name}")1007 target.reload (:select => "#{left_col_name}, #{right_col_name}, #{parent_col_name}")1011 self.reload #(:select => "#{left_col_name}, #{right_col_name}, #{parent_col_name}") 1012 target.reload #(:select => "#{left_col_name}, #{right_col_name}, #{parent_col_name}") 1008 1013 end 1009 1014 end 1010 1015
