diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 816a3f521..d2096a3d2 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -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] diff --git a/app/helpers/sort_helper.rb b/app/helpers/sort_helper.rb index 9fda5982b..fd797951a 100644 --- a/app/helpers/sort_helper.rb +++ b/app/helpers/sort_helper.rb @@ -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. diff --git a/app/models/query.rb b/app/models/query.rb index 64af16f54..efa161a62 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -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 diff --git a/test/functional/issues_controller_test.rb b/test/functional/issues_controller_test.rb index a6917d2f9..69545bf92 100644 --- a/test/functional/issues_controller_test.rb +++ b/test/functional/issues_controller_test.rb @@ -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