diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2008-12-20 13:43:05 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2008-12-20 13:43:05 +0000 |
commit | 8de41294c0ecd562f9a8f42891409dff6711ca6c (patch) | |
tree | c95f55d09ef2ec894c316e8e7db558143797f55c | |
parent | 058e4db223153bbf0a0abbfae8ab804a79b7105b (diff) | |
download | redmine-8de41294c0ecd562f9a8f42891409dff6711ca6c.tar.gz redmine-8de41294c0ecd562f9a8f42891409dff6711ca6c.zip |
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
-rw-r--r-- | nested_projects/app/controllers/projects_controller.rb | 8 | ||||
-rw-r--r-- | nested_projects/app/helpers/projects_helper.rb | 4 | ||||
-rw-r--r-- | nested_projects/app/models/project.rb | 30 | ||||
-rw-r--r-- | nested_projects/app/views/projects/_form.rhtml | 4 | ||||
-rw-r--r-- | nested_projects/test/unit/project_test.rb | 20 |
5 files changed, 47 insertions, 19 deletions
diff --git a/nested_projects/app/controllers/projects_controller.rb b/nested_projects/app/controllers/projects_controller.rb index baefdef89..18735445d 100644 --- a/nested_projects/app/controllers/projects_controller.rb +++ b/nested_projects/app/controllers/projects_controller.rb @@ -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 diff --git a/nested_projects/app/helpers/projects_helper.rb b/nested_projects/app/helpers/projects_helper.rb index 6f5e03349..c4a83f277 100644 --- a/nested_projects/app/helpers/projects_helper.rb +++ b/nested_projects/app/helpers/projects_helper.rb @@ -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 diff --git a/nested_projects/app/models/project.rb b/nested_projects/app/models/project.rb index 11a4a2a33..5db7ffca2 100644 --- a/nested_projects/app/models/project.rb +++ b/nested_projects/app/models/project.rb @@ -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 diff --git a/nested_projects/app/views/projects/_form.rhtml b/nested_projects/app/views/projects/_form.rhtml index 860b19fe4..41f9b1859 100644 --- a/nested_projects/app/views/projects/_form.rhtml +++ b/nested_projects/app/views/projects/_form.rhtml @@ -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> diff --git a/nested_projects/test/unit/project_test.rb b/nested_projects/test/unit/project_test.rb index 6e32c02e7..3821df698 100644 --- a/nested_projects/test/unit/project_test.rb +++ b/nested_projects/test/unit/project_test.rb @@ -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
|