Commits

Anonymous committed b4f0c5e

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

  • Participants
  • Parent commits 1ab0889

Comments (0)

Files changed (4)

app/controllers/issues_controller.rb

     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]

app/helpers/sort_helper.rb

       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
     @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.

app/models/query.rb

     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
 
     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
 

test/functional/issues_controller_test.rb

     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