From: Jean-Philippe Lang Date: Fri, 28 Nov 2014 09:31:56 +0000 (+0000) Subject: Group filters in the filter select list (#13849). X-Git-Tag: 3.0.0~325 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4d2688e7a8b95a28c12f61b7d4ce8db6551283e8;p=redmine.git Group filters in the filter select list (#13849). git-svn-id: http://svn.redmine.org/redmine/trunk@13662 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/helpers/queries_helper.rb b/app/helpers/queries_helper.rb index 1eb0fa593..64dd3f817 100644 --- a/app/helpers/queries_helper.rb +++ b/app/helpers/queries_helper.rb @@ -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) diff --git a/public/javascripts/application.js b/public/javascripts/application.js index e598bbcaf..dc8837bbd 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -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); } diff --git a/test/unit/helpers/queries_helper_test.rb b/test/unit/helpers/queries_helper_test.rb index db492be6e..37aa5fd4f 100644 --- a/test/unit/helpers/queries_helper_test.rb +++ b/test/unit/helpers/queries_helper_test.rb @@ -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