From 27e72a260c01fb41d59f9ea10d125f721b9fdc46 Mon Sep 17 00:00:00 2001 From: Michael Esemplare Date: Thu, 14 Aug 2014 19:29:00 -0700 Subject: Adds ability to copy documents when copying a project (#5664) --- app/models/project.rb | 16 +++++++++++++++- app/views/projects/copy.html.erb | 1 + test/object_helpers.rb | 9 +++++++++ test/unit/project_copy_test.rb | 31 +++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index 96de855..f280ce0 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -717,7 +717,7 @@ class Project < ActiveRecord::Base 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 @@ -967,6 +967,20 @@ class Project < ActiveRecord::Base 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 + new_document.attachments = document.attachments.map do |attachement| + attachement.copy(:container => new_document) + end + end + end + def allowed_permissions @allowed_permissions ||= begin module_names = enabled_modules.loaded? ? enabled_modules.map(&:name) : enabled_modules.pluck(:name) diff --git a/app/views/projects/copy.html.erb b/app/views/projects/copy.html.erb index 26ec2e5..ce68ba0 100644 --- a/app/views/projects/copy.html.erb +++ b/app/views/projects/copy.html.erb @@ -10,6 +10,7 @@ + <%= hidden_field_tag 'only[]', '' %>
diff --git a/test/object_helpers.rb b/test/object_helpers.rb index 4e4a9fb..766537e 100644 --- a/test/object_helpers.rb +++ b/test/object_helpers.rb @@ -186,6 +186,15 @@ module ObjectHelpers query.save! query end + + def Document.generate!(attributes={}) + document = Document.new(attributes) + document.project ||= Project.find(1) + document.category ||= document.project.categories.first + document.title = 'Generated' if document.title.blank? + yield document if block_given? + document + end end module IssueObjectHelpers diff --git a/test/unit/project_copy_test.rb b/test/unit/project_copy_test.rb index 2f1f6ee..80b53d3 100644 --- a/test/unit/project_copy_test.rb +++ b/test/unit/project_copy_test.rb @@ -327,6 +327,7 @@ class ProjectCopyTest < ActiveSupport::TestCase assert @project.members.any? assert @project.issue_categories.any? assert @project.issues.empty? + assert @project.documents.empty? end test "#copy should copy subtasks" do @@ -345,4 +346,34 @@ class ProjectCopyTest < ActiveSupport::TestCase child_copy = copy.children.detect {|c| c.subject == 'Child1'} assert child_copy.descendants.any? end + + test "#copy should copy documents" do + @source_project.documents << Document.generate!(:title => "copy document title", + :category_id => 1, + :project_id => @source_project.id) + assert @project.valid? + assert @project.documents.empty? + assert @project.copy(@source_project) + + assert_equal @source_project.documents.size, @project.documents.size + @project.documents.each do |document| + assert document.valid? + assert_equal @project, document.project + end + + copied_document = @project.documents.where(:title => "copy document title").first + assert copied_document + end + + test "#copy should copy document attachments" do + document = Document.generate!(:title => "copy document attachment", :category_id => 1, :project_id => @source_project.id) + Attachment.create!(:container => document, :file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 1) + @source_project.documents << document + assert @project.copy(@source_project) + + copied_document = @project.documents.where(:title => "copy document attachment").first + assert_not_nil copied_document + assert_equal 1, copied_document.attachments.count, "Attachment not copied" + assert_equal "testfile.txt", copied_document.attachments.first.filename + end end -- 1.7.9