From 78b88703d4936eabe69f056513035f27cc88c587 Mon Sep 17 00:00:00 2001 From: Marius BALTEANU Date: Tue, 1 May 2018 11:02:47 +0000 Subject: [PATCH 3/3] Auto complete should propose only valid parents --- app/controllers/auto_completes_controller.rb | 5 +++++ app/views/issues/_attributes.html.erb | 2 +- test/functional/auto_completes_controller_test.rb | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/controllers/auto_completes_controller.rb b/app/controllers/auto_completes_controller.rb index 293ac20..5a1cd8b 100644 --- a/app/controllers/auto_completes_controller.rb +++ b/app/controllers/auto_completes_controller.rb @@ -23,6 +23,7 @@ class AutoCompletesController < ApplicationController q = (params[:q] || params[:term]).to_s.strip status = params[:status].to_s issue_id = params[:issue_id].to_s + parent_tracker_id = params[:parent_tracker_id].to_s if q.present? scope = Issue.cross_project_scope(@project, params[:scope]).visible if status.present? @@ -31,6 +32,10 @@ class AutoCompletesController < ApplicationController if issue_id.present? scope = scope.where.not(:id => issue_id.to_i) end + if parent_tracker_id.present? + allowed_subtasks = Tracker.find(parent_tracker_id.to_i).allowed_subtask_ids + scope = scope.joins(:tracker).where(:tracker => allowed_subtasks) + end if q.match(/\A#?(\d+)\z/) @issues << scope.find_by_id($1.to_i) end diff --git a/app/views/issues/_attributes.html.erb b/app/views/issues/_attributes.html.erb index 3a3b5ac..b0df5a3 100644 --- a/app/views/issues/_attributes.html.erb +++ b/app/views/issues/_attributes.html.erb @@ -47,7 +47,7 @@
<% if @issue.safe_attribute? 'parent_issue_id' %>

<%= f.text_field :parent_issue_id, :size => 10, :required => @issue.required_attribute?('parent_issue_id') %>

-<%= javascript_tag "observeAutocompleteField('issue_parent_issue_id', '#{escape_javascript auto_complete_issues_path(:project_id => @issue.project, :scope => Setting.cross_project_subtasks, :status => @issue.closed? ? 'c' : 'o', :issue_id => @issue.id)}')" %> +<%= javascript_tag "observeAutocompleteField('issue_parent_issue_id', '#{escape_javascript auto_complete_issues_path(:project_id => @issue.project, :scope => Setting.cross_project_subtasks, :status => @issue.closed? ? 'c' : 'o', :issue_id => @issue.id, :parent_tracker_id => @issue.tracker.id)}')" %> <% end %> <% if @issue.safe_attribute? 'start_date' %> diff --git a/test/functional/auto_completes_controller_test.rb b/test/functional/auto_completes_controller_test.rb index f4f8b4f..26e926c 100644 --- a/test/functional/auto_completes_controller_test.rb +++ b/test/functional/auto_completes_controller_test.rb @@ -21,6 +21,7 @@ class AutoCompletesControllerTest < Redmine::ControllerTest fixtures :projects, :issues, :issue_statuses, :enumerations, :users, :issue_categories, :trackers, + :trackers_subtasks, :projects_trackers, :roles, :member_roles, @@ -138,4 +139,26 @@ class AutoCompletesControllerTest < Redmine::ControllerTest assert_include "issue", response.body assert_not_include "Bug #12: Closed issue on a locked version", response.body end + + def test_auto_complete_with_parent_tracker_id_should_return_only_valid_subtasks + Issue.generate!(:subject => "Feature request with issue in subject", :tracker_id => 2) + tracker = Tracker.find(1) + tracker.subtask_ids = [2, 3] + tracker.save! + + get :issues, :params => { + :project_id => 'ecookbook', + :q => 'issue', + :parent_tracker_id => '1' + } + + assert_response :success + + json = ActiveSupport::JSON.decode(response.body) + assert_kind_of Array, json + assert_equal 1, json.count + + assert_include "Feature request with issue in subject", response.body + assert_not_include "Bug", response.body + end end -- 2.1.4