Index: test/unit/project_copy_test.rb
===================================================================
--- test/unit/project_copy_test.rb (revision 12626)
+++ test/unit/project_copy_test.rb (working copy)
@@ -316,6 +316,7 @@
assert @project.members.any?
assert @project.issue_categories.any?
assert @project.issues.empty?
+ assert @project.documents.empty?
end
test "#copy should copy subtasks" do
@@ -334,4 +335,34 @@
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
Index: test/object_helpers.rb
===================================================================
--- test/object_helpers.rb (revision 12626)
+++ test/object_helpers.rb (working copy)
@@ -178,4 +178,13 @@
changeset.save!
changeset
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
Index: app/models/project.rb
===================================================================
--- app/models/project.rb (revision 12626)
+++ app/models/project.rb (working copy)
@@ -709,7 +709,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
@@ -958,6 +958,20 @@
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)
Index: app/views/projects/copy.html.erb
===================================================================
--- app/views/projects/copy.html.erb (revision 12626)
+++ app/views/projects/copy.html.erb (working copy)
@@ -10,6 +10,7 @@
+
<%= hidden_field_tag 'only[]', '' %>