Ticket #37: leaves_and_parents_class_methods.patch

File leaves_and_parents_class_methods.patch, 2.8 kB (added by rails, 1 year ago)

Methods, tests, and documentation.

  • test/acts_as_nested_set_test.rb

    old new  
    8787    assert_equal(10, NestedSet.roots.size) # May change if STI behavior changes 
    8888  end 
    8989   
     90  def test_class_parents 
     91    assert_equal(8, NestedSetWithStringScope.parents.size) 
     92  end 
     93   
     94  def test_class_leaves 
     95    assert_equal(12, NestedSetWithStringScope.leaves.size) 
     96  end 
     97   
     98  def test_class_proper_count_of_leaves_and_parents 
     99    assert_equal(NestedSetWithStringScope.count, NestedSetWithStringScope.parents.size + NestedSetWithStringScope.leaves.size) 
     100  end 
     101   
    90102  def test_check_all_1 
    91103    assert_nothing_raised {NestedSetWithStringScope.check_all} 
    92104    NestedSetWithStringScope.update_all("lft = 3", "id = 103") 
  • lib/better_nested_set.rb

    old new  
    9090          acts_as_nested_set_options[:class].find(:all, :conditions => "(#{acts_as_nested_set_options[:parent_column]} IS NULL OR #{acts_as_nested_set_options[:parent_column]} = 0)", :order => "#{acts_as_nested_set_options[:left_column]}") 
    9191        end 
    9292         
     93        # Returns all items which have children across all trees 
     94        def parents 
     95          acts_as_nested_set_options[:class].find(:all, :conditions => "(#{acts_as_nested_set_options[:right_column]} <> #{acts_as_nested_set_options[:left_column]} + 1)", :order => "#{acts_as_nested_set_options[:left_column]}") 
     96        end 
     97         
     98        # Returns all items which do not have children across all trees 
     99        def leaves 
     100          acts_as_nested_set_options[:class].find(:all, :conditions => "(#{acts_as_nested_set_options[:right_column]} = #{acts_as_nested_set_options[:left_column]} + 1)", :order => "#{acts_as_nested_set_options[:left_column]}") 
     101        end 
     102         
    93103        # Checks the left/right indexes of all records,  
    94104        # returning the number of records checked. Throws ActiveRecord::ActiveRecordError if it finds a problem. 
    95105        def check_all 
  • README

    old new  
    9393Other instance methods added by this plugin include: 
    9494* <tt>root</tt> - root item of the tree (the one that has a nil parent) 
    9595* <tt>roots</tt> - root items, in case of multiple roots (the ones that have a nil parent) 
     96* <tt>parents</tt> - parent items, all items that have children 
     97* <tt>leaves</tt> - leaf items, all items that do not have children 
    9698* <tt>level</tt> - number indicating the level, a root being level 0 
    9799* <tt>ancestors</tt> - array of all parents, with root as first item 
    98100* <tt>self_and_ancestors</tt> - array of all parents and self