0001-Allow-to-copy-documents-along-with-a-project.patch

Holger Just, 2017-08-03 18:45

Download (4.91 KB)

View differences:

app/models/project.rb
821 821
  def copy(project, options={})
822 822
    project = project.is_a?(Project) ? project : Project.find(project)
823 823

  
824
    to_be_copied = %w(members wiki versions issue_categories issues queries boards)
824
    to_be_copied = %w(members wiki versions issue_categories issues queries boards documents)
825 825
    to_be_copied = to_be_copied & Array.wrap(options[:only]) unless options[:only].nil?
826 826

  
827 827
    Project.transaction do
......
1102 1102
    end
1103 1103
  end
1104 1104

  
1105
  # Copies documents from +project+
1106
  def copy_documents(project)
1107
    project.documents.each do |document|
1108
      new_document = Document.new
1109
      new_document.attributes = document.attributes.dup.except("id", "project_id")
1110
      new_document.project = self
1111

  
1112
      new_document.attachments = document.attachments.map do |attachement|
1113
        attachement.copy(:container => new_document)
1114
      end
1115

  
1116
      self.documents << new_document
1117
    end
1118
  end
1119

  
1105 1120
  def allowed_permissions
1106 1121
    @allowed_permissions ||= begin
1107 1122
      module_names = enabled_modules.loaded? ? enabled_modules.map(&:name) : enabled_modules.pluck(:name)
app/views/projects/copy.html.erb
9 9
  <label class="block"><%= check_box_tag 'only[]', 'issue_categories', true, :id => nil %> <%= l(:label_issue_category_plural) %> (<%= @source_project.issue_categories.count %>)</label>
10 10
  <label class="block"><%= check_box_tag 'only[]', 'issues', true, :id => nil %> <%= l(:label_issue_plural) %> (<%= @source_project.issues.count %>)</label>
11 11
  <label class="block"><%= check_box_tag 'only[]', 'queries', true, :id => nil %> <%= l(:label_query_plural) %> (<%= @source_project.queries.count %>)</label>
12
  <label class="block"><%= check_box_tag 'only[]', 'documents', true, :id => nil %> <%= l(:label_document_plural) %> (<%= @source_project.documents.count %>)</label>
12 13
  <label class="block"><%= check_box_tag 'only[]', 'boards', true, :id => nil %> <%= l(:label_board_plural) %> (<%= @source_project.boards.count %>)</label>
13 14
  <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>
14 15
  <%= hidden_field_tag 'only[]', '' %>
test/object_helpers.rb
227 227
    query
228 228
  end
229 229

  
230
  def Document.generate!(attributes={})
231
    document = new(attributes)
232
    document.title = "Generated document" if document.title.blank?
233
    document.category ||= DocumentCategory.find(1)
234
    document.save!
235
    document
236
  end
237

  
230 238
  def generate_import(fixture_name='import_issues.csv')
231 239
    import = IssueImport.new
232 240
    import.user_id = 2
test/unit/project_copy_test.rb
341 341
    end
342 342
  end
343 343

  
344
  test "#copy should copy documents" do
345
    source_project = Project.find(1)
346
    assert @project.copy(source_project)
347

  
348
    assert_equal 2, @project.documents.size
349
    @project.documents.each do |document|
350
      assert !source_project.documents.include?(document)
351
    end
352
  end
353

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

  
360
    copied_document = @project.documents.where(:title => "copy with attachment").first
361
    assert_not_nil copied_document
362
    assert_equal 1, copied_document.attachments.count, "Attachment not copied"
363
    assert_equal "testfile.txt", copied_document.attachments.first.filename
364
  end
365

  
344 366
  test "#copy should change the new issues to use the copied issue categories" do
345 367
    issue = Issue.find(4)
346 368
    issue.update_attribute(:category_id, 3)
347
-