Project

General

Profile

Feature #34258 ยป 34258-copy-tracker.patch

Takenori TAKAKI, 2020-11-20 09:06

View differences:

app/controllers/trackers_controller.rb
36 36
  def new
37 37
    @tracker ||= Tracker.new(:default_status => IssueStatus.sorted.first)
38 38
    @tracker.safe_attributes = params[:tracker]
39
    if params[:copy].present? && @copy_from = Tracker.find_by_id(params[:copy])
40
      @tracker.copy_from(@copy_from)
41
    end
39 42
    @trackers = Tracker.sorted.to_a
40 43
    @projects = Project.all
41 44
  end
app/models/tracker.rb
76 76
    'project_ids',
77 77
    'description')
78 78

  
79
  def copy_from(arg, options={})
80
    return unless arg.present?
81

  
82
    tracker = arg.is_a?(Tracker) ? arg : Tracker.find_by_id(arg.to_s)
83
    self.attributes = tracker.attributes.dup.except("id", "name", "position")
84
    self.custom_field_ids = tracker.custom_field_ids.dup
85
    self.project_ids = tracker.project_ids.dup
86
    self
87
  end
88

  
79 89
  def to_s; name end
80 90

  
81 91
  def <=>(tracker)
app/views/trackers/_form.html.erb
39 39

  
40 40
<% if @tracker.new_record? && @trackers.any? %>
41 41
<p><label for="copy_workflow_from"><%= l(:label_copy_workflow_from) %></label>
42
<%= select_tag(:copy_workflow_from, content_tag("option") + options_from_collection_for_select(@trackers, :id, :name)) %></p>
42
<%= select_tag(:copy_workflow_from, content_tag("option") + options_from_collection_for_select(@trackers, :id, :name, @copy_from.try(:id))) %></p>
43 43
<% end %>
44 44
<!--[eoform:tracker]-->
45 45
</div>
app/views/trackers/index.html.erb
28 28
  </td>
29 29
  <td class="buttons">
30 30
    <%= reorder_handle(tracker) %>
31
    <%= link_to l(:button_copy), new_tracker_path(:copy => tracker), :class => 'icon icon-copy' %>
31 32
    <%= delete_link tracker_path(tracker) %>
32 33
  </td>
33 34
  </tr>
test/functional/trackers_controller_test.rb
54 54
    end
55 55
  end
56 56

  
57
  def test_new_with_copy
58
    core_fields = ["assigned_to_id", "category_id", "fixed_version_id", "parent_issue_id", "start_date", "due_date"]
59
    custom_field_ids = custom_field_ids = [1, 2, 6]
60
    project_ids = [1, 3, 5]
61

  
62
    copy_from = Tracker.find(1)
63
    copy_from.core_fields = core_fields
64
    copy_from.custom_field_ids = custom_field_ids
65
    copy_from.project_ids = project_ids
66
    copy_from.save
67

  
68
    get :new, :params => {:copy => copy_from.id.to_s}
69
    assert_response :success
70
    assert_select 'input[name=?]', 'tracker[name]'
71

  
72
    assert_select 'form' do
73
      # blank name
74
      assert_select 'input[name=?][value=""]', 'tracker[name]'
75
      # core field checked
76
      copy_from.core_fields.each do |core_field|
77
        assert_select "input[type=checkbox][name=?][value=#{core_field}][checked=checked]", 'tracker[core_fields][]'
78
      end
79
      # core field not checked
80
      copy_from.disabled_core_fields do |core_field|
81
        assert_select "input[type=checkbox][name=?][value=#{core_field}]", 'tracker[core_fields][]'
82
      end
83
      # custom field checked
84
      custom_field_ids.each do |custom_field_id|
85
        assert_select "input[type=checkbox][name=?][value=#{custom_field_id}][checked=checked]", 'tracker[custom_field_ids][]'
86
      end
87
      # custom field not checked
88
      (IssueCustomField.sorted.pluck(:id) - custom_field_ids).each do |custom_field_id|
89
        assert_select "input[type=checkbox][name=?][value=#{custom_field_id}]", 'tracker[custom_field_ids][]'
90
      end
91
      # project checked
92
      project_ids.each do |project_id|
93
        assert_select "input[type=checkbox][name=?][value=#{project_id}][checked=checked]", 'tracker[project_ids][]'
94
      end
95
      # project not checked
96
      (Project.all.pluck(:id) - project_ids).each do |project_id|
97
        assert_select "input[type=checkbox][name=?][value=#{project_id}]", 'tracker[project_ids][]'
98
      end
99
      # workflow copy selected
100
      assert_select 'select[name=?]', 'copy_workflow_from' do
101
        assert_select 'option[value="1"][selected=selected]'
102
      end
103
    end
104
  end
105

  
57 106
  def test_create
58 107
    assert_difference 'Tracker.count' do
59 108
      post :create, :params => {
test/unit/tracker_test.rb
46 46
    assert_equal [2], project.rolled_up_trackers(false).visible(user).map(&:id)
47 47
  end
48 48

  
49
  def test_copy_from
50
    tracker = Tracker.find(1)
51
    copy = Tracker.new.copy_from(tracker)
52

  
53
    assert_nil copy.id
54
    assert_nil copy.position
55
    assert_equal '', copy.name
56
    assert_equal tracker.default_status_id, copy.default_status_id
57
    assert_equal tracker.is_in_roadmap, copy.is_in_roadmap
58
    assert_equal tracker.core_fields, copy.core_fields
59
    assert_equal tracker.description, copy.description
60

  
61
    copy.name = 'Copy'
62
    assert copy.save
63
  end
64

  
65
  def test_copy_from_should_copy_custom_fields
66
    tracker = Tracker.generate!(:custom_field_ids => [1, 2, 6])
67
    copy = Tracker.new.copy_from(tracker)
68
    assert_equal [1, 2, 6], copy.custom_field_ids.sort
69
  end
70

  
71
  def test_copy_from_should_copy_projects
72
    tracker = Tracker.generate!(:project_ids => [1, 2, 3, 4, 5, 6])
73
    copy = Tracker.new.copy_from(tracker)
74
    assert_equal [1, 2, 3, 4, 5, 6], copy.project_ids.sort
75
  end
76

  
49 77
  def test_copy_workflows
50 78
    source = Tracker.find(1)
51 79
    rules_count = source.workflow_rules.count
    (1-1/1)