]> source.dussan.org Git - redmine.git/commitdiff
Group filters in the filter select list (#13849).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 28 Nov 2014 09:31:56 +0000 (09:31 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 28 Nov 2014 09:31:56 +0000 (09:31 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@13662 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/queries_helper.rb
public/javascripts/application.js
test/unit/helpers/queries_helper_test.rb

index 1eb0fa593130b9d2ae823ee5114b6f2ae411123b..64dd3f81759668ec7b123d57fcae11d5841dc48e 100644 (file)
@@ -21,14 +21,35 @@ module QueriesHelper
   include ApplicationHelper
 
   def filters_options_for_select(query)
-    options_for_select(filters_options(query))
-  end
-
-  def filters_options(query)
-    options = [[]]
-    options += query.available_filters.map do |field, field_options|
-      [field_options[:name], field]
+    ungrouped = []
+    grouped = {}
+    query.available_filters.map do |field, field_options|
+      if field_options[:type] == :relation
+        group = :label_related_issues
+      elsif field =~ /^(.+)\./
+        # association filters
+        group = "field_#{$1}"
+      elsif %w(member_of_group assigned_to_role).include?(field)
+        group = :field_assigned_to
+      elsif field_options[:type] == :date_past || field_options[:type] == :date
+        group = :label_date
+      end
+      if group
+        (grouped[group] ||= []) << [field_options[:name], field]
+      else
+        ungrouped << [field_options[:name], field]
+      end
+    end
+    # Don't group dates if there's only one (eg. time entries filters)
+    if grouped[:label_date].try(:size) == 1 
+      ungrouped << grouped.delete(:label_date).first
+    end
+    s = options_for_select([[]] + ungrouped)
+    if grouped.present?
+      localized_grouped = grouped.map {|k,v| [l(k), v]}
+      s << grouped_options_for_select(localized_grouped)
     end
+    s
   end
 
   def query_filters_hidden_tags(query)
index e598bbcafb215ac1d39e2d8b15bae7acbec2f6c3..dc8837bbdd173a8d452020c08f83ecce95c7a06e 100644 (file)
@@ -127,7 +127,7 @@ function addFilter(field, operator, values) {
   }
   $('#cb_'+fieldId).prop('checked', true);
   toggleFilter(field);
-  $('#add_filter_select').val('').children('option').each(function() {
+  $('#add_filter_select').val('').find('option').each(function() {
     if ($(this).attr('value') == field) {
       $(this).attr('disabled', true);
     }
index db492be6e5a7964aa5572af47398ab8a62f7fd9f..37aa5fd4fe5eaadf13d9f85990b83fce2718f04e 100644 (file)
@@ -29,12 +29,55 @@ class QueriesHelperTest < ActionView::TestCase
            :projects_trackers,
            :custom_fields_trackers
 
-  def test_filters_options_has_empty_item
-    query = IssueQuery.new
-    filter_count = query.available_filters.size
-    fo = filters_options(query)
-    assert_equal filter_count + 1, fo.size
-    assert_equal [], fo[0]
+  def test_filters_options_for_select_should_have_a_blank_option
+    options = filters_options_for_select(IssueQuery.new)
+    assert_select_in options, 'option[value=""]'
+  end
+
+  def test_filters_options_for_select_should_not_group_regular_filters
+    with_locale 'en' do
+      options = filters_options_for_select(IssueQuery.new)
+      assert_select_in options, 'option[value=status_id]:root'
+      assert_select_in options, 'option[value=status_id]', :text => 'Status'
+    end
+  end
+
+  def test_filters_options_for_select_should_group_date_filters
+    with_locale 'en' do
+      options = filters_options_for_select(IssueQuery.new)
+      assert_select_in options, 'optgroup[label=?]', 'Date', 1
+      assert_select_in options, 'optgroup > option[value=due_date]', :text => 'Due date'
+    end
+  end
+
+  def test_filters_options_for_select_should_not_group_only_one_date_filter
+    with_locale 'en' do
+      options = filters_options_for_select(TimeEntryQuery.new)
+      assert_select_in options, 'optgroup[label=?]', 'Date', 0
+      assert_select_in options, 'option[value=spent_on]:root', :text => 'Date'
+    end
+  end
+
+  def test_filters_options_for_select_should_group_relations_filters
+    with_locale 'en' do
+      options = filters_options_for_select(IssueQuery.new)
+      assert_select_in options, 'optgroup[label=?]', 'Related issues', 1
+      assert_select_in options, 'optgroup[label=?] > option', 'Related issues', 9
+      assert_select_in options, 'optgroup > option[value=relates]', :text => 'Related to'
+    end
+  end
+
+  def test_filters_options_for_select_should_group_associations_filters
+    CustomField.delete_all
+    cf1 = ProjectCustomField.create!(:name => 'Foo', :field_format => 'string', :is_filter => true)
+    cf2 = ProjectCustomField.create!(:name => 'Bar', :field_format => 'string', :is_filter => true)
+
+    with_locale 'en' do
+      options = filters_options_for_select(IssueQuery.new)
+      assert_select_in options, 'optgroup[label=?]', 'Project', 1
+      assert_select_in options, 'optgroup[label=?] > option', 'Project', 2
+      assert_select_in options, 'optgroup > option[value=?]', "project.cf_#{cf1.id}", :text => "Project's Foo"
+    end
   end
 
   def test_query_to_csv_should_translate_boolean_custom_field_values