1
0
mirror of https://github.com/meineerde/redmine.git synced 2026-03-12 04:03:08 +00:00

Makes parent parent assignment work on project form.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/branches/work@2150 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2008-12-20 13:43:05 +00:00
parent 058e4db223
commit 8de41294c0
5 changed files with 47 additions and 19 deletions

View File

@ -63,9 +63,6 @@ class ProjectsController < ApplicationController
def add
@issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
@trackers = Tracker.all
@root_projects = Project.find(:all,
:conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
:order => 'name')
@project = Project.new(params[:project])
if request.get?
@project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
@ -75,6 +72,7 @@ class ProjectsController < ApplicationController
else
@project.enabled_module_names = params[:enabled_modules]
if @project.save
@project.set_parent!(params[:project]['parent_id']) if User.current.admin? && params[:project].has_key?('parent_id')
flash[:notice] = l(:notice_successful_create)
redirect_to :controller => 'admin', :action => 'projects'
end
@ -106,9 +104,6 @@ class ProjectsController < ApplicationController
end
def settings
@root_projects = Project.find(:all,
:conditions => ["parent_id IS NULL AND status = #{Project::STATUS_ACTIVE} AND id <> ?", @project.id],
:order => 'name')
@issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
@issue_category ||= IssueCategory.new
@member ||= @project.members.new
@ -122,6 +117,7 @@ class ProjectsController < ApplicationController
if request.post?
@project.attributes = params[:project]
if @project.save
@project.set_parent!(params[:project]['parent_id']) if User.current.admin? && params[:project].has_key?('parent_id')
flash[:notice] = l(:notice_successful_update)
redirect_to :action => 'settings', :id => @project
else

View File

@ -33,4 +33,8 @@ module ProjectsHelper
]
tabs.select {|tab| User.current.allowed_to?(tab[:action], @project)}
end
def project_hierarchy_collection_for_select(projects)
projects.sort_by(&:lft).collect {|p| [('>' * p.level) + p.name.to_s, p.id]}
end
end

View File

@ -66,6 +66,7 @@ class Project < ActiveRecord::Base
before_destroy :delete_all_members
named_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] } }
named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
def identifier=(identifier)
super unless identifier_frozen?
@ -177,6 +178,31 @@ class Project < ActiveRecord::Base
update_attribute :status, STATUS_ACTIVE
end
def possible_parents
@possible_parents ||= (Project.active.find(:all) - self_and_descendants)
end
# Sets the parent of the project
# Argument can be either a Project, a String, a Fixnum or nil
def set_parent!(p)
unless p.nil? || p.is_a?(Project)
if p.to_s.blank?
p = nil
else
p = Project.find_by_id(p)
return false unless p
end
end
if p == parent
true
elsif p.nil? || (p.active? && move_possible?(p))
move_to_child_of(p)
true
else
false
end
end
def active_children
children.select {|child| child.active?}
end
@ -257,8 +283,8 @@ class Project < ActiveRecord::Base
protected
def validate
errors.add(parent_id, " must be a root project") if parent and parent.parent
errors.add_to_base("A project with subprojects can't be a subproject") if parent and children.size > 0
#errors.add(parent_id, " must be a root project") if parent and parent.parent
#errors.add_to_base("A project with subprojects can't be a subproject") if parent and children.size > 0
errors.add(:identifier, :activerecord_error_invalid) if !identifier.blank? && identifier.match(/^\d*$/)
end

View File

@ -4,8 +4,8 @@
<!--[form:project]-->
<p><%= f.text_field :name, :required => true %><br /><em><%= l(:text_caracters_maximum, 30) %></em></p>
<% if User.current.admin? and !@root_projects.empty? %>
<p><%= f.select :parent_id, (@root_projects.collect {|p| [p.name, p.id]}), { :include_blank => true } %></p>
<% if User.current.admin? && !@project.possible_parents.empty? %>
<p><%= f.select :parent_id, project_hierarchy_collection_for_select(@project.possible_parents), { :include_blank => true } %></p>
<% end %>
<p><%= f.text_area :description, :rows => 5, :class => 'wiki-edit' %></p>

View File

@ -95,25 +95,27 @@ class ProjectTest < Test::Unit::TestCase
assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty?
end
def test_subproject_ok
def test_move_an_orphan_project_to_a_root_project
sub = Project.find(2)
sub.parent = @ecookbook
assert sub.save
sub.set_parent! @ecookbook
assert_equal @ecookbook.id, sub.parent.id
@ecookbook.reload
assert_equal 4, @ecookbook.children.size
end
def test_subproject_invalid
def test_move_an_orphan_project_to_a_subproject
sub = Project.find(2)
sub.parent = @ecookbook_sub1
assert !sub.save
assert sub.set_parent!(@ecookbook_sub1)
end
def test_subproject_invalid_2
def test_move_a_root_project_to_a_project
sub = @ecookbook
sub.parent = Project.find(2)
assert !sub.save
assert sub.set_parent!(Project.find(2))
end
def test_should_not_move_a_project_to_its_children
sub = @ecookbook
assert !(sub.set_parent!(Project.find(3)))
end
def test_rolled_up_trackers