Commit 5524e58d authored by jplang's avatar jplang

Ability to sort issues by grouped column (#3511).

git-svn-id: https://svn.redmine.org/redmine/trunk@10765 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent 6f6b5cfb
......@@ -56,6 +56,7 @@ class IssuesController < ApplicationController
retrieve_query
sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
sort_update(@query.sortable_columns)
@query.sort_criteria = sort_criteria.to_a
if @query.valid?
case params[:format]
......
......@@ -89,6 +89,10 @@ module SortHelper
sql.blank? ? nil : sql
end
def to_a
@criteria.dup
end
def add!(key, asc)
@criteria.delete_if {|k,o| k == key}
@criteria = [[key, asc]] + @criteria
......@@ -182,6 +186,10 @@ module SortHelper
@sort_criteria.to_sql
end
def sort_criteria
@sort_criteria
end
# Returns a link which sorts by the named column.
#
# - column is the name of an attribute in the sorted record collection.
......
......@@ -542,7 +542,7 @@ class Query < ActiveRecord::Base
if arg.is_a?(Hash)
arg = arg.keys.sort.collect {|k| arg[k]}
end
c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, o == 'desc' ? o : 'asc']}
c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, (o == 'desc' || o == false) ? 'desc' : 'asc']}
write_attribute(:sort_criteria, c)
end
......@@ -558,12 +558,17 @@ class Query < ActiveRecord::Base
sort_criteria && sort_criteria[arg] && sort_criteria[arg].last
end
def sort_criteria_order_for(key)
sort_criteria.detect {|k, order| key.to_s == k}.try(:last)
end
# Returns the SQL sort order that should be prepended for grouping
def group_by_sort_order
if grouped? && (column = group_by_column)
order = sort_criteria_order_for(column.name) || column.default_order
column.sortable.is_a?(Array) ?
column.sortable.collect {|s| "#{s} #{column.default_order}"}.join(',') :
"#{column.sortable} #{column.default_order}"
column.sortable.collect {|s| "#{s} #{order}"}.join(',') :
"#{column.sortable} #{order}"
end
end
......
......@@ -297,6 +297,26 @@ class IssuesControllerTest < ActionController::TestCase
end
end
def test_index_with_query_grouped_by_tracker
3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc'
assert_response :success
trackers = assigns(:issues).map(&:tracker).uniq
assert_equal [1, 2, 3], trackers.map(&:id)
end
def test_index_with_query_grouped_by_tracker_in_reverse_order
3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc,tracker:desc'
assert_response :success
trackers = assigns(:issues).map(&:tracker).uniq
assert_equal [3, 2, 1], trackers.map(&:id)
end
def test_index_with_query_id_and_project_id_should_set_session_query
get :index, :project_id => 1, :query_id => 4
assert_response :success
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment