Ticket #42: betternestedset.patch

File betternestedset.patch, 3.8 kB (added by rails, 11 months ago)

adds descendant_of? method

  • test/acts_as_nested_set_test.rb

    old new  
    310310    assert_equal(set2(1), set2(2).parent) 
    311311    assert_equal(set2(3), set2(7).parent) 
    312312  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 
    313325   
     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   
    314340  def test_ancestors 
    315341    assert_equal([], set2(1).ancestors) 
    316342    assert_equal([set2(1), set2(4), set2(9)], set2(10).ancestors) 
  • lib/better_nested_set.rb

    old new  
    449449          self[left_col_name] <=> x[left_col_name] 
    450450        end 
    451451         
     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         
    452457        # Deprecated. Returns true if this is a root node. 
    453458        def root? 
    454459          parent_id = self[parent_col_name] 
     
    925930          raise ActiveRecord::ActiveRecordError, "You cannot move a node if left or right is nil" unless self[left_col_name] && self[right_col_name] 
    926931           
    927932          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) 
    929934            if target.is_a?(base_set_class) 
    930               target.reload(:select => "#{left_col_name}, #{right_col_name}, #{parent_col_name}") # could be stale 
     935              target.reload #(:select => "#{left_col_name}, #{right_col_name}, #{parent_col_name}") # could be stale 
    931936            else 
    932937              target = self.class.find_in_nested_set(target) # load object if we were given an ID 
    933938            end 
     
    10031008                  ELSE #{parent_col_name} END", 
    10041009                scope_condition) 
    10051010            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}") 
    10081013          end 
    10091014        end 
    10101015