Fixed that projects are not ordered alphabetically after renaming project (#11508).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10187 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang
2012-08-10 16:50:15 +00:00
parent 02b2a61e15
commit e52219f09d
2 changed files with 158 additions and 99 deletions

View File

@@ -81,6 +81,7 @@ class Project < ActiveRecord::Base
# reserved words
validates_exclusion_of :identifier, :in => %w( new )
after_save :update_position_under_parent, :if => Proc.new {|project| project.name_changed?}
before_destroy :delete_all_members
scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } }
@@ -383,22 +384,7 @@ class Project < ActiveRecord::Base
# Nothing to do
true
elsif p.nil? || (p.active? && move_possible?(p))
# Insert the project so that target's children or root projects stay alphabetically sorted
sibs = (p.nil? ? self.class.roots : p.children)
to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
if to_be_inserted_before
move_to_left_of(to_be_inserted_before)
elsif p.nil?
if sibs.empty?
# move_to_root adds the project in first (ie. left) position
move_to_root
else
move_to_right_of(sibs.last) unless self == sibs.last
end
else
# move_to_child_of adds the project in last (ie.right) position
move_to_child_of(p)
end
set_or_update_position_under(p)
Issue.update_versions_from_hierarchy_change(self)
true
else
@@ -943,4 +929,28 @@ class Project < ActiveRecord::Base
end
update_attribute :status, STATUS_ARCHIVED
end
def update_position_under_parent
set_or_update_position_under(parent)
end
# Inserts/moves the project so that target's children or root projects stay alphabetically sorted
def set_or_update_position_under(target_parent)
sibs = (target_parent.nil? ? self.class.roots : target_parent.children)
to_be_inserted_before = sibs.sort_by {|c| c.name.to_s.downcase}.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
if to_be_inserted_before
move_to_left_of(to_be_inserted_before)
elsif target_parent.nil?
if sibs.empty?
# move_to_root adds the project in first (ie. left) position
move_to_root
else
move_to_right_of(sibs.last) unless self == sibs.last
end
else
# move_to_child_of adds the project in last (ie.right) position
move_to_child_of(target_parent)
end
end
end