]> source.dussan.org Git - redmine.git/commitdiff
Create tracker by copy (#34258).
authorGo MAEDA <maeda@farend.jp>
Wed, 25 Nov 2020 07:51:58 +0000 (07:51 +0000)
committerGo MAEDA <maeda@farend.jp>
Wed, 25 Nov 2020 07:51:58 +0000 (07:51 +0000)
Patch by Takenori TAKAKI.

git-svn-id: http://svn.redmine.org/redmine/trunk@20492 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/trackers_controller.rb
app/models/tracker.rb
app/views/trackers/_form.html.erb
app/views/trackers/index.html.erb
test/functional/trackers_controller_test.rb
test/unit/tracker_test.rb

index fdb5f0e377f7f10ed2a27f8bd269d5499ce33973..c69721f52055bd04c93de43b47b285b7014300e1 100644 (file)
@@ -36,6 +36,9 @@ class TrackersController < ApplicationController
   def new
     @tracker ||= Tracker.new(:default_status => IssueStatus.sorted.first)
     @tracker.safe_attributes = params[:tracker]
+    if params[:copy].present? && @copy_from = Tracker.find_by_id(params[:copy])
+      @tracker.copy_from(@copy_from)
+    end
     @trackers = Tracker.sorted.to_a
     @projects = Project.all
   end
index 7bd5c97592856152ac1c5399b01b999086f2b8ce..49c334af821cc2b92b87dcc6c1298a4f40c53441 100644 (file)
@@ -76,6 +76,16 @@ class Tracker < ActiveRecord::Base
     'project_ids',
     'description')
 
+  def copy_from(arg, options={})
+    return if arg.blank?
+
+    tracker = arg.is_a?(Tracker) ? arg : Tracker.find_by_id(arg.to_s)
+    self.attributes = tracker.attributes.dup.except("id", "name", "position")
+    self.custom_field_ids = tracker.custom_field_ids.dup
+    self.project_ids = tracker.project_ids.dup
+    self
+  end
+
   def to_s; name end
 
   def <=>(tracker)
index 27e086e568d29cdbb7401895d821679485371bcd..2ccd225e6989759b83d55fd0060bc8c8f9f0ee2e 100644 (file)
@@ -39,7 +39,7 @@
 
 <% if @tracker.new_record? && @trackers.any? %>
 <p><label for="copy_workflow_from"><%= l(:label_copy_workflow_from) %></label>
-<%= select_tag(:copy_workflow_from, content_tag("option") + options_from_collection_for_select(@trackers, :id, :name)) %></p>
+<%= select_tag(:copy_workflow_from, content_tag("option") + options_from_collection_for_select(@trackers, :id, :name, params[:copy_workflow_from] || @copy_from.try(:id))) %></p>
 <% end %>
 <!--[eoform:tracker]-->
 </div>
index 20d09754d2d7c3e5fe6e33b78f31e4d33b7bc8ec..85d3249fc4c22b5ce21d5a51e26bfcb143099e2c 100644 (file)
@@ -28,6 +28,7 @@
   </td>
   <td class="buttons">
     <%= reorder_handle(tracker) %>
+    <%= link_to l(:button_copy), new_tracker_path(:copy => tracker), :class => 'icon icon-copy' %>
     <%= delete_link tracker_path(tracker) %>
   </td>
   </tr>
index 0e036e643c7cdce01b7997c42ee7bf9d23b4e6d1..a6e9e71b134da8f2651860eab241a9f926897b99 100644 (file)
@@ -54,6 +54,55 @@ class TrackersControllerTest < Redmine::ControllerTest
     end
   end
 
+  def test_new_with_copy
+    core_fields = ['assigned_to_id', 'category_id', 'fixed_version_id', 'parent_issue_id', 'start_date', 'due_date']
+    custom_field_ids = custom_field_ids = [1, 2, 6]
+    project_ids = [1, 3, 5]
+
+    copy_from = Tracker.find(1)
+    copy_from.core_fields = core_fields
+    copy_from.custom_field_ids = custom_field_ids
+    copy_from.project_ids = project_ids
+    copy_from.save
+
+    get :new, :params => {:copy => copy_from.id.to_s}
+    assert_response :success
+    assert_select 'input[name=?]', 'tracker[name]'
+
+    assert_select 'form' do
+      # blank name
+      assert_select 'input[name=?][value=""]', 'tracker[name]'
+      # core field checked
+      copy_from.core_fields.each do |core_field|
+        assert_select "input[type=checkbox][name=?][value=#{core_field}][checked=checked]", 'tracker[core_fields][]'
+      end
+      # core field not checked
+      copy_from.disabled_core_fields do |core_field|
+        assert_select "input[type=checkbox][name=?][value=#{core_field}]", 'tracker[core_fields][]'
+      end
+      # custom field checked
+      custom_field_ids.each do |custom_field_id|
+        assert_select "input[type=checkbox][name=?][value=#{custom_field_id}][checked=checked]", 'tracker[custom_field_ids][]'
+      end
+      # custom field not checked
+      (IssueCustomField.sorted.pluck(:id) - custom_field_ids).each do |custom_field_id|
+        assert_select "input[type=checkbox][name=?][value=#{custom_field_id}]", 'tracker[custom_field_ids][]'
+      end
+      # project checked
+      project_ids.each do |project_id|
+        assert_select "input[type=checkbox][name=?][value=#{project_id}][checked=checked]", 'tracker[project_ids][]'
+      end
+      # project not checked
+      (Project.all.pluck(:id) - project_ids).each do |project_id|
+        assert_select "input[type=checkbox][name=?][value=#{project_id}]", 'tracker[project_ids][]'
+      end
+      # workflow copy selected
+      assert_select 'select[name=?]', 'copy_workflow_from' do
+        assert_select 'option[value="1"][selected=selected]'
+      end
+    end
+  end
+
   def test_create
     assert_difference 'Tracker.count' do
       post :create, :params => {
index 89a711fdd613ea312bce01572685110d1fc73046..1f3447da7cbe606b271778e298b6d97b57055dbd 100644 (file)
@@ -46,6 +46,34 @@ class TrackerTest < ActiveSupport::TestCase
     assert_equal [2], project.rolled_up_trackers(false).visible(user).map(&:id)
   end
 
+  def test_copy_from
+    tracker = Tracker.find(1)
+    copy = Tracker.new.copy_from(tracker)
+
+    assert_nil copy.id
+    assert_nil copy.position
+    assert_equal '', copy.name
+    assert_equal tracker.default_status_id, copy.default_status_id
+    assert_equal tracker.is_in_roadmap, copy.is_in_roadmap
+    assert_equal tracker.core_fields, copy.core_fields
+    assert_equal tracker.description, copy.description
+
+    copy.name = 'Copy'
+    assert copy.save
+  end
+
+  def test_copy_from_should_copy_custom_fields
+    tracker = Tracker.generate!(:custom_field_ids => [1, 2, 6])
+    copy = Tracker.new.copy_from(tracker)
+    assert_equal [1, 2, 6], copy.custom_field_ids.sort
+  end
+
+  def test_copy_from_should_copy_projects
+    tracker = Tracker.generate!(:project_ids => [1, 2, 3, 4, 5, 6])
+    copy = Tracker.new.copy_from(tracker)
+    assert_equal [1, 2, 3, 4, 5, 6], copy.project_ids.sort
+  end
+
   def test_copy_workflows
     source = Tracker.find(1)
     rules_count = source.workflow_rules.count