Project

General

Profile

Feature #7867 » 7867-v2.patch

Go MAEDA, 2024-08-18 06:36

View differences:

app/models/issue_query.rb
171 171
      "author_id",
172 172
      :type => :list, :values => lambda {author_values}
173 173
    )
174
    add_available_filter(
175
      "author.group",
176
      :type => :list,
177
      :values => lambda {Group.givable.visible.pluck(:name, :id).map {|name, id| [name, id.to_s]}},
178
      :name => l(:label_attribute_of_author, :name => l(:label_group))
179
    )
180
    add_available_filter(
181
      "author.role",
182
      :type => :list,
183
      :values => lambda {Role.givable.pluck(:name, :id).map {|name, id| [name, id.to_s]}},
184
      :name => l(:label_attribute_of_author, :name => l(:field_role))
185
    )
174 186
    add_available_filter(
175 187
      "assigned_to_id",
176 188
      :type => :list_optional_with_history, :values => lambda {assigned_to_values}
......
608 620
    end
609 621
  end
610 622

  
623
  def sql_for_author_group_field(field, operator, value)
624
    groups = if value.empty?
625
      Group.givable
626
    else
627
      Group.where(:id => value).to_a
628
    end
629

  
630
    author_groups = groups.inject([]) do |user_ids, group|
631
      user_ids + group.user_ids + [group.id]
632
    end.uniq.compact.sort.collect(&:to_s)
633

  
634
    '(' + sql_for_field("author_id", operator, author_groups, Issue.table_name, "author_id", false) + ')'
635
  end
636

  
637
  def sql_for_author_role_field(field, operator, value)
638
    role_cond =
639
      if value.any?
640
        "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{self.class.connection.quote_string(val)}'"}.join(",") + ")"
641
      else
642
        "1=0"
643
      end
644
    sw = operator == "!" ? 'NOT' : ''
645
    nl = operator == "!" ? "#{Issue.table_name}.author_id IS NULL OR" : ''
646
    subquery =
647
      "SELECT 1" +
648
      " FROM #{Member.table_name} inner join #{MemberRole.table_name} on members.id = member_roles.member_id" +
649
      " WHERE #{Issue.table_name}.project_id = #{Member.table_name}.project_id AND #{Member.table_name}.user_id = #{Issue.table_name}.author_id AND #{role_cond}"
650
    "(#{nl} #{sw} EXISTS (#{subquery}))"
651
  end
652

  
611 653
  def sql_for_fixed_version_status_field(field, operator, value)
612 654
    where = sql_for_field(field, operator, value, Version.table_name, "status")
613 655
    version_id_scope = project ? project.shared_versions : Version.visible
test/unit/query_test.rb
2851 2851
    assert ! query.available_filters["assigned_to_role"][:values].include?(['Anonymous', '5'])
2852 2852
  end
2853 2853

  
2854
  def test_available_filters_should_include_author_group_filter
2855
    query = IssueQuery.new
2856
    assert query.available_filters.key?("author.group")
2857
    assert_equal :list, query.available_filters["author.group"][:type]
2858
    assert query.available_filters["author.group"][:values].present?
2859
    assert_equal Group.givable.sort.map {|g| [g.name, g.id.to_s]},
2860
                 query.available_filters["author.group"][:values].sort
2861
  end
2862

  
2863
  def test_available_filters_should_include_author_role_filter
2864
    query = IssueQuery.new
2865
    assert query.available_filters.key?("author.role")
2866
    assert_equal :list, query.available_filters["author.role"][:type]
2867

  
2868
    assert query.available_filters["author.role"][:values].include?(['Manager', '1'])
2869
    assert query.available_filters["author.role"][:values].include?(['Developer', '2'])
2870
    assert query.available_filters["author.role"][:values].include?(['Reporter', '3'])
2871

  
2872
    assert_not query.available_filters["author.role"][:values].include?(['Non member', '4'])
2873
    assert_not query.available_filters["author.role"][:values].include?(['Anonymous', '5'])
2874
  end
2875

  
2854 2876
  def test_available_filters_should_include_custom_field_according_to_user_visibility
2855 2877
    visible_field = IssueCustomField.generate!(:is_for_all => true, :is_filter => true, :visible => true)
2856 2878
    hidden_field = IssueCustomField.generate!(:is_for_all => true, :is_filter => true, :visible => false, :role_ids => [1])
......
3021 3043
    assert_query_result [@issue1, @issue2, @issue3, @issue4, @issue5], @query
3022 3044
  end
3023 3045

  
3046
  def test_author_group_filter_should_return_issues_with_or_without_author_in_group
3047
    project = Project.generate!
3048
    author = User.generate!
3049
    author_group = Group.generate!
3050
    not_author_group = Group.generate!
3051
    author.groups << author_group
3052
    issues = 3.times.collect { Issue.generate!(:project => project, :author => author) }
3053

  
3054
    query = IssueQuery.new(:name => '_', :project => project)
3055
    query.add_filter('author_group', '=', [author_group.id.to_s])
3056
    assert_equal 3, query.issues.count
3057
    assert_query_result issues, query
3058

  
3059
    query = IssueQuery.new(:name => '_', :project => project)
3060
    query.add_filter('author_group', '!', [not_author_group.id.to_s])
3061
    assert_equal 3, query.issues.count
3062
    assert_query_result issues, query
3063
  end
3064

  
3065
  def test_author_role_filter_should_return_issues_with_or_without_author_in_role
3066
    project = Project.generate!
3067
    author = User.generate!
3068
    manager_role = Role.find_by_name('Manager')
3069
    developer_role = Role.find_by_name('Developer')
3070
    User.add_to_project(author, project, manager_role)
3071
    issues = 3.times.collect { Issue.generate!(:project => project, :author => author) }
3072

  
3073
    query = IssueQuery.new(:name => 'issues generated by manager', :project => project)
3074
    query.add_filter('author_role', '=', [manager_role.id.to_s])
3075
    assert_equal 3, query.issues.count
3076
    assert_query_result issues, query
3077

  
3078
    query = IssueQuery.new(:name => 'issues does not generated by developer', :project => project)
3079
    query.add_filter('author_role', '!', [developer_role.id.to_s])
3080
    assert_equal 3, query.issues.count
3081
    assert_query_result issues, query
3082
  end
3083

  
3024 3084
  def test_query_column_should_accept_a_symbol_as_caption
3025 3085
    set_language_if_valid 'en'
3026 3086
    c = QueryColumn.new('foo', :caption => :general_text_Yes)
(2-2/2)