Adds :order option to acts_as_nested_tree so that children are sorted alphabetically when the tree is rebuilt.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/branches/work@2156 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang
2008-12-20 17:58:55 +00:00
parent 01ab34d401
commit efff5dff9b
3 changed files with 20 additions and 3 deletions

View File

@@ -43,7 +43,7 @@ class Project < ActiveRecord::Base
:join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
:association_foreign_key => 'custom_field_id'
acts_as_nested_set :dependent => :nullify
acts_as_nested_set :order => 'name', :dependent => :nullify
acts_as_attachable :view_permission => :view_files,
:delete_permission => :manage_files

View File

@@ -118,6 +118,22 @@ class ProjectTest < Test::Unit::TestCase
assert !(sub.set_parent!(Project.find(3)))
end
def test_rebuild_should_sort_children_alphabetically
ProjectCustomField.delete_all
parent = Project.create!(:name => 'Parent', :identifier => 'parent')
Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent)
Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent)
Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent)
Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent)
Project.update_all("lft = NULL, rgt = NULL")
Project.rebuild!
parent.reload
assert_equal 4, parent.children.size
assert_equal parent.children.sort_by(&:name), parent.children
end
def test_rolled_up_trackers
parent = Project.find(1)
parent.trackers = Tracker.find([1,2])

View File

@@ -56,6 +56,7 @@ module CollectiveIdea #:nodoc:
:parent_column => 'parent_id',
:left_column => 'lft',
:right_column => 'rgt',
:order => 'id',
:dependent => :delete_all, # or :destroy
}.merge(options)
@@ -179,14 +180,14 @@ module CollectiveIdea #:nodoc:
# set left
node[left_column_name] = indices[scope.call(node)] += 1
# find
find(:all, :conditions => ["#{quoted_parent_column_name} = ? #{scope.call(node)}", node], :order => "#{quoted_left_column_name}, #{quoted_right_column_name}, id").each{|n| set_left_and_rights.call(n) }
find(:all, :conditions => ["#{quoted_parent_column_name} = ? #{scope.call(node)}", node], :order => "#{quoted_left_column_name}, #{quoted_right_column_name}, #{acts_as_nested_set_options[:order]}").each{|n| set_left_and_rights.call(n) }
# set right
node[right_column_name] = indices[scope.call(node)] += 1
node.save!
end
# Find root node(s)
root_nodes = find(:all, :conditions => "#{quoted_parent_column_name} IS NULL", :order => "#{quoted_left_column_name}, #{quoted_right_column_name}, id").each do |root_node|
root_nodes = find(:all, :conditions => "#{quoted_parent_column_name} IS NULL", :order => "#{quoted_left_column_name}, #{quoted_right_column_name}, #{acts_as_nested_set_options[:order]}").each do |root_node|
# setup index for this scope
indices[scope.call(root_node)] ||= 0
set_left_and_rights.call(root_node)