From 4e2f310fbd409c8c33f4aa76189f7682462b4fff Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Fri, 10 Dec 2021 11:29:21 +0100 Subject: [PATCH] Fixes #3869 - Overview select fields are sorted by key. --- .../_application_controller/table.coffee | 18 +++------ spec/system/ticket/view_spec.rb | 39 ++++++++++++++++++- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller/table.coffee b/app/assets/javascripts/app/controllers/_application_controller/table.coffee index 30c878ade..a1d9db8d7 100644 --- a/app/assets/javascripts/app/controllers/_application_controller/table.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller/table.coffee @@ -493,16 +493,6 @@ class App.ControllerTable extends App.Controller sortable: @dndCallback )) - getGroupByKeyName: (object, groupBy) -> - reference_key = groupBy + '_id' - - if reference_key of object - attribute = _.findWhere(object.constructor.configure_attributes, { name: reference_key }) - - return App[attribute.relation]?.find(object[reference_key])?.displayName() || reference_key - - groupBy - sortObjectKeys: (objects, direction) -> sorted = Object.keys(objects).sort() @@ -525,7 +515,7 @@ class App.ControllerTable extends App.Controller objectsToShow = @objectsOfPage(@pagerShownPage) if @groupBy # group by raw (and not printable) value so dates work also - objectsGrouped = _.groupBy(objectsToShow, (object) => object[@getGroupByKeyName(object, @groupBy)]) + objectsGrouped = _.groupBy(objectsToShow, (object) => @groupObjectName(object, @groupBy, excludeTags: ['date', 'datetime'])) else objectsGrouped = { '': objectsToShow } @@ -865,11 +855,15 @@ class App.ControllerTable extends App.Controller @objects = localObjects @lastSortedobjects = localObjects - groupObjectName: (object, key = undefined) -> + groupObjectName: (object, key = undefined, options = {}) -> group = object if key if key not of object key += '_id' + + # return internal value if needed + return object[key] if options.excludeTags && _.find(@attributesList, (attr) -> attr.name == key && _.contains(options.excludeTags, attr.tag)) + group = App.viewPrint(object, key, @attributesList) if _.isEmpty(group) group = '' diff --git a/spec/system/ticket/view_spec.rb b/spec/system/ticket/view_spec.rb index 67dd7631c..7a61cd2d4 100644 --- a/spec/system/ticket/view_spec.rb +++ b/spec/system/ticket/view_spec.rb @@ -363,7 +363,7 @@ RSpec.describe 'Ticket views', type: :system do true end - it 'does show the date groups sorted' do + it 'does show the values grouped and sorted by date key value (yyy-mm-dd) instead of display value' do visit 'ticket/view/all_unassigned' text = page.find('.js-tableBody').text(:all) @@ -372,5 +372,42 @@ RSpec.describe 'Ticket views', type: :system do expect(text.index('01/19/2019') < text.index('08/18/2021')).to eq(true) end end + + context 'when sorted by custom object select', authenticated_as: :authenticate, db_strategy: :reset do + let(:ticket1) { create(:ticket, group: Group.find_by(name: 'Users'), cselect: 'a') } + let(:ticket2) { create(:ticket, group: Group.find_by(name: 'Users'), cselect: 'b') } + let(:ticket3) { create(:ticket, group: Group.find_by(name: 'Users'), cselect: 'c') } + + def authenticate + create :object_manager_attribute_select, name: 'cselect', data_option: { + 'default' => '', + 'options' => { + 'a' => 'Zzz a', + 'b' => 'Yyy b', + 'c' => 'Xxx c', + }, + 'relation' => '', + 'nulloption' => true, + 'multiple' => false, + 'null' => true, + 'translate' => true, + 'maxlength' => 255 + } + ObjectManager::Attribute.migration_execute + ticket1 + ticket2 + ticket3 + Overview.find_by(link: 'all_unassigned').update(group_by: 'cselect') + true + end + + it 'does show the values grouped and sorted by display value instead of key value' do + visit 'ticket/view/all_unassigned' + text = page.find('.js-tableBody').text(:all) + + expect(text.index('Xxx c') < text.index('Yyy b')).to eq(true) + expect(text.index('Yyy b') < text.index('Zzz a')).to eq(true) + end + end end end