Project

General

Profile

Feature #5664: Copying documents too when copying a project

Added by Michael Esemplare over 12 years ago

Hello,

I am new to these forums and well new to ruby itself. My company really needed this feature (http://www.redmine.org/issues/5664) so I patched it. I don't want to submit a patch file if I did something wrong or unsafe so I figured I would post here first and people can comment.

The version we are running is v1.0.5. There were two files patched: app.views.projects.copy.rhtml, and app.models.project.rb.

For app.views.projects.copy.rhtml, one line added:

@@ -19,6 +19,7 @@
   <label class="block"><%= check_box_tag 'only[]', 'issues', true %> <%= l(:label_issue_plural) %> (<%= @source_project.issues.count %>)</label>
   <label class="block"><%= check_box_tag 'only[]', 'queries', true %> <%= l(:label_query_plural) %> (<%= @source_project.queries.count %>)</label>
   <label class="block"><%= check_box_tag 'only[]', 'boards', true %> <%= l(:label_board_plural) %> (<%= @source_project.boards.count %>)</label>
+  <label class="block"><%= check_box_tag 'only[]', 'documents', true %> <%= l(:label_document_plural) %> (<%= @source_project.documents.count %>)</label>
   <label class="block"><%= check_box_tag 'only[]', 'wiki', true %> <%= l(:label_wiki_page_plural) %> (<%= @source_project.wiki.nil? ? 0 : @source_project.wiki.pages.count %>)</label>
   <%= hidden_field_tag 'only[]', '' %>
   <br />

For app.models.project.rb:

@@ -483,7 +483,7 @@
   def copy(project, options={})
     project = project.is_a?(Project) ? project : Project.find(project)

-    to_be_copied = %w(wiki versions issue_categories issues members queries boards)
+    to_be_copied = %w(wiki versions issue_categories issues members queries boards documents)
     to_be_copied = to_be_copied & options[:only].to_a unless options[:only].nil?

     Project.transaction do
@@ -668,6 +668,29 @@
       new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id")
       new_board.project = self
       self.boards << new_board
+    end
+  end
+  
+  # Copies documents from +project+
+  def copy_documents(project)
+    project.documents.each do |document|
+      new_document = Document.new
+      new_document.attributes = document.attributes.dup.except("id", "project_id")
+      new_document.project = self
+      self.documents << new_document
+      # Copy attachments to document
+          document.attachments.each do |attachment|
+          new_attachment = Attachment.new
+          new_attachment.attributes = attachment.attributes.dup.except("id","container_id","disk_filename")
+          #Duplicate file for attachment
+          @@storage_dir = "#{RAILS_ROOT}/files" 
+          newFilename = Attachment.disk_filename(attachment.filename)
+          oldFilePath = File.join(@@storage_dir, attachment.disk_filename)
+          newFilePath = File.join(@@storage_dir, newFilename)
+          FileUtils.copy_file(oldFilePath, newFilePath, true)
+          new_attachment.disk_filename = newFilename
+          new_document.attachments << new_attachment
+      end
     end
   end

I have attached the two files. If there are any suggestions for improving upon this, or making it suitable to be a patch please reply.

Mike.