Просмотр исходного кода

Fixes #3869 - Overview select fields are sorted by key.

Rolf Schmidt 3 лет назад
Родитель
Сommit
4e2f310fbd

+ 6 - 12
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 = ''

+ 38 - 1
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