diff --git a/app/models/issue_query.rb b/app/models/issue_query.rb index 7ac5f4a22..f8656621f 100644 --- a/app/models/issue_query.rb +++ b/app/models/issue_query.rb @@ -27,7 +27,7 @@ class IssueQuery < Query :default_order => 'desc', :caption => '#', :frozen => true), QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), - QueryColumn.new(:parent, + QueryParentIssueColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), QueryAssociationColumn.new(:parent, :subject, :caption => :field_parent_issue_subject), diff --git a/app/models/query.rb b/app/models/query.rb index 64977b545..8a5031129 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -213,6 +213,20 @@ class QueryAssociationCustomFieldColumn < QueryCustomFieldColumn end end +class QueryParentIssueColumn < QueryColumn + def groupable? + true + end + + def group_by_statement + "#{Issue.table_name}.parent_id" + end + + def group_value(object) + object.parent + end +end + class QueryFilter include Redmine::I18n @@ -942,6 +956,8 @@ class Query < ApplicationRecord column_sortable = column.sortable if column.is_a?(TimestampQueryColumn) column_sortable = Redmine::Database.timestamp_to_date(column.sortable, User.current.time_zone) + elsif column.is_a?(QueryParentIssueColumn) + column_sortable = "#{Issue.table_name}.parent_id" end Array(column_sortable).map {|s| Arel.sql("#{s} #{order}")} end @@ -1117,6 +1133,10 @@ class Query < ApplicationRecord c = group_by_column if c.is_a?(QueryCustomFieldColumn) r = r.keys.to_h { |k| [c.custom_field.cast_value(k), r[k]] } + elsif c.is_a?(QueryParentIssueColumn) + ids = r.keys.compact + issues_by_id = Issue.where(id: ids).index_by(&:id) + r = r.keys.to_h { |k| [k ? issues_by_id[k] : nil, r[k]] } end end r