From b2681cca2e38b7465cd94d51dde14e53b724d0d1 Mon Sep 17 00:00:00 2001
From: Jeremy Kemper <jeremy@bitsweat.net>
Date: Wed, 30 May 2007 06:34:25 +0000
Subject: acts_as_nested_set: direct_children is sorted correctly. Closes
 #4761.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6903 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
---
 activerecord/CHANGELOG                            |  2 ++
 activerecord/lib/active_record/acts/nested_set.rb |  2 +-
 activerecord/test/fixtures/mixins.yml             |  4 ++--
 activerecord/test/mixin_nested_set_test.rb        | 15 ++++++++-------
 4 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 945c2238df..8d12f3ab49 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
 *SVN*
 
+* acts_as_nested_set: direct_children is sorted correctly.  #4761 [Josh Peek, rails@33lc0.net]
+
 * Raise an exception if both attr_protected and attr_accessible are declared.  #8507 [stellsmi]
 
 * SQLite, MySQL, PostgreSQL, Oracle: quote column names in column migration SQL statements.  #8466 [marclove, lorenjohnson]
diff --git a/activerecord/lib/active_record/acts/nested_set.rb b/activerecord/lib/active_record/acts/nested_set.rb
index 09bf1c39af..37d79d5f0c 100644
--- a/activerecord/lib/active_record/acts/nested_set.rb
+++ b/activerecord/lib/active_record/acts/nested_set.rb
@@ -190,7 +190,7 @@ module ActiveRecord
                                   
         # Returns a set of only this entry's immediate children
         def direct_children
-          self.class.base_class.find(:all, :conditions => "#{scope_condition} and #{parent_column} = #{self.id}")
+          self.class.base_class.find(:all, :conditions => "#{scope_condition} and #{parent_column} = #{self.id}", :order => left_col_name)
         end
                                       
         # Prunes a branch off of the tree, shifting all of the elements on the right
diff --git a/activerecord/test/fixtures/mixins.yml b/activerecord/test/fixtures/mixins.yml
index 881b97c1d2..1f5d3ba60b 100644
--- a/activerecord/test/fixtures/mixins.yml
+++ b/activerecord/test/fixtures/mixins.yml
@@ -101,10 +101,10 @@ sti_set_<%= sti[0] %>:
   [4002, 4001, 2, 7],
   [4003, 4002, 3, 4],
   [4004, 4002, 5, 6],
-  [4005, 4001, 8, 13],
+  [4005, 4001, 14, 13],
   [4006, 4005, 9, 10],
   [4007, 4005, 11, 12],
-  [4008, 4001, 14, 19],
+  [4008, 4001, 8, 19],
   [4009, 4008, 15, 16],
   [4010, 4008, 17, 18]].each do |set| %>
 tree_<%= set[0] %>:
diff --git a/activerecord/test/mixin_nested_set_test.rb b/activerecord/test/mixin_nested_set_test.rb
index 7ed9075bda..87fa140dc9 100644
--- a/activerecord/test/mixin_nested_set_test.rb
+++ b/activerecord/test/mixin_nested_set_test.rb
@@ -116,12 +116,13 @@ class MixinNestedSetTest < Test::Unit::TestCase
     # Make sure we have the right one
     assert_equal( 3, big_tree.direct_children.length )
     assert_equal( 10, big_tree.full_set.length )
+    assert_equal [4002, 4008, 4005], big_tree.direct_children.map(&:id)
     
     NestedSetWithStringScope.find( 4005 ).destroy
 
     big_tree = NestedSetWithStringScope.find( 4001 )
     
-    assert_equal( 7, big_tree.full_set.length )
+    assert_equal( 9, big_tree.full_set.length )
     assert_equal( 2, big_tree.direct_children.length )
     
     assert_equal( 1, NestedSetWithStringScope.find(4001).lft )
@@ -132,12 +133,12 @@ class MixinNestedSetTest < Test::Unit::TestCase
     assert_equal( 6, NestedSetWithStringScope.find(4004).rgt )
     assert_equal( 7, NestedSetWithStringScope.find(4002).rgt )
     assert_equal( 8, NestedSetWithStringScope.find(4008).lft )
-    assert_equal( 9, NestedSetWithStringScope.find(4009).lft )
-    assert_equal(10, NestedSetWithStringScope.find(4009).rgt )
-    assert_equal(11, NestedSetWithStringScope.find(4010).lft )
-    assert_equal(12, NestedSetWithStringScope.find(4010).rgt )
-    assert_equal(13, NestedSetWithStringScope.find(4008).rgt )
-    assert_equal(14, NestedSetWithStringScope.find(4001).rgt )
+    assert_equal(15, NestedSetWithStringScope.find(4009).lft )
+    assert_equal(16, NestedSetWithStringScope.find(4009).rgt )
+    assert_equal(17, NestedSetWithStringScope.find(4010).lft )
+    assert_equal(18, NestedSetWithStringScope.find(4010).rgt )
+    assert_equal(19, NestedSetWithStringScope.find(4008).rgt )
+    assert_equal(20, NestedSetWithStringScope.find(4001).rgt )
   end
   
   def test_deleting_root
-- 
cgit v1.2.3