object_manager_attributes.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::Translation::Search::Collector::ObjectManagerAttributes < Service::Translation::Search::Collector
  3. private
  4. def list_sources
  5. @list_sources ||= display_names | option_labels
  6. end
  7. def search_sources
  8. @search_sources ||= list_sources.select { |source| source.downcase.include?(query.downcase) }
  9. end
  10. def object_attributes
  11. @object_attributes ||= ObjectManager::Attribute.where(editable: true)
  12. end
  13. def display_names
  14. object_attributes.pluck(:display)
  15. end
  16. def option_labels
  17. labels = []
  18. object_attributes.each do |attribute|
  19. labels |= labels_from_options(attribute)
  20. end
  21. labels
  22. end
  23. def labels_from_options(attribute)
  24. options = attribute[:data_option][:options]
  25. return [] if !attribute[:data_option][:translate] || options.nil?
  26. if options.is_a?(Array)
  27. return collect_labels(attribute[:data_option][:options])
  28. end
  29. options.values
  30. end
  31. def collect_labels(options, labels = [])
  32. options.each do |option|
  33. # Collect the 'name' value if it exists
  34. labels << option['name'] if option['name']
  35. # If 'children' key exists and is an array, recursively collect labels from the children
  36. if option['children'].is_a?(Array)
  37. collect_labels(option['children'], labels)
  38. end
  39. end
  40. labels
  41. end
  42. end