Project

General

Profile

Feature #5664 » feature_5664_copy_docs_w_tests_trunk.diff

Michael Esemplare, 2014-01-11 09:21

View differences:

test/unit/project_copy_test.rb (working copy)
316 316
    assert @project.members.any?
317 317
    assert @project.issue_categories.any?
318 318
    assert @project.issues.empty?
319
    assert @project.documents.empty?
319 320
  end
320 321

  
321 322
  test "#copy should copy subtasks" do
......
334 335
    child_copy = copy.children.detect {|c| c.subject == 'Child1'}
335 336
    assert child_copy.descendants.any?
336 337
  end
338

  
339
  test "#copy should copy documents" do
340
    @source_project.documents << Document.generate!(:title => "copy document title",
341
                                              :category_id => 1,
342
                                              :project_id => @source_project.id)
343
    assert @project.valid?
344
    assert @project.documents.empty?
345
    assert @project.copy(@source_project)
346

  
347
    assert_equal @source_project.documents.size, @project.documents.size
348
    @project.documents.each do |document|
349
      assert document.valid?
350
      assert_equal @project, document.project
351
    end
352

  
353
    copied_document = @project.documents.where(:title => "copy document title").first
354
    assert copied_document
355
  end
356

  
357
  test "#copy should copy document attachments" do
358
    document = Document.generate!(:title => "copy document attachment", :category_id => 1, :project_id => @source_project.id)
359
    Attachment.create!(:container => document, :file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 1)
360
    @source_project.documents << document
361
    assert @project.copy(@source_project)
362

  
363
    copied_document = @project.documents.where(:title => "copy document attachment").first
364
    assert_not_nil copied_document
365
    assert_equal 1, copied_document.attachments.count, "Attachment not copied"
366
    assert_equal "testfile.txt", copied_document.attachments.first.filename
367
  end
337 368
end
test/object_helpers.rb (working copy)
178 178
    changeset.save!
179 179
    changeset
180 180
  end
181

  
182
  def Document.generate!(attributes={})
183
    document = Document.new(attributes)
184
    document.project ||= Project.find(1)
185
    document.category ||= document.project.categories.first
186
    document.title = 'Generated' if document.title.blank?
187
    yield document if block_given?
188
    document
189
  end
181 190
end
app/models/project.rb (working copy)
709 709
  def copy(project, options={})
710 710
    project = project.is_a?(Project) ? project : Project.find(project)
711 711

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

  
715 715
    Project.transaction do
......
958 958
    end
959 959
  end
960 960

  
961
  # Copies documents from +project+
962
  def copy_documents(project)
963
    project.documents.each do |document|
964
      new_document = Document.new
965
      new_document.attributes = document.attributes.dup.except("id", "project_id")
966
      new_document.project = self
967
      self.documents << new_document
968
      # Copy attachments to document
969
      new_document.attachments = document.attachments.map do |attachement|
970
        attachement.copy(:container => new_document)
971
      end
972
    end
973
  end
974

  
961 975
  def allowed_permissions
962 976
    @allowed_permissions ||= begin
963 977
      module_names = enabled_modules.loaded? ? enabled_modules.map(&:name) : enabled_modules.pluck(:name)
app/views/projects/copy.html.erb (working copy)
10 10
  <label class="block"><%= check_box_tag 'only[]', 'issues', true %> <%= l(:label_issue_plural) %> (<%= @source_project.issues.count %>)</label>
11 11
  <label class="block"><%= check_box_tag 'only[]', 'queries', true %> <%= l(:label_query_plural) %> (<%= @source_project.queries.count %>)</label>
12 12
  <label class="block"><%= check_box_tag 'only[]', 'boards', true %> <%= l(:label_board_plural) %> (<%= @source_project.boards.count %>)</label>
13
  <label class="block"><%= check_box_tag 'only[]', 'documents', true %> <%= l(:label_document_plural) %> (<%= @source_project.documents.count %>)</label>
13 14
  <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>
14 15
  <%= hidden_field_tag 'only[]', '' %>
15 16
  <br />
(2-2/3)