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

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10765 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2012-10-29 18:32:41 +00:00
parent 1fc324b74c
commit 9f148e098b
4 changed files with 37 additions and 3 deletions

View File

@ -56,6 +56,7 @@ class IssuesController < ApplicationController
retrieve_query retrieve_query
sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
sort_update(@query.sortable_columns) sort_update(@query.sortable_columns)
@query.sort_criteria = sort_criteria.to_a
if @query.valid? if @query.valid?
case params[:format] case params[:format]

View File

@ -89,6 +89,10 @@ module SortHelper
sql.blank? ? nil : sql sql.blank? ? nil : sql
end end
def to_a
@criteria.dup
end
def add!(key, asc) def add!(key, asc)
@criteria.delete_if {|k,o| k == key} @criteria.delete_if {|k,o| k == key}
@criteria = [[key, asc]] + @criteria @criteria = [[key, asc]] + @criteria
@ -182,6 +186,10 @@ module SortHelper
@sort_criteria.to_sql @sort_criteria.to_sql
end end
def sort_criteria
@sort_criteria
end
# Returns a link which sorts by the named column. # Returns a link which sorts by the named column.
# #
# - column is the name of an attribute in the sorted record collection. # - column is the name of an attribute in the sorted record collection.

View File

@ -542,7 +542,7 @@ class Query < ActiveRecord::Base
if arg.is_a?(Hash) if arg.is_a?(Hash)
arg = arg.keys.sort.collect {|k| arg[k]} arg = arg.keys.sort.collect {|k| arg[k]}
end 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) write_attribute(:sort_criteria, c)
end end
@ -558,12 +558,17 @@ class Query < ActiveRecord::Base
sort_criteria && sort_criteria[arg] && sort_criteria[arg].last sort_criteria && sort_criteria[arg] && sort_criteria[arg].last
end 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 # Returns the SQL sort order that should be prepended for grouping
def group_by_sort_order def group_by_sort_order
if grouped? && (column = group_by_column) if grouped? && (column = group_by_column)
order = sort_criteria_order_for(column.name) || column.default_order
column.sortable.is_a?(Array) ? column.sortable.is_a?(Array) ?
column.sortable.collect {|s| "#{s} #{column.default_order}"}.join(',') : column.sortable.collect {|s| "#{s} #{order}"}.join(',') :
"#{column.sortable} #{column.default_order}" "#{column.sortable} #{order}"
end end
end end

View File

@ -297,6 +297,26 @@ class IssuesControllerTest < ActionController::TestCase
end end
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 def test_index_with_query_id_and_project_id_should_set_session_query
get :index, :project_id => 1, :query_id => 4 get :index, :project_id => 1, :query_id => 4
assert_response :success assert_response :success