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 committed 2008-12-20 13:43:05 +00:00
1 parent 058e4db223
commit 8de41294c0
5 files changed
+47 -19

No files matched your search

@@ -63,9 +63,6 @@ class ProjectsController < ApplicationController
def add def add
@issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
@trackers = Tracker.all @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]) @project = Project.new(params[:project])
if request.get? if request.get?
@project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
@@ -75,6 +72,7 @@ class ProjectsController < ApplicationController
else else
@project.enabled_module_names = params[:enabled_modules] @project.enabled_module_names = params[:enabled_modules]
if @project.save 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) flash[:notice] = l(:notice_successful_create)
redirect_to :controller => 'admin', :action => 'projects' redirect_to :controller => 'admin', :action => 'projects'
end end
@@ -106,9 +104,6 @@ class ProjectsController < ApplicationController
end end
def settings 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_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
@issue_category ||= IssueCategory.new @issue_category ||= IssueCategory.new
@member ||= @project.members.new @member ||= @project.members.new
@@ -122,6 +117,7 @@ class ProjectsController < ApplicationController
if request.post? if request.post?
@project.attributes = params[:project] @project.attributes = params[:project]
if @project.save 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) flash[:notice] = l(:notice_successful_update)
redirect_to :action => 'settings', :id => @project redirect_to :action => 'settings', :id => @project
else else
@@ -33,4 +33,8 @@ module ProjectsHelper
] ]
tabs.select {|tab| User.current.allowed_to?(tab[:action], @project)} tabs.select {|tab| User.current.allowed_to?(tab[:action], @project)}
end end
def project_hierarchy_collection_for_select(projects)
projects.sort_by(&:lft).collect {|p| [('>' * p.level) + p.name.to_s, p.id]}
end
end end
+28 -2
View File
@@ -66,6 +66,7 @@ class Project < ActiveRecord::Base
before_destroy :delete_all_members 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 :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) def identifier=(identifier)
super unless identifier_frozen? super unless identifier_frozen?
@@ -177,6 +178,31 @@ class Project < ActiveRecord::Base
update_attribute :status, STATUS_ACTIVE update_attribute :status, STATUS_ACTIVE
end 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 def active_children
children.select {|child| child.active?} children.select {|child| child.active?}
end end
@@ -257,8 +283,8 @@ class Project < ActiveRecord::Base
protected protected
def validate def validate
errors.add(parent_id, " must be a root project") if parent and parent.parent #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_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*$/) errors.add(:identifier, :activerecord_error_invalid) if !identifier.blank? && identifier.match(/^\d*$/)
end end
@@ -4,8 +4,8 @@
<!--[form:project]--> <!--[form:project]-->
<p><%= f.text_field :name, :required => true %><br /><em><%= l(:text_caracters_maximum, 30) %></em></p> <p><%= f.text_field :name, :required => true %><br /><em><%= l(:text_caracters_maximum, 30) %></em></p>
<% if User.current.admin? and !@root_projects.empty? %> <% if User.current.admin? && !@project.possible_parents.empty? %>
<p><%= f.select :parent_id, (@root_projects.collect {|p| [p.name, p.id]}), { :include_blank => true } %></p> <p><%= f.select :parent_id, project_hierarchy_collection_for_select(@project.possible_parents), { :include_blank => true } %></p>
<% end %> <% end %>
<p><%= f.text_area :description, :rows => 5, :class => 'wiki-edit' %></p> <p><%= f.text_area :description, :rows => 5, :class => 'wiki-edit' %></p>
+11 -9
View File
@@ -95,25 +95,27 @@ class ProjectTest < Test::Unit::TestCase
assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty? assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty?
end end
def test_subproject_ok def test_move_an_orphan_project_to_a_root_project
sub = Project.find(2) sub = Project.find(2)
sub.parent = @ecookbook sub.set_parent! @ecookbook
assert sub.save
assert_equal @ecookbook.id, sub.parent.id assert_equal @ecookbook.id, sub.parent.id
@ecookbook.reload @ecookbook.reload
assert_equal 4, @ecookbook.children.size assert_equal 4, @ecookbook.children.size
end end
def test_subproject_invalid def test_move_an_orphan_project_to_a_subproject
sub = Project.find(2) sub = Project.find(2)
sub.parent = @ecookbook_sub1 assert sub.set_parent!(@ecookbook_sub1)
assert !sub.save
end end
def test_subproject_invalid_2 def test_move_a_root_project_to_a_project
sub = @ecookbook sub = @ecookbook
sub.parent = Project.find(2) assert sub.set_parent!(Project.find(2))
assert !sub.save end
def test_should_not_move_a_project_to_its_children
sub = @ecookbook
assert !(sub.set_parent!(Project.find(3)))
end end
def test_rolled_up_trackers def test_rolled_up_trackers