Group filters in the filter select list (#13849).

git-svn-id: http://svn.redmine.org/redmine/trunk@13662 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang
2014-11-28 09:31:56 +00:00
parent 86d8acb344
commit 4d2688e7a8
3 changed files with 78 additions and 14 deletions

View File

@@ -21,15 +21,36 @@ module QueriesHelper
include ApplicationHelper include ApplicationHelper
def filters_options_for_select(query) def filters_options_for_select(query)
options_for_select(filters_options(query)) 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 end
if group
def filters_options(query) (grouped[group] ||= []) << [field_options[:name], field]
options = [[]] else
options += query.available_filters.map do |field, field_options| ungrouped << [field_options[:name], field]
[field_options[:name], field]
end end
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) def query_filters_hidden_tags(query)
tags = ''.html_safe tags = ''.html_safe

View File

@@ -127,7 +127,7 @@ function addFilter(field, operator, values) {
} }
$('#cb_'+fieldId).prop('checked', true); $('#cb_'+fieldId).prop('checked', true);
toggleFilter(field); toggleFilter(field);
$('#add_filter_select').val('').children('option').each(function() { $('#add_filter_select').val('').find('option').each(function() {
if ($(this).attr('value') == field) { if ($(this).attr('value') == field) {
$(this).attr('disabled', true); $(this).attr('disabled', true);
} }

View File

@@ -29,12 +29,55 @@ class QueriesHelperTest < ActionView::TestCase
:projects_trackers, :projects_trackers,
:custom_fields_trackers :custom_fields_trackers
def test_filters_options_has_empty_item def test_filters_options_for_select_should_have_a_blank_option
query = IssueQuery.new options = filters_options_for_select(IssueQuery.new)
filter_count = query.available_filters.size assert_select_in options, 'option[value=""]'
fo = filters_options(query) end
assert_equal filter_count + 1, fo.size
assert_equal [], fo[0] 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 end
def test_query_to_csv_should_translate_boolean_custom_field_values def test_query_to_csv_should_translate_boolean_custom_field_values