diff options
author | Go MAEDA <maeda@farend.jp> | 2018-04-28 03:45:23 +0000 |
---|---|---|
committer | Go MAEDA <maeda@farend.jp> | 2018-04-28 03:45:23 +0000 |
commit | ea562ffa435301a73e0838f2563a6c868799c2a0 (patch) | |
tree | 9e1965c52228289378c16f6cf2a09ae981cfbc03 | |
parent | b1781507617d9386c8545a28941fac163afd50fa (diff) | |
download | redmine-ea562ffa435301a73e0838f2563a6c868799c2a0.tar.gz redmine-ea562ffa435301a73e0838f2563a6c868799c2a0.zip |
Allow to copy documents along with projects (#26621).
Patch by Holger Just.
git-svn-id: http://svn.redmine.org/redmine/trunk@17314 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r-- | app/models/project.rb | 17 | ||||
-rw-r--r-- | app/views/projects/copy.html.erb | 1 | ||||
-rw-r--r-- | test/object_helpers.rb | 8 | ||||
-rw-r--r-- | test/unit/project_copy_test.rb | 22 |
4 files changed, 47 insertions, 1 deletions
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 @@ <label class="block"><%= check_box_tag 'only[]', 'issue_categories', true, :id => nil %> <%= l(:label_issue_category_plural) %> (<%= @source_project.issue_categories.count %>)</label> <label class="block"><%= check_box_tag 'only[]', 'issues', true, :id => nil %> <%= l(:label_issue_plural) %> (<%= @source_project.issues.count %>)</label> <label class="block"><%= check_box_tag 'only[]', 'queries', true, :id => nil %> <%= l(:label_query_plural) %> (<%= @source_project.queries.count %>)</label> + <label class="block"><%= check_box_tag 'only[]', 'documents', true, :id => nil %> <%= l(:label_document_plural) %> (<%= @source_project.documents.count %>)</label> <label class="block"><%= check_box_tag 'only[]', 'boards', true, :id => nil %> <%= l(:label_board_plural) %> (<%= @source_project.boards.count %>)</label> <label class="block"><%= check_box_tag 'only[]', 'wiki', true, :id => nil %> <%= l(:label_wiki_page_plural) %> (<%= @source_project.wiki.nil? ? 0 : @source_project.wiki.pages.count %>)</label> <%= 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) |