##// END OF EJS Templates
Group filters in the filter select list (#13849)....
Jean-Philippe Lang -
r13280:4d2688e7a8b9
parent child
Show More
@@ -21,14 +21,35 module QueriesHelper
21 21 include ApplicationHelper
22 22
23 23 def filters_options_for_select(query)
24 options_for_select(filters_options(query))
25 end
26
27 def filters_options(query)
28 options = [[]]
29 options += query.available_filters.map do |field, field_options|
30 [field_options[:name], field]
24 ungrouped = []
25 grouped = {}
26 query.available_filters.map do |field, field_options|
27 if field_options[:type] == :relation
28 group = :label_related_issues
29 elsif field =~ /^(.+)\./
30 # association filters
31 group = "field_#{$1}"
32 elsif %w(member_of_group assigned_to_role).include?(field)
33 group = :field_assigned_to
34 elsif field_options[:type] == :date_past || field_options[:type] == :date
35 group = :label_date
36 end
37 if group
38 (grouped[group] ||= []) << [field_options[:name], field]
39 else
40 ungrouped << [field_options[:name], field]
41 end
42 end
43 # Don't group dates if there's only one (eg. time entries filters)
44 if grouped[:label_date].try(:size) == 1
45 ungrouped << grouped.delete(:label_date).first
46 end
47 s = options_for_select([[]] + ungrouped)
48 if grouped.present?
49 localized_grouped = grouped.map {|k,v| [l(k), v]}
50 s << grouped_options_for_select(localized_grouped)
31 51 end
52 s
32 53 end
33 54
34 55 def query_filters_hidden_tags(query)
@@ -127,7 +127,7 function addFilter(field, operator, values) {
127 127 }
128 128 $('#cb_'+fieldId).prop('checked', true);
129 129 toggleFilter(field);
130 $('#add_filter_select').val('').children('option').each(function() {
130 $('#add_filter_select').val('').find('option').each(function() {
131 131 if ($(this).attr('value') == field) {
132 132 $(this).attr('disabled', true);
133 133 }
@@ -29,12 +29,55 class QueriesHelperTest < ActionView::TestCase
29 29 :projects_trackers,
30 30 :custom_fields_trackers
31 31
32 def test_filters_options_has_empty_item
33 query = IssueQuery.new
34 filter_count = query.available_filters.size
35 fo = filters_options(query)
36 assert_equal filter_count + 1, fo.size
37 assert_equal [], fo[0]
32 def test_filters_options_for_select_should_have_a_blank_option
33 options = filters_options_for_select(IssueQuery.new)
34 assert_select_in options, 'option[value=""]'
35 end
36
37 def test_filters_options_for_select_should_not_group_regular_filters
38 with_locale 'en' do
39 options = filters_options_for_select(IssueQuery.new)
40 assert_select_in options, 'option[value=status_id]:root'
41 assert_select_in options, 'option[value=status_id]', :text => 'Status'
42 end
43 end
44
45 def test_filters_options_for_select_should_group_date_filters
46 with_locale 'en' do
47 options = filters_options_for_select(IssueQuery.new)
48 assert_select_in options, 'optgroup[label=?]', 'Date', 1
49 assert_select_in options, 'optgroup > option[value=due_date]', :text => 'Due date'
50 end
51 end
52
53 def test_filters_options_for_select_should_not_group_only_one_date_filter
54 with_locale 'en' do
55 options = filters_options_for_select(TimeEntryQuery.new)
56 assert_select_in options, 'optgroup[label=?]', 'Date', 0
57 assert_select_in options, 'option[value=spent_on]:root', :text => 'Date'
58 end
59 end
60
61 def test_filters_options_for_select_should_group_relations_filters
62 with_locale 'en' do
63 options = filters_options_for_select(IssueQuery.new)
64 assert_select_in options, 'optgroup[label=?]', 'Related issues', 1
65 assert_select_in options, 'optgroup[label=?] > option', 'Related issues', 9
66 assert_select_in options, 'optgroup > option[value=relates]', :text => 'Related to'
67 end
68 end
69
70 def test_filters_options_for_select_should_group_associations_filters
71 CustomField.delete_all
72 cf1 = ProjectCustomField.create!(:name => 'Foo', :field_format => 'string', :is_filter => true)
73 cf2 = ProjectCustomField.create!(:name => 'Bar', :field_format => 'string', :is_filter => true)
74
75 with_locale 'en' do
76 options = filters_options_for_select(IssueQuery.new)
77 assert_select_in options, 'optgroup[label=?]', 'Project', 1
78 assert_select_in options, 'optgroup[label=?] > option', 'Project', 2
79 assert_select_in options, 'optgroup > option[value=?]', "project.cf_#{cf1.id}", :text => "Project's Foo"
80 end
38 81 end
39 82
40 83 def test_query_to_csv_should_translate_boolean_custom_field_values
General Comments 0
You need to be logged in to leave comments. Login now