Browse Source

Fixes #5116 - ".value" in text modules is not working

Co-authored-by: Tobias Schäfer <ts@zammad.com>
Co-authored-by: Florian Liebe <fl@zammad.com>
Tobias Schäfer 9 months ago
parent
commit
2ace248d95

+ 35 - 10
app/assets/javascripts/app/lib/app_post/utils.coffee

@@ -907,22 +907,47 @@ class App.Utils
       else if dataRef isnt undefined && dataRef isnt null && dataRef.toString
         # in case if we have a references object, check what datatype the attribute has
         # and e. g. convert timestamps/dates to browser locale
+        attributeName = level
+        objectName    = levels[levels.length - 2]
         className     = dataRefLast?.constructor?.className
-        lastLevelName = levels[levels.length - 2]
-        if lastLevelName && !className
-          className = lastLevelName.charAt(0).toUpperCase() + lastLevelName.slice(1)
+        if !className && objectName
+          className = objectName.charAt(0).toUpperCase() + objectName.slice(1)
+
+        if level is 'value'
+          attributeName = levels[levels.length - 2]
+          objectName    = levels[levels.length - 3]
+          className     = objectName.charAt(0).toUpperCase() + objectName.slice(1)
 
         if className && App[className]
           localClassRef = App[className]
           if localClassRef?.attributesGet
             attributes = localClassRef.attributesGet()
-            if attributes?[level]
-              if attributes[level]['tag'] is 'datetime'
-                value = App.i18n.translateTimestamp(dataRef)
-              else if attributes[level]['tag'] is 'date'
-                value = App.i18n.translateDate(dataRef)
-              else if attributes[level]['tag'] is 'autocompletion_ajax_external_data_source'
-                value = dataRef.value
+            if attributes?[attributeName]
+              dataType = attributes[attributeName]['tag']
+
+              if level is 'value'
+                switch dataType
+                  when 'select'
+                    key = dataRefLast[attributeName]
+                    value = attributes[attributeName]['historical_options'][key]
+                  when 'multiselect'
+                    key = dataRefLast[attributeName] || []
+                    value = key.map((element) -> attributes[attributeName]['historical_options'][element]).join(', ')
+                  when 'autocompletion_ajax_external_data_source'
+                    value = dataRefLast.label
+                  else
+                    value = dataRefLast[attributeName]
+              else
+                switch dataType
+                  when 'datetime'
+                    value = App.i18n.translateTimestamp(dataRef)
+                  when 'date'
+                    value = App.i18n.translateDate(dataRef)
+                  when 'multiselect'
+                    dataRef = dataRef || []
+                    value = dataRef.map((element) -> element).join(', ')
+                  when 'autocompletion_ajax_external_data_source'
+                    value = dataRef.value || '-'
 
         # as fallback use value of toString()
         if !value

+ 83 - 4
public/assets/tests/qunit/html_utils.js

@@ -1576,9 +1576,7 @@ QUnit.test("check replace tags", assert => {
   var attribute_external_source = {
     name: 'external_data_source', display: 'external_data_source',  tag: 'autocompletion_ajax_external_data_source', null: true
   };
-  App.Ticket.configure_attributes.push( attribute_external_source )
-  message = "<a href=\"https://example.co/product/#{ticket.external_data_source}\">some text</a>"
-  result  = '<a href=\"https://example.co/product/1234">some text</a>'
+  App.Ticket.configure_attributes.push(attribute_external_source)
   data    = {
     ticket: {
       external_data_source: {
@@ -1587,7 +1585,88 @@ QUnit.test("check replace tags", assert => {
       }
     }
   }
-  verify = App.Utils.replaceTags(message, data, true)
+  message = "<a href=\"https://example.co/product/#{ticket.external_data_source}\">some text</a>"
+  result  = '<a href=\"https://example.co/product/1234">some text</a>'
+  verify  = App.Utils.replaceTags(message, data, true)
+  assert.equal(verify, result)
+
+  message = "<a href=\"https://example.co/product/#{ticket.external_data_source.value}\">some text</a>"
+  result = '<a href=\"https://example.co/product/Example%20Label">some text</a>'
+  verify  = App.Utils.replaceTags(message, data, true)
+  assert.equal(verify, result)
+
+  data = { ticket: {} }
+  message = "Test: #{ticket.external_data_source}"
+  result = 'Test: -'
+  verify = App.Utils.replaceTags(message, data)
+  assert.equal(verify, result)
+
+  data = { ticket: {} }
+  message = "Test: #{ticket.external_data_source.value}"
+  result = 'Test: -'
+  verify = App.Utils.replaceTags(message, data)
+  assert.equal(verify, result)
+
+  var attribute_select = {
+    name: 'select', display: 'select', tag: 'select', null: true, nulloption: true, options: { a: 'Value A', b: 'Value B' }, value: null, historical_options: { a: 'Value A', b: 'Value B', c: 'Value C' }
+  }
+  App.Ticket.configure_attributes.push(attribute_select)
+  data = {
+    ticket: {
+      select: 'b',
+    }
+  }
+  message = "Test: #{ticket.select}"
+  result  = 'Test: b'
+  verify  = App.Utils.replaceTags(message, data)
+  assert.equal(verify, result)
+
+  message = "Test: #{ticket.select.value}"
+  result  = 'Test: Value B'
+  verify  = App.Utils.replaceTags(message, data)
+  assert.equal(verify, result)
+
+  data = { ticket: {} }
+  message = "Test: #{ticket.select}"
+  result = 'Test: -'
+  verify = App.Utils.replaceTags(message, data)
+  assert.equal(verify, result)
+
+  data = { ticket: {} }
+  message = "Test: #{ticket.select.value}"
+  result = 'Test: -'
+  verify = App.Utils.replaceTags(message, data)
+  assert.equal(verify, result)
+
+  var attribute_multiselect = {
+    name: 'multiselect', display: 'multiselect', tag: 'multiselect', null: true, nulloption: true, options: { a: 'Value A', b: 'Value B', c: 'Value C' }, value: null, historical_options: { a: 'Value A', b: 'Value B', c: 'Value C' }
+  }
+  App.Ticket.configure_attributes.push(attribute_multiselect)
+  data = {
+    ticket: {
+      multiselect: ['a', 'b'],
+    }
+  }
+  message = "Test: #{ticket.multiselect}"
+  result = 'Test: a, b'
+  verify = App.Utils.replaceTags(message, data)
+  assert.equal(verify, result)
+
+  message = "Test: #{ticket.multiselect.value}"
+  result = 'Test: Value A, Value B'
+  verify = App.Utils.replaceTags(message, data)
+  assert.equal(verify, result)
+
+  data = { ticket: {} }
+  message = "Test: #{ticket.multiselect}"
+  result = 'Test: -'
+  verify = App.Utils.replaceTags(message, data)
+  assert.equal(verify, result)
+
+  data = { ticket: {} }
+  message = "Test: #{ticket.multiselect.value}"
+  result = 'Test: -'
+  verify = App.Utils.replaceTags(message, data)
   assert.equal(verify, result)
 
   message = "<div>#{user.avatar(100, 100)}</div>"

+ 1 - 1
spec/system/examples/text_modules_examples.rb

@@ -232,7 +232,7 @@ RSpec.shared_examples 'text modules' do |path:, ticket: nil|
           within(:active_content) do
             find(:richtext).send_keys('::ext')
             page.send_keys(:enter)
-            expect(find(:richtext)).to have_text 'external aaa'
+            expect(find(:richtext)).to have_text 'external AAA'
           end
         end
       end