From 4fd7e5f542935c9e5449a599abe52a6d6e48a28f Mon Sep 17 00:00:00 2001 From: Holger Just Date: Thu, 3 Aug 2017 18:03:26 +0200 Subject: [PATCH] Allow to copy documents along with a project --- app/models/project.rb | 17 ++++++++++++++++- app/views/projects/copy.html.erb | 1 + test/object_helpers.rb | 8 ++++++++ test/unit/project_copy_test.rb | 22 ++++++++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/app/models/project.rb b/app/models/project.rb index dc1709d84..06660adc4 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -821,7 +821,7 @@ class Project < ActiveRecord::Base def copy(project, options={}) project = project.is_a?(Project) ? project : Project.find(project) - to_be_copied = %w(members wiki versions issue_categories issues queries boards) + to_be_copied = %w(members wiki versions issue_categories issues queries boards documents) to_be_copied = to_be_copied & Array.wrap(options[:only]) unless options[:only].nil? Project.transaction do @@ -1102,6 +1102,21 @@ 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 + + new_document.attachments = document.attachments.map do |attachement| + attachement.copy(:container => new_document) + end + + self.documents << new_document + 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 021288e7f..dbec31eda 100644 --- a/app/views/projects/copy.html.erb +++ b/app/views/projects/copy.html.erb @@ -9,6 +9,7 @@ + <%= hidden_field_tag 'only[]', '' %> diff --git a/test/object_helpers.rb b/test/object_helpers.rb index ce38356a9..b7d2eac93 100644 --- a/test/object_helpers.rb +++ b/test/object_helpers.rb @@ -227,6 +227,14 @@ module ObjectHelpers query end + def Document.generate!(attributes={}) + document = new(attributes) + document.title = "Generated document" if document.title.blank? + document.category ||= DocumentCategory.find(1) + document.save! + document + end + def generate_import(fixture_name='import_issues.csv') import = IssueImport.new import.user_id = 2 diff --git a/test/unit/project_copy_test.rb b/test/unit/project_copy_test.rb index 0a819b78c..a1292204b 100644 --- a/test/unit/project_copy_test.rb +++ b/test/unit/project_copy_test.rb @@ -341,6 +341,28 @@ class ProjectCopyTest < ActiveSupport::TestCase end end + test "#copy should copy documents" do + source_project = Project.find(1) + assert @project.copy(source_project) + + assert_equal 2, @project.documents.size + @project.documents.each do |document| + assert !source_project.documents.include?(document) + end + end + + test "#copy should copy document attachments" do + document = Document.generate!(:title => "copy with 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 with 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 + test "#copy should change the new issues to use the copied issue categories" do issue = Issue.find(4) issue.update_attribute(:category_id, 3) -- 2.13.0