Skip to content

Commit

Permalink
Adds issue filters on parent/subtasks (#6118).
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.redmine.org/redmine/trunk@14304 e93f8b46-1217-0410-a6f0-8f06a7374b81
  • Loading branch information
jplang committed Jun 13, 2015
1 parent 3077ed8 commit 98f2b30
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/helpers/queries_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def filters_options_for_select(query)
ungrouped = []
grouped = {}
query.available_filters.map do |field, field_options|
if field_options[:type] == :relation
if [:tree, :relation].include?(field_options[:type])
group = :label_related_issues
elsif field =~ /^(.+)\./
# association filters
Expand Down
43 changes: 43 additions & 0 deletions app/models/issue_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ def initialize_available_filters
IssueRelation::TYPES.each do |relation_type, options|
add_available_filter relation_type, :type => :relation, :label => options[:name]
end
add_available_filter "parent_id", :type => :tree, :label => :field_parent_issue
add_available_filter "child_id", :type => :tree, :label => :label_subtask_plural

Tracker.disabled_core_fields(trackers).each {|field|
delete_available_filter field
Expand Down Expand Up @@ -451,6 +453,47 @@ def sql_for_is_private_field(field, operator, value)
"#{Issue.table_name}.is_private #{op} (#{va})"
end

def sql_for_parent_id_field(field, operator, value)
case operator
when "="
"#{Issue.table_name}.parent_id = #{value.first.to_i}"
when "~"
root_id, lft, rgt = Issue.where(:id => value.first.to_i).pluck(:root_id, :lft, :rgt).first
if root_id && lft && rgt
"#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft > #{lft} AND #{Issue.table_name}.rgt < #{rgt}"
else
"1=0"
end
when "!*"
"#{Issue.table_name}.parent_id IS NULL"
when "*"
"#{Issue.table_name}.parent_id IS NOT NULL"
end
end

def sql_for_child_id_field(field, operator, value)
case operator
when "="
parent_id = Issue.where(:id => value.first.to_i).pluck(:parent_id).first
if parent_id
"#{Issue.table_name}.id = #{parent_id}"
else
"1=0"
end
when "~"
root_id, lft, rgt = Issue.where(:id => value.first.to_i).pluck(:root_id, :lft, :rgt).first
if root_id && lft && rgt
"#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft < #{lft} AND #{Issue.table_name}.rgt > #{rgt}"
else
"1=0"
end
when "!*"
"#{Issue.table_name}.rgt - #{Issue.table_name}.lft = 1"
when "*"
"#{Issue.table_name}.rgt - #{Issue.table_name}.lft > 1"
end
end

def sql_for_relations(field, operator, value, options={})
relation_options = IssueRelation::TYPES[field]
return relation_options unless relation_options
Expand Down
3 changes: 2 additions & 1 deletion app/models/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ class StatementInvalid < ::ActiveRecord::StatementInvalid
:text => [ "~", "!~", "!*", "*" ],
:integer => [ "=", ">=", "<=", "><", "!*", "*" ],
:float => [ "=", ">=", "<=", "><", "!*", "*" ],
:relation => ["=", "=p", "=!p", "!p", "!*", "*"]
:relation => ["=", "=p", "=!p", "!p", "!*", "*"],
:tree => ["=", "~", "!*", "*"]
}

class_attribute :available_columns
Expand Down
1 change: 1 addition & 0 deletions public/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ function buildFilterRow(field, operator, values) {
break;
case "integer":
case "float":
case "tree":
tr.find('td.values').append(
'<span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'_1" size="6" class="value" /></span>' +
' <span style="display:none;"><input type="text" name="v['+field+'][]" id="values_'+fieldId+'_2" size="6" class="value" /></span>'
Expand Down
40 changes: 40 additions & 0 deletions test/unit/query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,46 @@ def test_filter_on_relations_should_not_ignore_other_filter
assert_equal [issue1], find_issues_with_query(query)
end

def test_filter_on_parent
Issue.delete_all
parent = Issue.generate_with_descendants!


query = IssueQuery.new(:name => '_')
query.filters = {"parent_id" => {:operator => '=', :values => [parent.id.to_s]}}
assert_equal parent.children.map(&:id).sort, find_issues_with_query(query).map(&:id).sort

query.filters = {"parent_id" => {:operator => '~', :values => [parent.id.to_s]}}
assert_equal parent.descendants.map(&:id).sort, find_issues_with_query(query).map(&:id).sort

query.filters = {"parent_id" => {:operator => '*', :values => ['']}}
assert_equal parent.descendants.map(&:id).sort, find_issues_with_query(query).map(&:id).sort

query.filters = {"parent_id" => {:operator => '!*', :values => ['']}}
assert_equal [parent.id], find_issues_with_query(query).map(&:id).sort
end

def test_filter_on_child
Issue.delete_all
parent = Issue.generate_with_descendants!
child, leaf = parent.children.sort_by(&:id)
grandchild = child.children.first


query = IssueQuery.new(:name => '_')
query.filters = {"child_id" => {:operator => '=', :values => [grandchild.id.to_s]}}
assert_equal [child.id], find_issues_with_query(query).map(&:id).sort

query.filters = {"child_id" => {:operator => '~', :values => [grandchild.id.to_s]}}
assert_equal [parent, child].map(&:id).sort, find_issues_with_query(query).map(&:id).sort

query.filters = {"child_id" => {:operator => '*', :values => ['']}}
assert_equal [parent, child].map(&:id).sort, find_issues_with_query(query).map(&:id).sort

query.filters = {"child_id" => {:operator => '!*', :values => ['']}}
assert_equal [grandchild, leaf].map(&:id).sort, find_issues_with_query(query).map(&:id).sort
end

def test_statement_should_be_nil_with_no_filters
q = IssueQuery.new(:name => '_')
q.filters = {}
Expand Down

0 comments on commit 98f2b30

Please sign in to comment.