From 13028b913b56eb4b5a7007ea5eea65ea2b19828e Mon Sep 17 00:00:00 2001 From: Go MAEDA Date: Fri, 29 Dec 2023 08:24:43 +0000 Subject: [PATCH] Extend "contains" operator in "Parent task" filter to support multiple issue IDs (#39805). Patch by Go MAEDA (@maeda). git-svn-id: https://svn.redmine.org/redmine/trunk@22577 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/issue_query.rb | 15 ++++++++++++--- test/unit/query_test.rb | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/app/models/issue_query.rb b/app/models/issue_query.rb index b18117cc9..21552dfd6 100644 --- a/app/models/issue_query.rb +++ b/app/models/issue_query.rb @@ -662,9 +662,18 @@ class IssueQuery < Query "1=0" end when "~" - root_id, lft, rgt = Issue.where(:id => value.first.to_i).pick(:root_id, :lft, :rgt) - if root_id && lft && rgt - "#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft > #{lft} AND #{Issue.table_name}.rgt < #{rgt}" + ids = value.first.to_s.scan(/\d+/).map(&:to_i).uniq + conditions = ids.filter_map do |id| + root_id, lft, rgt = Issue.where(id: id).pick(:root_id, :lft, :rgt) + if root_id && lft && rgt + "(#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft > #{lft} AND #{Issue.table_name}.rgt < #{rgt})" + else + nil + end + end + + if conditions.any? + "(#{conditions.join(' OR ')})" else "1=0" end diff --git a/test/unit/query_test.rb b/test/unit/query_test.rb index a630f75d8..a1c528e26 100644 --- a/test/unit/query_test.rb +++ b/test/unit/query_test.rb @@ -1819,6 +1819,31 @@ class QueryTest < ActiveSupport::TestCase assert_equal [], find_issues_with_query(query) end + def test_operator_contains_on_parent_id_should_accept_comma_separated_values + parent1 = Issue.generate! + children_of_parent1 = [ + Issue.generate!(parent_id: parent1.id), + Issue.generate!(parent_id: parent1.id) + ] + parent2 = Issue.generate! + children_of_parent2 = [ + Issue.generate!(parent_id: parent2.id), + Issue.generate!(parent_id: parent2.id) + ] + grandchild_of_parent2 = [ + Issue.generate!(parent_id: children_of_parent2.first.id) + ] + + query = IssueQuery.new(name: '_') + query.add_filter('parent_id', '~', ["#{parent1.id},#{parent2.id}"]) + issues = find_issues_with_query(query) + + expected = + children_of_parent1 + children_of_parent2 + grandchild_of_parent2 + assert_equal expected.size, issues.size + assert_equal expected.map(&:id).sort, issues.map(&:id).sort + end + def test_filter_on_child Issue.delete_all parent = Issue.generate_with_descendants! -- 2.39.5