]> source.dussan.org Git - redmine.git/commitdiff
Filter issues by file description (#34715).
authorGo MAEDA <maeda@farend.jp>
Mon, 14 Jun 2021 07:01:27 +0000 (07:01 +0000)
committerGo MAEDA <maeda@farend.jp>
Mon, 14 Jun 2021 07:01:27 +0000 (07:01 +0000)
Patch by Yuichi HARADA.

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

app/helpers/queries_helper.rb
app/models/issue_query.rb
config/locales/en.yml
test/unit/query_test.rb

index dccf35adcaf931fdc2206ef35c7fe20da97a5532..4085673775fea9c4928d1155076f49d8c3b3c843 100644 (file)
@@ -41,6 +41,8 @@ module QueriesHelper
         group = :label_date
       elsif %w(estimated_hours spent_time).include?(field)
         group = :label_time_tracking
+      elsif %w(attachment attachment_description).include?(field)
+        group = :label_attachment
       end
       if group
         (grouped[group] ||= []) << [field_options[:name], field]
index 1ed7056d2fb70698fe9545eb1057fe157a6816a4..b36cbc5b1b484ee6ee88d6864a82eabd76c41bcd 100644 (file)
@@ -202,6 +202,10 @@ class IssueQuery < Query
       "attachment",
       :type => :text, :name => l(:label_attachment)
     )
+    add_available_filter(
+      "attachment_description",
+      :type => :text, :name => l(:label_attachment_description)
+    )
     if User.current.logged?
       add_available_filter(
         "watcher_id",
@@ -597,6 +601,23 @@ class IssueQuery < Query
     end
   end
 
+  def sql_for_attachment_description_field(field, operator, value)
+    cond_description = "a.description IS NOT NULL AND a.description <> ''"
+    c =
+      case operator
+      when '*', '!*'
+        (operator == '*' ? cond_description : "NOT (#{cond_description})")
+      when '~', '!~'
+        (operator == '~' ? '' : "#{cond_description} AND ") +
+        sql_contains('a.description', value.first, :match => (operator == '~'))
+      when '^', '$'
+        sql_contains('a.description', value.first, (operator == '^' ? :starts_with : :ends_with) => true)
+      else
+        '1=0'
+      end
+    "EXISTS (SELECT 1 FROM #{Attachment.table_name} a WHERE a.container_type = 'Issue' AND a.container_id = #{Issue.table_name}.id AND #{c})"
+  end
+
   def sql_for_parent_id_field(field, operator, value)
     case operator
     when "="
index 2ac0affb831be27e7313e37cdcf763772b47a8d0..d89d39ebcf37f66efa14778cd3bb508740b6c6e9 100644 (file)
@@ -701,6 +701,7 @@ en:
   label_attachment_delete: Delete file
   label_attachment_plural: Files
   label_file_added: File added
+  label_attachment_description: File description
   label_report: Report
   label_report_plural: Reports
   label_news: News
index d2159c84b478cb9d766525bef0df57cfcaf1ffaf..07921a951045d98899cdb0014e3abdd88e013a05 100644 (file)
@@ -1527,6 +1527,48 @@ class QueryTest < ActiveSupport::TestCase
     assert_equal [3, 4], issues.collect(&:id).sort
   end
 
+  def test_filter_on_attachment_description_when_any
+    query = IssueQuery.new(:name => '_')
+    query.filters = {"attachment_description" => {:operator => '*', :values =>  ['']}}
+    issues = find_issues_with_query(query)
+    assert_equal [2, 3, 14], issues.collect(&:id).sort
+  end
+
+  def test_filter_on_attachment_description_when_none
+    query = IssueQuery.new(:name => '_')
+    query.filters = {"attachment_description" => {:operator => '!*', :values =>  ['']}}
+    issues = find_issues_with_query(query)
+    assert_equal [2, 3, 4, 14], issues.collect(&:id).sort
+  end
+
+  def test_filter_on_attachment_description_when_contains
+    query = IssueQuery.new(:name => '_')
+    query.filters = {"attachment_description" => {:operator => '~', :values =>  ['attachment']}}
+    issues = find_issues_with_query(query)
+    assert_equal [3, 14], issues.collect(&:id).sort
+  end
+
+  def test_filter_on_attachment_description_when_does_not_contain
+    query = IssueQuery.new(:name => '_')
+    query.filters = {"attachment_description" => {:operator => '!~', :values =>  ['attachment']}}
+    issues = find_issues_with_query(query)
+    assert_equal [2], issues.collect(&:id).sort
+  end
+
+  def test_filter_on_attachment_description_when_starts_with
+    query = IssueQuery.new(:name => '_')
+    query.filters = {"attachment_description" => {:operator => '^', :values =>  ['attachment']}}
+    issues = find_issues_with_query(query)
+    assert_equal [14], issues.collect(&:id).sort
+  end
+
+  def test_filter_on_attachment_description_when_ends_with
+    query = IssueQuery.new(:name => '_')
+    query.filters = {"attachment_description" => {:operator => '$', :values =>  ['attachment']}}
+    issues = find_issues_with_query(query)
+    assert_equal [3], issues.collect(&:id).sort
+  end
+
   def test_filter_on_subject_when_starts_with
     query = IssueQuery.new(:name => '_')
     query.filters = {'subject' => {:operator => '^', :values => ['issue']}}