has_options.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Import::OTRS::DynamicField::Mixin::HasOptions
  3. extend ActiveSupport::Concern
  4. # Method to build nested structure
  5. def build_options_tree_structure(data)
  6. # Transform data into a hierarchical structure
  7. hierarchy = {}
  8. data.each do |path, name|
  9. current_level = hierarchy
  10. segments = path.split('::')
  11. segments.each_with_index do |segment, index|
  12. current_level[segment] ||= {}
  13. # Assign name to the last segment
  14. if index == segments.size - 1
  15. current_level[segment][:name] = name
  16. end
  17. current_level = current_level[segment]
  18. end
  19. end
  20. # Recursively build the array structure from the hierarchy.
  21. build_options_array_structure(hierarchy)
  22. end
  23. def build_options_array_structure(hierarchy)
  24. hierarchy.filter_map do |key, value|
  25. if value.is_a?(Hash) && value.key?(:name)
  26. # Using except to exclude the :name key
  27. children = value.except(:name)
  28. node = { 'value' => key, 'name' => value[:name] }
  29. node['children'] = build_options_array_structure(children) if children.any?
  30. node
  31. end
  32. end
  33. end
  34. def option_list(possible_values, tree_select)
  35. return possible_values if !tree_select
  36. build_options_tree_structure(possible_values)
  37. end
  38. end