Project

General

Profile

Feature #39805 ยป 39805.patch

Go MAEDA, 2023-12-11 09:40

View differences:

app/models/issue_query.rb
659 659
        "1=0"
660 660
      end
661 661
    when "~"
662
      root_id, lft, rgt = Issue.where(:id => value.first.to_i).pick(:root_id, :lft, :rgt)
663
      if root_id && lft && rgt
664
        "#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft > #{lft} AND #{Issue.table_name}.rgt < #{rgt}"
662
      ids = value.first.to_s.scan(/\d+/).map(&:to_i).uniq
663
      conditions = ids.filter_map do |id|
664
        root_id, lft, rgt = Issue.where(id: id).pick(:root_id, :lft, :rgt)
665
        if root_id && lft && rgt
666
          "(#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft > #{lft} AND #{Issue.table_name}.rgt < #{rgt})"
667
        else
668
          nil
669
        end
670
      end
671

  
672
      if conditions.any?
673
        "(#{conditions.join(' OR ')})"
665 674
      else
666 675
        "1=0"
667 676
      end
test/unit/query_test.rb
1819 1819
    assert_equal [], find_issues_with_query(query)
1820 1820
  end
1821 1821

  
1822
  def test_operator_contains_on_parent_id_should_accept_comma_separated_values
1823
    parent1 = Issue.generate!
1824
    children_of_parent1 = [
1825
      Issue.generate!(parent_id: parent1.id),
1826
      Issue.generate!(parent_id: parent1.id)
1827
    ]
1828
    parent2 = Issue.generate!
1829
    children_of_parent2 = [
1830
      Issue.generate!(parent_id: parent2.id),
1831
      Issue.generate!(parent_id: parent2.id)
1832
    ]
1833
    grandchild_of_parent2 = [
1834
      Issue.generate!(parent_id: children_for_parent2.first.id)
1835
    ]
1836

  
1837
    query = IssueQuery.new(name: '_')
1838
    query.add_filter('parent_id', '~', ["#{parent1.id},#{parent2.id}"])
1839
    issues = find_issues_with_query(query)
1840

  
1841
    expected =
1842
      children_of_parent1 + children_of_parent2 + grandchild_of_parent2
1843
    assert_equal expected.size, issues.size
1844
    assert_equal expected.map(&:id).sort, issues.map(&:id).sort
1845
  end
1846

  
1822 1847
  def test_filter_on_child
1823 1848
    Issue.delete_all
1824 1849
    parent = Issue.generate_with_descendants!
    (1-1/1)