Ticket #37: leaves_and_parents_class_methods.patch
| File leaves_and_parents_class_methods.patch, 2.8 kB (added by rails, 1 year ago) |
|---|
-
test/acts_as_nested_set_test.rb
old new 87 87 assert_equal(10, NestedSet.roots.size) # May change if STI behavior changes 88 88 end 89 89 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 90 102 def test_check_all_1 91 103 assert_nothing_raised {NestedSetWithStringScope.check_all} 92 104 NestedSetWithStringScope.update_all("lft = 3", "id = 103") -
lib/better_nested_set.rb
old new 90 90 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]}") 91 91 end 92 92 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 93 103 # Checks the left/right indexes of all records, 94 104 # returning the number of records checked. Throws ActiveRecord::ActiveRecordError if it finds a problem. 95 105 def check_all -
README
old new 93 93 Other instance methods added by this plugin include: 94 94 * <tt>root</tt> - root item of the tree (the one that has a nil parent) 95 95 * <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 96 98 * <tt>level</tt> - number indicating the level, a root being level 0 97 99 * <tt>ancestors</tt> - array of all parents, with root as first item 98 100 * <tt>self_and_ancestors</tt> - array of all parents and self
