]> source.dussan.org Git - redmine.git/commitdiff
Project copy: let the user choose what to copy from the source project (everything...
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 24 Oct 2009 13:30:23 +0000 (13:30 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 24 Oct 2009 13:30:23 +0000 (13:30 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2966 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/projects_controller.rb
app/models/project.rb
app/views/projects/copy.rhtml
test/unit/project_test.rb

index 32326d45dfe52d9d49b382708f2d23ec570d1d72..e17c790f1fad4f09bfc459022a26496ec95ee380 100644 (file)
@@ -104,7 +104,7 @@ class ProjectsController < ApplicationController
     else
       @project = Project.new(params[:project])
       @project.enabled_module_names = params[:enabled_modules]
-      if @project.copy(params[:id])
+      if @project.copy(params[:id], :only => params[:only])
         flash[:notice] = l(:notice_successful_create)
         redirect_to :controller => 'admin', :action => 'projects'
       end              
index 668238fc880ffde3d1e9a03881e38627effcf3a7..70ec85dda4ff102cba2007983b9244ae4f871a9f 100644 (file)
@@ -391,73 +391,30 @@ class Project < ActiveRecord::Base
   end
 
   # Copies and saves the Project instance based on the +project+.
-  # Will duplicate the source project's:
+  # Duplicates the source project's:
+  # * Wiki
+  # * Versions
+  # * Categories
   # * Issues
   # * Members
   # * Queries
-  def copy(project)
+  #
+  # Accepts an +options+ argument to specify what to copy
+  #
+  # Examples:
+  #   project.copy(1)                                    # => copies everything
+  #   project.copy(1, :only => 'members')                # => copies members only
+  #   project.copy(1, :only => ['members', 'versions'])  # => copies members and versions
+  def copy(project, options={})
     project = project.is_a?(Project) ? project : Project.find(project)
-
-    Project.transaction do
-      # Wikis
-      self.wiki = Wiki.new(project.wiki.attributes.dup.except("project_id"))
-      project.wiki.pages.each do |page|
-        new_wiki_content = WikiContent.new(page.content.attributes.dup.except("page_id"))
-        new_wiki_page = WikiPage.new(page.attributes.dup.except("wiki_id"))
-        new_wiki_page.content = new_wiki_content
-
-        self.wiki.pages << new_wiki_page
-      end
-      
-      # Versions
-      project.versions.each do |version|
-        new_version = Version.new
-        new_version.attributes = version.attributes.dup.except("project_id")
-        self.versions << new_version
-      end
-
-      project.issue_categories.each do |issue_category|
-        new_issue_category = IssueCategory.new
-        new_issue_category.attributes = issue_category.attributes.dup.except("project_id")
-        self.issue_categories << new_issue_category
-      end
-      
-      # Issues
-      project.issues.each do |issue|
-        new_issue = Issue.new
-        new_issue.copy_from(issue)
-        # Reassign fixed_versions by name, since names are unique per
-        # project and the versions for self are not yet saved
-        if issue.fixed_version
-          new_issue.fixed_version = self.versions.select {|v| v.name == issue.fixed_version.name}.first
-        end
-        # Reassign the category by name, since names are unique per
-        # project and the categories for self are not yet saved
-        if issue.category
-          new_issue.category = self.issue_categories.select {|c| c.name == issue.category.name}.first
-        end
-        
-        self.issues << new_issue
-      end
     
-      # Members
-      project.members.each do |member|
-        new_member = Member.new
-        new_member.attributes = member.attributes.dup.except("project_id")
-        new_member.role_ids = member.role_ids.dup
-        new_member.project = self
-        self.members << new_member
-      end
-      
-      # Queries
-      project.queries.each do |query|
-        new_query = Query.new
-        new_query.attributes = query.attributes.dup.except("project_id", "sort_criteria")
-        new_query.sort_criteria = query.sort_criteria if query.sort_criteria
-        new_query.project = self
-        self.queries << new_query
+    to_be_copied = %w(wiki versions issue_categories issues members queries)
+    to_be_copied = to_be_copied & options[:only].to_a unless options[:only].nil?
+    
+    Project.transaction do
+      to_be_copied.each do |name|
+        send "copy_#{name}", project
       end
-
       Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
       self.save
     end
@@ -486,7 +443,78 @@ class Project < ActiveRecord::Base
     end
   end
   
-private
+  private
+  
+  # Copies wiki from +project+
+  def copy_wiki(project)
+    self.wiki = Wiki.new(project.wiki.attributes.dup.except("project_id"))
+    project.wiki.pages.each do |page|
+      new_wiki_content = WikiContent.new(page.content.attributes.dup.except("page_id"))
+      new_wiki_page = WikiPage.new(page.attributes.dup.except("wiki_id"))
+      new_wiki_page.content = new_wiki_content
+      self.wiki.pages << new_wiki_page
+    end
+  end
+
+  # Copies versions from +project+
+  def copy_versions(project)
+    project.versions.each do |version|
+      new_version = Version.new
+      new_version.attributes = version.attributes.dup.except("project_id")
+      self.versions << new_version
+    end
+  end
+
+  # Copies issue categories from +project+
+  def copy_issue_categories(project)
+    project.issue_categories.each do |issue_category|
+      new_issue_category = IssueCategory.new
+      new_issue_category.attributes = issue_category.attributes.dup.except("project_id")
+      self.issue_categories << new_issue_category
+    end
+  end
+  
+  # Copies issues from +project+
+  def copy_issues(project)
+    project.issues.each do |issue|
+      new_issue = Issue.new
+      new_issue.copy_from(issue)
+      # Reassign fixed_versions by name, since names are unique per
+      # project and the versions for self are not yet saved
+      if issue.fixed_version
+        new_issue.fixed_version = self.versions.select {|v| v.name == issue.fixed_version.name}.first
+      end
+      # Reassign the category by name, since names are unique per
+      # project and the categories for self are not yet saved
+      if issue.category
+        new_issue.category = self.issue_categories.select {|c| c.name == issue.category.name}.first
+      end
+      self.issues << new_issue
+    end
+  end
+
+  # Copies members from +project+
+  def copy_members(project)
+    project.members.each do |member|
+      new_member = Member.new
+      new_member.attributes = member.attributes.dup.except("project_id")
+      new_member.role_ids = member.role_ids.dup
+      new_member.project = self
+      self.members << new_member
+    end
+  end
+
+  # Copies queries from +project+
+  def copy_queries(project)
+    project.queries.each do |query|
+      new_query = Query.new
+      new_query.attributes = query.attributes.dup.except("project_id", "sort_criteria")
+      new_query.sort_criteria = query.sort_criteria if query.sort_criteria
+      new_query.project = self
+      self.queries << new_query
+    end
+  end
+  
   def allowed_permissions
     @allowed_permissions ||= begin
       module_names = enabled_modules.collect {|m| m.name}
index ac531e0697c1b41fa1350cc45e402a65ba975ba2..d03e4d0c99231a72a1120053c3873d1628235624 100644 (file)
 <% end %>
 </fieldset>
 
+<fieldset class="box"><legend><%= l(:button_copy) %></legend>
+  <label class="floating"><%= check_box_tag 'only[]', 'members', true %> <%= l(:label_member_plural) %></label>
+  <label class="floating"><%= check_box_tag 'only[]', 'versions', true %> <%= l(:label_version_plural) %></label>
+  <label class="floating"><%= check_box_tag 'only[]', 'issue_categories', true %> <%= l(:label_issue_category_plural) %></label>
+  <label class="floating"><%= check_box_tag 'only[]', 'issues', true %> <%= l(:label_issue_plural) %></label>
+  <label class="floating"><%= check_box_tag 'only[]', 'queries', true %> <%= l(:label_query_plural) %></label>
+  <label class="floating"><%= check_box_tag 'only[]', 'wiki', true %> <%= l(:label_wiki) %></label>
+  <%= hidden_field_tag 'only[]', '' %>
+</fieldset>
+
 <%= submit_tag l(:button_copy) %>
 <% end %>
index ad483a23cc29188b695fc127223ad6d4e66eb7f1..eafc9ea954887ff5e7c271959e633682f8a77c35 100644 (file)
@@ -499,6 +499,18 @@ class ProjectTest < ActiveSupport::TestCase
       end
     end
     
+    should "limit copy with :only option" do
+      assert @project.members.empty?
+      assert @project.issue_categories.empty?
+      assert @source_project.issues.any?
+    
+      assert @project.copy(@source_project, :only => ['members', 'issue_categories'])
+
+      assert @project.members.any?
+      assert @project.issue_categories.any?
+      assert @project.issues.empty?
+    end
+    
     should "copy issue relations"
     should "link issue relations if cross project issue relations are valid"